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) {
@ -88,72 +88,74 @@ export const execQuery = async (args: DBArg, prisma: any) => {
>; >;
} }
>; >;
if (mode !== "raw") {
for (const row of data) {
for (const [k, v] of Object.entries(row) as any) {
const rel = rels[k];
if (rel) {
if (rel.type === "has-one") {
const to = rel.to.fields[0];
if (!v.connect && v[to]) {
let newv = { connect: { [to]: v[to] } };
row[k] = newv;
}
} else if (rel.type === "has-many") {
if (!rel_many[k]) {
const schema_table = schema.findByType("model", {
name: rel.to.table,
});
for (const row of data) { let pks: Property[] = [];
for (const [k, v] of Object.entries(row) as any) { if (schema_table) {
const rel = rels[k]; for (const col of schema_table.properties) {
if (rel) { if (col.type === "field" && !col.array) {
if (rel.type === "has-one") { if (
const to = rel.to.fields[0]; col.attributes &&
if (!v.connect && v[to]) { col.attributes?.length > 0
let newv = { connect: { [to]: v[to] } }; ) {
row[k] = newv; const is_pk = col.attributes.find(
} (e) => e.name === "id"
} else if (rel.type === "has-many") { );
if (!rel_many[k]) { if (is_pk) {
const schema_table = schema.findByType("model", { pks.push(col);
name: rel.to.table, break;
}); }
let pks: Property[] = [];
if (schema_table) {
for (const col of schema_table.properties) {
if (col.type === "field" && !col.array) {
if (
col.attributes &&
col.attributes?.length > 0
) {
const is_pk = col.attributes.find(
(e) => e.name === "id"
);
if (is_pk) {
pks.push(col);
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 {