update finfo
This commit is contained in:
parent
986ac4cb27
commit
9c04dadf03
|
|
@ -1,9 +1,9 @@
|
||||||
import { Property, createPrismaSchemaBuilder } from "@mrleebo/prisma-ast";
|
import { Property, createPrismaSchemaBuilder } from "@mrleebo/prisma-ast";
|
||||||
import { readAsync } from "fs-jetpack";
|
import { readAsync } from "fs-jetpack";
|
||||||
|
import { createRelMany } from "./db/create-rel-many";
|
||||||
import { HasManyType, HasOneType } from "./db/types";
|
import { HasManyType, HasOneType } from "./db/types";
|
||||||
import { dir } from "./dir";
|
import { dir } from "./dir";
|
||||||
import { gunzipAsync } from "./gzip";
|
import { gunzipAsync } from "./gzip";
|
||||||
import { createRelMany } from "./db/create-rel-many";
|
|
||||||
export type DBArg = {
|
export type DBArg = {
|
||||||
db: string;
|
db: string;
|
||||||
table: string;
|
table: string;
|
||||||
|
|
@ -113,7 +113,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
|
||||||
col.attributes?.length > 0
|
col.attributes?.length > 0
|
||||||
) {
|
) {
|
||||||
const is_pk = col.attributes.find(
|
const is_pk = col.attributes.find(
|
||||||
(e) => e.name === "id"
|
(e) => e.name === "id",
|
||||||
);
|
);
|
||||||
if (is_pk) {
|
if (is_pk) {
|
||||||
pks.push(col);
|
pks.push(col);
|
||||||
|
|
@ -234,7 +234,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
|
||||||
if (current.delete.size > 0) {
|
if (current.delete.size > 0) {
|
||||||
for (const d of current.delete) {
|
for (const d of current.delete) {
|
||||||
transactions.push(
|
transactions.push(
|
||||||
prisma[rel_many[k].to].delete({ where: d })
|
prisma[rel_many[k].to].delete({ where: d }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -276,7 +276,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
|
||||||
transactions.push(
|
transactions.push(
|
||||||
prisma[table].create({
|
prisma[table].create({
|
||||||
data: row,
|
data: row,
|
||||||
})
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -304,7 +304,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
transactions.push(
|
transactions.push(
|
||||||
prisma[table].update({ data: row, where, select })
|
prisma[table].update({ data: row, where, select }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -365,7 +365,10 @@ export const execQuery = async (args: DBArg, prisma: any) => {
|
||||||
const tableInstance = prisma[item.table];
|
const tableInstance = prisma[item.table];
|
||||||
if (tableInstance) {
|
if (tableInstance) {
|
||||||
promises.push(
|
promises.push(
|
||||||
tableInstance.updateMany({ where: item.where, data: item.data })
|
tableInstance.updateMany({
|
||||||
|
where: item.where,
|
||||||
|
data: item.data,
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -380,16 +383,19 @@ export const execQuery = async (args: DBArg, prisma: any) => {
|
||||||
}
|
}
|
||||||
if (action.startsWith("schema_")) {
|
if (action.startsWith("schema_")) {
|
||||||
const schema_path = dir("app/db/prisma/schema.prisma");
|
const schema_path = dir("app/db/prisma/schema.prisma");
|
||||||
|
|
||||||
const schema = createPrismaSchemaBuilder(await readAsync(schema_path));
|
const schema = createPrismaSchemaBuilder(await readAsync(schema_path));
|
||||||
|
|
||||||
if (action === "schema_tables") {
|
if (action === "schema_tables") {
|
||||||
const tables = schema.findAllByType("model", {}).map((e) => e?.name);
|
const tables = schema.findAllByType("model", {}).map((e) => e?.name);
|
||||||
const view = schema.findAllByType("view", {}).map((e) => e?.name);
|
const view = schema.findAllByType("view", {}).map((e) => e?.name);
|
||||||
return [...tables, ...view];
|
return [...tables, ...view];
|
||||||
} else {
|
} else {
|
||||||
let schema_table = schema.findByType("model", { name: table });
|
let schema_table = schema.findAllByType("model", { name: table })?.[0];
|
||||||
|
|
||||||
let is_view = false;
|
let is_view = false;
|
||||||
if (!schema_table) {
|
if (!schema_table) {
|
||||||
const view = schema.findByType("view", { name: table });
|
const view = schema.findAllByType("view", { name: table })?.[0];
|
||||||
if (view) {
|
if (view) {
|
||||||
is_view = true;
|
is_view = true;
|
||||||
schema_table = view as any;
|
schema_table = view as any;
|
||||||
|
|
@ -429,11 +435,11 @@ export const execQuery = async (args: DBArg, prisma: any) => {
|
||||||
if (col.type === "field" && !col.array) {
|
if (col.type === "field" && !col.array) {
|
||||||
if (col.attributes && col.attributes?.length > 0) {
|
if (col.attributes && col.attributes?.length > 0) {
|
||||||
const attr = col.attributes.find(
|
const attr = col.attributes.find(
|
||||||
(e) => e.name !== "id" && e.name !== "default"
|
(e) => e.name !== "id" && e.name !== "default",
|
||||||
);
|
);
|
||||||
|
|
||||||
const default_val = col.attributes.find(
|
const default_val = col.attributes.find(
|
||||||
(e) => e.name === "default"
|
(e) => e.name === "default",
|
||||||
);
|
);
|
||||||
let is_pk = col.attributes.find((e) => e.name === "id");
|
let is_pk = col.attributes.find((e) => e.name === "id");
|
||||||
|
|
||||||
|
|
@ -505,7 +511,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
|
||||||
|
|
||||||
if (params[0] && params[0].where) {
|
if (params[0] && params[0].where) {
|
||||||
const filtered = Object.values(params[0].where).filter(
|
const filtered = Object.values(params[0].where).filter(
|
||||||
(e) => e
|
(e) => e,
|
||||||
).length;
|
).length;
|
||||||
if (filtered === 0)
|
if (filtered === 0)
|
||||||
throw new Error("deleteMany without condition is forbidden");
|
throw new Error("deleteMany without condition is forbidden");
|
||||||
|
|
@ -584,7 +590,7 @@ const getRels = ({
|
||||||
});
|
});
|
||||||
if (field && field.type === "field") {
|
if (field && field.type === "field") {
|
||||||
const rel = field.attributes?.find(
|
const rel = field.attributes?.find(
|
||||||
(e: any) => e.kind === "field"
|
(e: any) => e.kind === "field",
|
||||||
);
|
);
|
||||||
|
|
||||||
if (rel && rel.args) {
|
if (rel && rel.args) {
|
||||||
|
|
@ -603,7 +609,7 @@ const getRels = ({
|
||||||
}
|
}
|
||||||
} else if (col.attributes) {
|
} else if (col.attributes) {
|
||||||
const rel = col.attributes.find(
|
const rel = col.attributes.find(
|
||||||
(e: any) => e.type === "attribute" && e.name === "relation"
|
(e: any) => e.type === "attribute" && e.name === "relation",
|
||||||
);
|
);
|
||||||
if (rel && typeof col.fieldType === "string") {
|
if (rel && typeof col.fieldType === "string") {
|
||||||
const target = schema.findByType("model", {
|
const target = schema.findByType("model", {
|
||||||
|
|
@ -629,7 +635,7 @@ const getRels = ({
|
||||||
});
|
});
|
||||||
if (target) {
|
if (target) {
|
||||||
const to = target.properties.find(
|
const to = target.properties.find(
|
||||||
(e) => e.type === "field" && e.fieldType === table
|
(e) => e.type === "field" && e.fieldType === table,
|
||||||
);
|
);
|
||||||
if (to && to.type === "field" && (to.attributes?.length || 0) > 0) {
|
if (to && to.type === "field" && (to.attributes?.length || 0) > 0) {
|
||||||
const rel = to.attributes?.find((e) => e.name === "relation");
|
const rel = to.attributes?.find((e) => e.name === "relation");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue