From f8afc41afbe1a140a4a277c4fc4c76db850cf6a6 Mon Sep 17 00:00:00 2001 From: Rizky Date: Wed, 13 Mar 2024 15:46:06 +0700 Subject: [PATCH] wip fix --- pkgs/utils/query.ts | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/pkgs/utils/query.ts b/pkgs/utils/query.ts index 1db46fb..143524c 100644 --- a/pkgs/utils/query.ts +++ b/pkgs/utils/query.ts @@ -14,31 +14,35 @@ export const execQuery = async (args: DBArg, prisma: any) => { const { table, action, params } = args; if (action === "batch_update") { - const { batch } = params as unknown as { - table: string; - batch: { table: string; data: any; where: any }[]; + const { table, batch } = params as unknown as { + table?: { table: string; data: any; where: any }[]; + batch?: { table: string; data: any; where: any }[]; }; const promises = [] as any[]; - try { - for (const item of batch) { - if ( - item.table && - Object.entries(item.where).length > 0 && - Object.entries(item.data).length > 0 - ) { - const tableInstance = prisma[item.table]; - if (tableInstance) { - promises.push( - tableInstance.updateMany({ where: item.where, data: item.data }) - ); + const b = table || batch; + + if (b) { + try { + for (const item of b) { + if ( + item.table && + Object.entries(item.where).length > 0 && + Object.entries(item.data).length > 0 + ) { + const tableInstance = prisma[item.table]; + if (tableInstance) { + promises.push( + tableInstance.updateMany({ where: item.where, data: item.data }) + ); + } } } + await Promise.all(promises); + } catch (e: any) { + throw new Error(e.message); } - await Promise.all(promises); - } catch (e: any) { - throw new Error(e.message); } return;