fix query

This commit is contained in:
Rizky 2024-08-12 10:15:23 +07:00
parent 9c04dadf03
commit e23a4a2977
1 changed files with 49 additions and 45 deletions

View File

@ -4,6 +4,7 @@ import { createRelMany } from "./db/create-rel-many";
import { HasManyType, HasOneType } from "./db/types"; import { HasManyType, HasOneType } from "./db/types";
import { dir } from "./dir"; import { dir } from "./dir";
import { gunzipAsync } from "./gzip"; import { gunzipAsync } from "./gzip";
export type DBArg = { export type DBArg = {
db: string; db: string;
table: string; table: string;
@ -113,7 +114,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
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);
@ -181,7 +182,6 @@ export const execQuery = async (args: DBArg, prisma: any) => {
const updates = [] as any[]; const updates = [] as any[];
const inserts = [] as any[]; const inserts = [] as any[];
const deletes = [] as any[]; const deletes = [] as any[];
const delete_has_many = [] as { table: string; where: any }[];
const exists_idx = new Set<number>(); const exists_idx = new Set<number>();
const marker = {} as any; const marker = {} as any;
@ -194,48 +194,52 @@ export const execQuery = async (args: DBArg, prisma: any) => {
return true; return true;
}); });
for (const [k, v] of Object.entries(row) as any) { if (mode === "field") {
const rel = rels[k]; for (const [k, v] of Object.entries(row) as any) {
if (rel) { const rel = rels[k];
if (rel.type === "has-many" && rel_many[k]) { if (rel) {
delete row[k]; if (rel.type === "has-many" && rel_many[k]) {
const current = rel_many[k].ops.get(row); delete row[k];
if (current && found && found[k] && current.tobe) { const current = rel_many[k].ops.get(row);
let cur_matches = new Set<any>(); if (current && found && found[k] && current.tobe) {
for (const tobe of current.tobe) { let cur_matches = new Set<any>();
let tobe_found = false; for (const tobe of current.tobe) {
for (const cur of found[k]) { let tobe_found = false;
let matched_all = true; for (const cur of found[k]) {
for (const [k, v] of Object.entries(tobe)) { let matched_all = true;
if (cur[k] !== v) { for (const [k, v] of Object.entries(tobe)) {
matched_all = false; if (cur[k] !== v) {
matched_all = false;
break;
}
}
if (matched_all) {
cur_matches.add(cur);
tobe_found = true;
break; break;
} }
} }
if (matched_all) { if (!tobe_found) {
cur_matches.add(cur); current.insert.add(tobe);
tobe_found = true;
break;
} }
} }
if (!tobe_found) {
current.insert.add(tobe); for (const cur of found[k]) {
if (!cur_matches.has(cur)) {
current.delete.add(cur);
}
} }
}
for (const cur of found[k]) { row[k] = {
if (!cur_matches.has(cur)) { createMany: { data: [...current.insert] },
current.delete.add(cur); };
}
}
row[k] = { createMany: { data: [...current.insert] } }; if (current.delete.size > 0) {
for (const d of current.delete) {
if (current.delete.size > 0) { transactions.push(
for (const d of current.delete) { prisma[rel_many[k].to].delete({ where: d })
transactions.push( );
prisma[rel_many[k].to].delete({ where: d }), }
);
} }
} }
} }
@ -276,7 +280,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
transactions.push( transactions.push(
prisma[table].create({ prisma[table].create({
data: row, data: row,
}), })
); );
} }
} }
@ -304,7 +308,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
} }
transactions.push( transactions.push(
prisma[table].update({ data: row, where, select }), prisma[table].update({ data: row, where, select })
); );
} }
} }
@ -368,7 +372,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
tableInstance.updateMany({ tableInstance.updateMany({
where: item.where, where: item.where,
data: item.data, data: item.data,
}), })
); );
} }
} }
@ -435,11 +439,11 @@ export const execQuery = async (args: DBArg, prisma: any) => {
if (col.type === "field" && !col.array) { if (col.type === "field" && !col.array) {
if (col.attributes && col.attributes?.length > 0) { if (col.attributes && col.attributes?.length > 0) {
const attr = col.attributes.find( const attr = col.attributes.find(
(e) => e.name !== "id" && e.name !== "default", (e) => e.name !== "id" && e.name !== "default"
); );
const default_val = col.attributes.find( const default_val = col.attributes.find(
(e) => e.name === "default", (e) => e.name === "default"
); );
let is_pk = col.attributes.find((e) => e.name === "id"); let is_pk = col.attributes.find((e) => e.name === "id");
@ -511,7 +515,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
if (params[0] && params[0].where) { if (params[0] && params[0].where) {
const filtered = Object.values(params[0].where).filter( const filtered = Object.values(params[0].where).filter(
(e) => e, (e) => e
).length; ).length;
if (filtered === 0) if (filtered === 0)
throw new Error("deleteMany without condition is forbidden"); throw new Error("deleteMany without condition is forbidden");
@ -590,7 +594,7 @@ const getRels = ({
}); });
if (field && field.type === "field") { if (field && field.type === "field") {
const rel = field.attributes?.find( const rel = field.attributes?.find(
(e: any) => e.kind === "field", (e: any) => e.kind === "field"
); );
if (rel && rel.args) { if (rel && rel.args) {
@ -609,7 +613,7 @@ const getRels = ({
} }
} else if (col.attributes) { } else if (col.attributes) {
const rel = col.attributes.find( const rel = col.attributes.find(
(e: any) => e.type === "attribute" && e.name === "relation", (e: any) => e.type === "attribute" && e.name === "relation"
); );
if (rel && typeof col.fieldType === "string") { if (rel && typeof col.fieldType === "string") {
const target = schema.findByType("model", { const target = schema.findByType("model", {
@ -635,7 +639,7 @@ const getRels = ({
}); });
if (target) { if (target) {
const to = target.properties.find( const to = target.properties.find(
(e) => e.type === "field" && e.fieldType === table, (e) => e.type === "field" && e.fieldType === table
); );
if (to && to.type === "field" && (to.attributes?.length || 0) > 0) { if (to && to.type === "field" && (to.attributes?.length || 0) > 0) {
const rel = to.attributes?.find((e) => e.name === "relation"); const rel = to.attributes?.find((e) => e.name === "relation");