From 96dbca0e55da730a9150f4947e6c8c18a011d318 Mon Sep 17 00:00:00 2001 From: Rizky Date: Tue, 25 Jun 2024 15:41:38 +0700 Subject: [PATCH] fix query --- pkgs/utils/query.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pkgs/utils/query.ts b/pkgs/utils/query.ts index 61f9292..2391081 100644 --- a/pkgs/utils/query.ts +++ b/pkgs/utils/query.ts @@ -228,7 +228,15 @@ export const execQuery = async (args: DBArg, prisma: any) => { const view = schema.findAllByType("view", {}).map((e) => e?.name); return [...tables, ...view]; } else { - const schema_table = schema.findByType("model", { name: table }); + let schema_table = schema.findByType("model", { name: table }); + let is_view = false; + if (!schema_table) { + const view = schema.findByType("view", { name: table }); + if (view) { + is_view = true; + schema_table = view as any; + } + } const columns = {} as Record< string, { @@ -257,19 +265,22 @@ export const execQuery = async (args: DBArg, prisma: any) => { const default_val = col.attributes.find( (e) => e.name === "default" ); - const is_pk = col.attributes.find((e) => e.name === "id"); + let is_pk = col.attributes.find((e) => e.name === "id"); let type = "String"; if (typeof col.fieldType === "string") type = col.fieldType; if ((attr && attr.name !== "relation") || !attr) { + const db_type = attr + ? attr.name.toLowerCase() + : type.toLowerCase(); + if (is_view && db_type === "unique") is_pk = true as any; + columns[col.name] = { is_pk: !!is_pk, type: type.toLowerCase(), optional: !!col.optional, - db_type: attr - ? attr.name.toLowerCase() - : type.toLowerCase(), + db_type, default: default_val, }; }