🚑 hotfix: seeding system

This commit is contained in:
Rafi Arrafif
2026-01-29 02:40:25 +07:00
parent e305d955f1
commit 5bfb376e88
5 changed files with 77 additions and 39 deletions

View 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");
};

View File

@ -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 });