fix mode batch upsert

This commit is contained in:
Rizky 2024-08-12 10:50:33 +07:00
parent e23a4a2977
commit 78a4141dc8
1 changed files with 60 additions and 58 deletions

View File

@ -21,7 +21,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
table: string; table: string;
where: any; where: any;
data: any[]; data: any[];
mode: "field" | "relation"; mode: "field" | "raw";
}; };
}; };
if (arg) { if (arg) {
@ -89,71 +89,73 @@ export const execQuery = async (args: DBArg, prisma: any) => {
} }
>; >;
for (const row of data) { if (mode !== "raw") {
for (const [k, v] of Object.entries(row) as any) { for (const row of data) {
const rel = rels[k]; for (const [k, v] of Object.entries(row) as any) {
if (rel) { const rel = rels[k];
if (rel.type === "has-one") { if (rel) {
const to = rel.to.fields[0]; if (rel.type === "has-one") {
if (!v.connect && v[to]) { const to = rel.to.fields[0];
let newv = { connect: { [to]: v[to] } }; if (!v.connect && v[to]) {
row[k] = newv; let newv = { connect: { [to]: v[to] } };
} row[k] = newv;
} else if (rel.type === "has-many") { }
if (!rel_many[k]) { } else if (rel.type === "has-many") {
const schema_table = schema.findByType("model", { if (!rel_many[k]) {
name: rel.to.table, const schema_table = schema.findByType("model", {
}); name: rel.to.table,
});
let pks: Property[] = []; let pks: Property[] = [];
if (schema_table) { if (schema_table) {
for (const col of schema_table.properties) { for (const col of schema_table.properties) {
if (col.type === "field" && !col.array) { if (col.type === "field" && !col.array) {
if ( if (
col.attributes && col.attributes &&
col.attributes?.length > 0 col.attributes?.length > 0
) { ) {
const is_pk = col.attributes.find( const is_pk = col.attributes.find(
(e) => e.name === "id" (e) => e.name === "id"
); );
if (is_pk) { if (is_pk) {
pks.push(col); pks.push(col);
break; break;
}
} }
} }
} }
} }
rel_many[k] = {
to: rel.to.table,
select: new Set(),
from: rel.from.table,
pk: pks.map((e) => e.name),
ops: new Map(),
};
} }
rel_many[k] = { const tobe = createRelMany({
to: rel.to.table, row,
select: new Set(), k,
from: rel.from.table, schema,
pk: pks.map((e) => e.name), rel,
ops: new Map(), });
};
}
const tobe = createRelMany({ if (tobe) {
row, for (const row of tobe) {
k, for (const key of Object.keys(row)) {
schema, rel_many[k].select.add(key);
rel, }
});
if (tobe) {
for (const row of tobe) {
for (const key of Object.keys(row)) {
rel_many[k].select.add(key);
} }
} }
}
rel_many[k].ops.set(row, { rel_many[k].ops.set(row, {
delete: new Set(), delete: new Set(),
insert: new Set(), insert: new Set(),
tobe: tobe || [], tobe: tobe || [],
}); });
}
} }
} }
} }
@ -194,7 +196,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
return true; return true;
}); });
if (mode === "field") { if (mode !== "raw") {
for (const [k, v] of Object.entries(row) as any) { for (const [k, v] of Object.entries(row) as any) {
const rel = rels[k]; const rel = rels[k];
if (rel) { if (rel) {
@ -247,7 +249,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
} }
} }
if (mode === "field") { if (mode !== "raw") {
if (found) { if (found) {
updates.push({ ...row, ...where }); updates.push({ ...row, ...where });
} else { } else {