From c35c4c41f8a244e28d19d3f9e9a8a146a25751ae Mon Sep 17 00:00:00 2001 From: rizky Date: Wed, 17 Apr 2024 15:59:24 -0700 Subject: [PATCH] fix --- gen/gen_form/new_field.ts | 17 +++++++-- gen/gen_relation/gen_relation.ts | 12 ++++--- gen/prop/gen_prop_fields.ts | 62 +++----------------------------- gen/utils.ts | 2 +- 4 files changed, 27 insertions(+), 66 deletions(-) diff --git a/gen/gen_form/new_field.ts b/gen/gen_form/new_field.ts index 1897732..7adf7e6 100755 --- a/gen/gen_form/new_field.ts +++ b/gen/gen_form/new_field.ts @@ -1,7 +1,6 @@ import { createId } from "@paralleldrive/cuid2"; -import { GFCol, createItem, formatName } from "../utils"; import { gen_relation } from "../gen_relation/gen_relation"; -import { FMLocal } from "@/comps/form/typings"; +import { GFCol, createItem, formatName } from "../utils"; export const newItem = (component: { id: string; props: Record; @@ -48,7 +47,19 @@ export const newField = async (arg: GFCol, opt: { parent_table: string }) => { } else if (["has-many", "has-one"].includes(arg.type) && arg.relation) { type = "relation"; const value = JSON.stringify( - arg.relation.fields.map((e) => JSON.stringify(e)) + arg.relation.fields.map((e) => { + if (e.relation) { + const rel = { ...e }; + const fields = e.relation.fields; + delete (e as any).relation.fields; + return { + value: JSON.stringify(e), + checked: fields.map((e) => JSON.stringify(e)), + }; + } else { + return JSON.stringify(e); + } + }) ); const item = createItem({ component: { diff --git a/gen/gen_relation/gen_relation.ts b/gen/gen_relation/gen_relation.ts index c2be375..4998347 100755 --- a/gen/gen_relation/gen_relation.ts +++ b/gen/gen_relation/gen_relation.ts @@ -93,20 +93,22 @@ const genHasMany = async ( (await gen_prop_fields(arg.parent_table)).map((e: any) => e.value) ); const pk = defs.find((e) => e.is_pk); - - result["has_many_list"] = data["has_many_list"]; - result["has_many_list"].value = `\ + if (pk) { + console.log(arg.parent_table); + result["has_many_list"] = data["has_many_list"]; + result["has_many_list"].value = `\ async () => { const result: { value: string; label: string }[] = []; const list = await db.${arg.parent_table}.findMany({ select: { - ${pk}: true, + ${pk?.name}: true, }, where: { }, }); return result; }`; - code.has_many_list = result["has_many_list"].value; + code.has_many_list = result["has_many_list"].value; + } } if (data["label"]) { diff --git a/gen/prop/gen_prop_fields.ts b/gen/prop/gen_prop_fields.ts index 310fb6f..3ef4855 100755 --- a/gen/prop/gen_prop_fields.ts +++ b/gen/prop/gen_prop_fields.ts @@ -1,4 +1,3 @@ -const cache = {} as Record; const single = {} as Record< string, { @@ -40,8 +39,6 @@ const load_single = async (table: string) => { }; export const gen_prop_fields = async (gen_table: string) => { - if (cache[gen_table]) return cache[gen_table]; - const result: { label: string; value: string; @@ -68,6 +65,8 @@ export const gen_prop_fields = async (gen_table: string) => { let options = []; const to = v.to; const from = v.from; + const parent_name = k; + const parent_rel = v; const { cols, rels } = await load_single(v.to.table); if (cols) { for (const [k, v] of Object.entries(cols)) { @@ -111,6 +110,9 @@ export const gen_prop_fields = async (gen_table: string) => { }), label: k, options: sub_opt, + checked: + parent_rel.type === "has-many" && + parent_rel.from.table === v.to.table, }); } } @@ -130,57 +132,3 @@ export const gen_prop_fields = async (gen_table: string) => { return result; }; - -// const result: { -// label: string; -// value: string; -// options?: any[]; -// checked?: boolean; -// }[] = []; -// const fields = await db._schema.columns(gen_table); -// for (const [k, v] of Object.entries(fields)) { -// result.push({ -// value: JSON.stringify({ -// name: k, -// is_pk: v.is_pk, -// type: v.db_type || v.type, -// optional: v.optional, -// }), -// label: k, -// checked: v.is_pk, -// }); -// } -// const rels = await db._schema.rels(gen_table); -// for (const [k, v] of Object.entries(rels)) { -// let options = []; -// const to = v.to; -// const from = v.from; -// const fields = await db._schema.columns(v.to.table); -// for (const [k, v] of Object.entries(fields)) { -// options.push({ -// value: JSON.stringify({ -// name: k, -// is_pk: v.is_pk, -// type: v.db_type || v.type, -// optional: v.optional, -// }), -// label: k, -// checked: v.is_pk, -// }); -// } -// result.push({ -// value: JSON.stringify({ -// name: k, -// is_pk: false, -// type: v.type, -// optional: true, -// relation: { from, to }, -// }), -// label: k, -// options, -// }); -// } - -// if (!cache[gen_table]) { -// cache[gen_table] = result; -// } diff --git a/gen/utils.ts b/gen/utils.ts index 9444251..0edf73d 100755 --- a/gen/utils.ts +++ b/gen/utils.ts @@ -14,7 +14,7 @@ export const parseGenField = (fields: PropOptRaw) => { } catch (e) {} } else { const field = JSON.parse(f.value); - field.relation.fields = f.checked.map((e) => JSON.parse(e)); + field.relation.fields = parseGenField(f.checked); result.push(field); } }