diff --git a/pkgs/utils/prisma.ts b/pkgs/utils/prisma.ts index c39bd90..44cc793 100644 --- a/pkgs/utils/prisma.ts +++ b/pkgs/utils/prisma.ts @@ -2,6 +2,8 @@ import { existsAsync } from "fs-jetpack"; import { dir } from "./dir"; import { $ } from "execa"; import { g } from "./global"; +import { Prisma, PrismaClient } from "../../app/db/db"; +import { execQuery } from "./query"; export const preparePrisma = async () => { if (process.env.DATABASE_URL && !g.db) { @@ -12,6 +14,19 @@ export const preparePrisma = async () => { const { PrismaClient } = await import("../../app/db/db"); g.db = new PrismaClient(); + (g.db as any)._batch = { + upsert: (async (arg) => { + return execQuery( + { + action: "batch_upsert", + params: { arg } as any, + db: "", + table: arg.table, + }, + g.db + ); + }) as Upsert, + }; } catch (e) { console.log("Prisma not initialized", e); } @@ -19,3 +34,13 @@ export const preparePrisma = async () => { g.dburl = process.env.DATABASE_URL || ""; }; + +type Upsert = (arg: { + table: T; + where: Exclude< + Parameters[0], + undefined + >["where"]; + data: Exclude[0], undefined>["data"][]; + mode?: "field" | "relation"; +}) => Promise;