add mode in batch upsert

This commit is contained in:
rizrmd 2024-06-04 13:07:03 +07:00
parent 22134b76db
commit 594b37371b
3 changed files with 19 additions and 9 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -1,22 +1,22 @@
{ {
"name": "pkgs", "name": "pkgs",
"dependencies": { "dependencies": {
"@mrleebo/prisma-ast": "^0.10.1",
"@surfy/multipart-parser": "^1.0.2", "@surfy/multipart-parser": "^1.0.2",
"@swc/core": "^1.3.91", "@swc/core": "^1.5.7",
"@types/mime": "^3.0.2", "@types/mime": "^3.0.2",
"@types/unzipper": "^0.10.7", "@types/unzipper": "^0.10.7",
"execa": "^8.0.1", "execa": "^8.0.1",
"rambda": "^9.1.0",
"fs-jetpack": "^5.1.0", "fs-jetpack": "^5.1.0",
"@mrleebo/prisma-ast": "^0.10.1",
"lmdb": "^2.8.5", "lmdb": "^2.8.5",
"mime": "^3.0.0", "mime": "^3.0.0",
"parse-multipart-data": "^1.5.0",
"pino": "^8.15.3", "pino": "^8.15.3",
"pino-pretty": "^10.2.0", "pino-pretty": "^10.2.0",
"radix3": "^1.1.0", "radix3": "^1.1.0",
"rambda": "^9.1.0",
"sharp": "^0.33.2",
"typescript": "^5.2.2", "typescript": "^5.2.2",
"unzipper": "^0.10.14", "unzipper": "^0.10.14"
"parse-multipart-data": "^1.5.0",
"sharp": "^0.33.2"
} }
} }

View File

@ -21,10 +21,12 @@ export const execQuery = async (args: DBArg, prisma: any) => {
table: string; table: string;
where: any; where: any;
data: any[]; data: any[];
mode: "field" | "relation";
}; };
}; };
if (arg) { if (arg) {
const { table, where, data } = arg; const { table, where, data } = arg;
const mode = arg.mode || "field";
if (table && where && data) { if (table && where && data) {
const transactions = []; const transactions = [];
@ -70,11 +72,19 @@ export const execQuery = async (args: DBArg, prisma: any) => {
return true; return true;
}); });
if (mode === "field") {
if (found) { if (found) {
updates.push({ ...row, ...where }); updates.push({ ...row, ...where });
} else { } else {
inserts.push({ ...row, ...where }); inserts.push({ ...row, ...where });
} }
} else {
if (found) {
updates.push({ ...row });
} else {
inserts.push({ ...row });
}
}
} }
if (exists_idx.size !== existing.length) { if (exists_idx.size !== existing.length) {