fix rels
This commit is contained in:
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
export type HasManyType = {
|
||||
type: "has-many";
|
||||
to: { table: string; fields: string[] };
|
||||
from: { table: string; fields: string[] };
|
||||
};
|
||||
|
||||
export type HasOneType = {
|
||||
type: "has-one";
|
||||
to: { table: string; fields: string[] };
|
||||
from: { table: string; fields: string[] };
|
||||
};
|
||||
@@ -0,0 +1,62 @@
|
||||
import { createPrismaSchemaBuilder, Field } from "@mrleebo/prisma-ast";
|
||||
import { get } from "utils/get";
|
||||
import { HasManyType } from "./types";
|
||||
|
||||
export const upsertRelMany = (arg: {
|
||||
row: any;
|
||||
k: string;
|
||||
schema: ReturnType<typeof createPrismaSchemaBuilder>;
|
||||
rel: HasManyType;
|
||||
}) => {
|
||||
const { row, k, schema, rel } = arg;
|
||||
|
||||
if (Array.isArray(row[k])) {
|
||||
const create_many: any[] = row[k].map((e) => {
|
||||
const item = {} as any;
|
||||
const rel_fields = schema.findByType("model", {
|
||||
name: rel.to.table,
|
||||
});
|
||||
|
||||
if (rel_fields && rel_fields.properties.length > 0) {
|
||||
const fields: Record<string, Field> = {};
|
||||
for (const f of Object.values(rel_fields.properties)) {
|
||||
if (f.type === "field") {
|
||||
fields[f.name] = f;
|
||||
}
|
||||
}
|
||||
for (const [k, v] of Object.entries(e)) {
|
||||
const f = fields[k];
|
||||
if (f && k !== rel.from.table) {
|
||||
const is_rel = f.attributes?.find(
|
||||
(e) => e.kind === "field" && e.name === "relation"
|
||||
);
|
||||
if (is_rel) {
|
||||
let to_field_raw = is_rel.args?.find(
|
||||
(e) =>
|
||||
e.type === "attributeArgument" && get(e, "value.value.args.0")
|
||||
);
|
||||
|
||||
if (to_field_raw) {
|
||||
const to_field = get(
|
||||
to_field_raw,
|
||||
"value.value.args.0"
|
||||
) as string;
|
||||
if (typeof v === "object" && (v as any)?.connect) {
|
||||
const pk = Object.values((v as any).connect)[0];
|
||||
if (pk) {
|
||||
delete item[k];
|
||||
item[to_field] = pk;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
item[k] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return item;
|
||||
});
|
||||
row[k] = { createMany: { data: create_many } };
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user