This commit is contained in:
rizky 2024-04-17 15:59:24 -07:00
parent fa44a5b8d8
commit c35c4c41f8
4 changed files with 27 additions and 66 deletions

View File

@ -1,7 +1,6 @@
import { createId } from "@paralleldrive/cuid2"; import { createId } from "@paralleldrive/cuid2";
import { GFCol, createItem, formatName } from "../utils";
import { gen_relation } from "../gen_relation/gen_relation"; import { gen_relation } from "../gen_relation/gen_relation";
import { FMLocal } from "@/comps/form/typings"; import { GFCol, createItem, formatName } from "../utils";
export const newItem = (component: { export const newItem = (component: {
id: string; id: string;
props: Record<string, string>; props: Record<string, string>;
@ -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) { } else if (["has-many", "has-one"].includes(arg.type) && arg.relation) {
type = "relation"; type = "relation";
const value = JSON.stringify( 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({ const item = createItem({
component: { component: {

View File

@ -93,14 +93,15 @@ const genHasMany = async (
(await gen_prop_fields(arg.parent_table)).map((e: any) => e.value) (await gen_prop_fields(arg.parent_table)).map((e: any) => e.value)
); );
const pk = defs.find((e) => e.is_pk); const pk = defs.find((e) => e.is_pk);
if (pk) {
console.log(arg.parent_table);
result["has_many_list"] = data["has_many_list"]; result["has_many_list"] = data["has_many_list"];
result["has_many_list"].value = `\ result["has_many_list"].value = `\
async () => { async () => {
const result: { value: string; label: string }[] = []; const result: { value: string; label: string }[] = [];
const list = await db.${arg.parent_table}.findMany({ const list = await db.${arg.parent_table}.findMany({
select: { select: {
${pk}: true, ${pk?.name}: true,
}, },
where: { }, where: { },
}); });
@ -108,6 +109,7 @@ async () => {
}`; }`;
code.has_many_list = result["has_many_list"].value; code.has_many_list = result["has_many_list"].value;
} }
}
if (data["label"]) { if (data["label"]) {
result["label"] = data["label"]; result["label"] = data["label"];

View File

@ -1,4 +1,3 @@
const cache = {} as Record<string, any>;
const single = {} as Record< const single = {} as Record<
string, string,
{ {
@ -40,8 +39,6 @@ const load_single = async (table: string) => {
}; };
export const gen_prop_fields = async (gen_table: string) => { export const gen_prop_fields = async (gen_table: string) => {
if (cache[gen_table]) return cache[gen_table];
const result: { const result: {
label: string; label: string;
value: string; value: string;
@ -68,6 +65,8 @@ export const gen_prop_fields = async (gen_table: string) => {
let options = []; let options = [];
const to = v.to; const to = v.to;
const from = v.from; const from = v.from;
const parent_name = k;
const parent_rel = v;
const { cols, rels } = await load_single(v.to.table); const { cols, rels } = await load_single(v.to.table);
if (cols) { if (cols) {
for (const [k, v] of Object.entries(cols)) { for (const [k, v] of Object.entries(cols)) {
@ -111,6 +110,9 @@ export const gen_prop_fields = async (gen_table: string) => {
}), }),
label: k, label: k,
options: sub_opt, 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; 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;
// }

View File

@ -14,7 +14,7 @@ export const parseGenField = (fields: PropOptRaw) => {
} catch (e) {} } catch (e) {}
} else { } else {
const field = JSON.parse(f.value); 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); result.push(field);
} }
} }