This commit is contained in:
Rizky 2024-06-25 06:58:28 +07:00
parent 6e5d462317
commit 9debeedd0f
1 changed files with 36 additions and 28 deletions

View File

@ -47,7 +47,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
} }
} }
} }
const rels = getRels({ schema_table, schema, table }) const rels = getRels({ schema_table, schema, table });
if (pks.length > 0) { if (pks.length > 0) {
if (Object.keys(where.length > 0)) { if (Object.keys(where.length > 0)) {
const select = {} as any; const select = {} as any;
@ -100,23 +100,22 @@ export const execQuery = async (args: DBArg, prisma: any) => {
if (inserts.length > 0) { if (inserts.length > 0) {
for (const row of inserts) { for (const row of inserts) {
for (const [k, v] of Object.entries(row) as any) { for (const [k, v] of Object.entries(row) as any) {
const rel = rels[k]; const rel = rels[k];
if (rel) { if (rel) {
if (rel.type === 'has-one') { if (rel.type === "has-one") {
const to = rel.to.fields[0] const to = rel.to.fields[0];
if (!v.connect && v[to]) { if (!v.connect && v[to]) {
let newv = { connect: { [to]: v[to] } } let newv = { connect: { [to]: v[to] } };
row[k] = newv; row[k] = newv;
} }
} }
} }
} }
if (typeof row._marker !== 'undefined') { if (typeof row._marker !== "undefined") {
marker[transactions.length] = row._marker; marker[transactions.length] = row._marker;
delete row._marker delete row._marker;
} }
transactions.push( transactions.push(
@ -138,19 +137,19 @@ export const execQuery = async (args: DBArg, prisma: any) => {
for (const [k, v] of Object.entries(row) as any) { for (const [k, v] of Object.entries(row) as any) {
const rel = rels[k]; const rel = rels[k];
if (rel) { if (rel) {
if (rel.type === 'has-one') { if (rel.type === "has-one") {
const to = rel.to.fields[0] const to = rel.to.fields[0];
if (!v.connect && v[to]) { if (!v.connect && v[to]) {
let newv = { connect: { [to]: v[to] } } let newv = { connect: { [to]: v[to] } };
row[k] = newv; row[k] = newv;
} }
} }
} }
} }
if (typeof row._marker !== 'undefined') { if (typeof row._marker !== "undefined") {
marker[transactions.length] = row._marker; marker[transactions.length] = row._marker;
delete row._marker delete row._marker;
} }
transactions.push( transactions.push(
@ -238,7 +237,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
>; >;
if (schema_table) { if (schema_table) {
if (action === "schema_rels") { if (action === "schema_rels") {
return getRels({ schema_table, schema, table }) return getRels({ schema_table, schema, table });
} else if (action === "schema_columns") { } else if (action === "schema_columns") {
for (const col of schema_table.properties) { for (const col of schema_table.properties) {
if (col.type === "field" && !col.array) { if (col.type === "field" && !col.array) {
@ -265,6 +264,11 @@ export const execQuery = async (args: DBArg, prisma: any) => {
: type.toLowerCase(), : type.toLowerCase(),
default: default_val, default: default_val,
}; };
const c = columns[col.name];
if (c.type === c.db_type) {
delete columns[col.name];
console.log("schema_cols", attr);
}
} }
} else if (typeof col.fieldType === "string") { } else if (typeof col.fieldType === "string") {
columns[col.name] = { columns[col.name] = {
@ -359,7 +363,15 @@ const getFieldAndRef = (rel: any, target: any, table: string) => {
return { field, ref }; return { field, ref };
}; };
const getRels = ({ schema_table, schema, table }: { schema_table: any, schema: any, table: any }) => { const getRels = ({
schema_table,
schema,
table,
}: {
schema_table: any;
schema: any;
table: any;
}) => {
const rels = {} as Record< const rels = {} as Record<
string, string,
| { | {
@ -396,11 +408,7 @@ const getRels = ({ schema_table, schema, table }: { schema_table: any, schema: a
); );
if (rel && rel.args) { if (rel && rel.args) {
const { field, ref } = getFieldAndRef( const { field, ref } = getFieldAndRef(rel, target, table);
rel,
target,
table
);
if (target && ref) { if (target && ref) {
rels[col.name] = { rels[col.name] = {
@ -439,4 +447,4 @@ const getRels = ({ schema_table, schema, table }: { schema_table: any, schema: a
} }
} }
return rels; return rels;
} };