adding batch upsert to server.ts
This commit is contained in:
parent
4bf174a09c
commit
67deac8114
|
|
@ -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 = <T extends Prisma.ModelName>(arg: {
|
||||
table: T;
|
||||
where: Exclude<
|
||||
Parameters<PrismaClient[T]["findMany"]>[0],
|
||||
undefined
|
||||
>["where"];
|
||||
data: Exclude<Parameters<PrismaClient[T]["create"]>[0], undefined>["data"][];
|
||||
mode?: "field" | "relation";
|
||||
}) => Promise<void>;
|
||||
|
|
|
|||
Loading…
Reference in New Issue