prasi-lib/gen/prop/gen_prop_fields.ts

62 lines
1.4 KiB
TypeScript
Executable File

const cache = {} as Record<string, any>;
export const gen_prop_fields = async (gen_table: string) => {
if (cache[gen_table]) return cache[gen_table];
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;
}
return result;
};