🚑 hotfix: seeding system
This commit is contained in:
28
src/helpers/files/createFile/index.ts
Normal file
28
src/helpers/files/createFile/index.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
interface CreateFileConfig {
|
||||
targetDir: string;
|
||||
fileName: string;
|
||||
overwriteIfExists?: boolean;
|
||||
}
|
||||
|
||||
export const createFile = async (content: string, config: CreateFileConfig) => {
|
||||
const targetDir = path.join(process.cwd(), config.targetDir);
|
||||
const targetFile = path.join(targetDir, config.fileName);
|
||||
|
||||
// If file exists and overwrite is not allowed, throw an error
|
||||
if (fs.existsSync(targetFile) && !config.overwriteIfExists) {
|
||||
throw new Error(
|
||||
`File ${config.fileName} already exists in ${config.targetDir}`,
|
||||
);
|
||||
}
|
||||
|
||||
// Ensure target directory exists
|
||||
if (!fs.existsSync(targetDir)) {
|
||||
await fs.promises.mkdir(targetDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Write content to the file
|
||||
await fs.promises.writeFile(targetFile, content, "utf8");
|
||||
};
|
||||
@ -2,7 +2,6 @@ import { PrismaClient } from "@prisma/client";
|
||||
import { PrismaPg } from "@prisma/adapter-pg";
|
||||
import pg from "pg";
|
||||
import dotenv from "dotenv";
|
||||
import { generateUUIDv7 } from "../../../helpers/databases/uuidv7";
|
||||
dotenv.config();
|
||||
|
||||
const pool = new pg.Pool({
|
||||
@ -10,26 +9,4 @@ const pool = new pg.Pool({
|
||||
});
|
||||
|
||||
const adapter = new PrismaPg(pool);
|
||||
export const prisma = new PrismaClient({ adapter }).$extends({
|
||||
query: {
|
||||
$allModels: {
|
||||
async create({ args, query }) {
|
||||
if (
|
||||
args.data &&
|
||||
typeof args.data === "object" &&
|
||||
!("id" in args.data)
|
||||
) {
|
||||
// do nothing
|
||||
} else if (args.data && !args.data.id) {
|
||||
(args.data as { id?: string }).id = generateUUIDv7();
|
||||
}
|
||||
return query(args);
|
||||
},
|
||||
async upsert({ args, query }) {
|
||||
const create = args.create as any;
|
||||
if (create && !create.id) create.id = generateUUIDv7();
|
||||
return query(args);
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
export const prisma = new PrismaClient({ adapter });
|
||||
|
||||
Reference in New Issue
Block a user