wip add batch update
This commit is contained in:
parent
95b75d44eb
commit
4ac633d4a2
|
|
@ -13,6 +13,35 @@ export type DBArg = {
|
||||||
export const execQuery = async (args: DBArg, prisma: any) => {
|
export const execQuery = async (args: DBArg, prisma: any) => {
|
||||||
const { table, action, params } = args;
|
const { table, action, params } = args;
|
||||||
|
|
||||||
|
if (action === "batch_update") {
|
||||||
|
const { table, batch } = params as unknown as {
|
||||||
|
table: string;
|
||||||
|
batch: { data: any; where: any }[];
|
||||||
|
};
|
||||||
|
|
||||||
|
const promises = [] as any[];
|
||||||
|
|
||||||
|
const tableInstance = prisma[table];
|
||||||
|
if (tableInstance) {
|
||||||
|
try {
|
||||||
|
for (const item of batch) {
|
||||||
|
if (
|
||||||
|
Object.entries(item.where).length > 0 &&
|
||||||
|
Object.entries(item.data).length > 0
|
||||||
|
) {
|
||||||
|
promises.push(
|
||||||
|
tableInstance.updateMany({ where: item.where, data: item.data })
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await Promise.all(promises);
|
||||||
|
} catch (e: any) {
|
||||||
|
throw new Error(e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
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));
|
||||||
|
|
@ -28,73 +57,85 @@ export const execQuery = async (args: DBArg, prisma: any) => {
|
||||||
type: string;
|
type: string;
|
||||||
optional: boolean;
|
optional: boolean;
|
||||||
db_type: string;
|
db_type: string;
|
||||||
default?: any
|
default?: any;
|
||||||
}
|
}
|
||||||
>;
|
>;
|
||||||
const rels = {} as Record<string, {
|
const rels = {} as Record<
|
||||||
type: 'has-many',
|
string,
|
||||||
to: { table: string, fields: string[] }
|
| {
|
||||||
from: { table: string, fields: string[] }
|
type: "has-many";
|
||||||
} | {
|
to: { table: string; fields: string[] };
|
||||||
type: 'has-one',
|
from: { table: string; fields: string[] };
|
||||||
to: { table: string, fields: string[] }
|
}
|
||||||
from: { table: string, fields: string[] }
|
| {
|
||||||
}>
|
type: "has-one";
|
||||||
|
to: { table: string; fields: string[] };
|
||||||
|
from: { table: string; fields: string[] };
|
||||||
|
}
|
||||||
|
>;
|
||||||
if (schema_table) {
|
if (schema_table) {
|
||||||
if (action === "schema_rels") {
|
if (action === "schema_rels") {
|
||||||
for (const col of schema_table.properties) {
|
for (const col of schema_table.properties) {
|
||||||
if (
|
if (
|
||||||
col.type === "field" &&
|
col.type === "field" &&
|
||||||
(!!col.array ||
|
(!!col.array || (col.attributes && col.attributes?.length > 0))
|
||||||
col.attributes &&
|
|
||||||
col.attributes?.length > 0)
|
|
||||||
) {
|
) {
|
||||||
if (col.array) {
|
if (col.array) {
|
||||||
if (typeof col.fieldType === 'string') {
|
if (typeof col.fieldType === "string") {
|
||||||
const target = schema.findByType('model', { name: col.fieldType })
|
const target = schema.findByType("model", {
|
||||||
|
name: col.fieldType,
|
||||||
|
});
|
||||||
|
|
||||||
if (target) {
|
if (target) {
|
||||||
const field = target.properties.find(e => {
|
const field = target.properties.find((e) => {
|
||||||
if (e.type === 'field'
|
if (e.type === "field" && e.fieldType === table) {
|
||||||
&& e.fieldType === table) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (field && field.type === 'field') {
|
if (field && field.type === "field") {
|
||||||
const rel = field.attributes?.find(e => e.kind === 'field');
|
const rel = field.attributes?.find(
|
||||||
|
(e) => e.kind === "field"
|
||||||
|
);
|
||||||
|
|
||||||
if (rel && rel.args) {
|
if (rel && rel.args) {
|
||||||
const { field, ref } = getFieldAndRef(rel, target, table);
|
const { field, ref } = getFieldAndRef(
|
||||||
|
rel,
|
||||||
|
target,
|
||||||
|
table
|
||||||
|
);
|
||||||
|
|
||||||
if (target && ref) {
|
if (target && ref) {
|
||||||
rels[col.name] = {
|
rels[col.name] = {
|
||||||
type: 'has-many',
|
type: "has-many",
|
||||||
to: field,
|
to: field,
|
||||||
from: ref
|
from: ref,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (col.attributes) {
|
} else if (col.attributes) {
|
||||||
const rel = col.attributes.find(e => e.type === 'attribute' && e.name === 'relation');
|
const rel = col.attributes.find(
|
||||||
if (rel && typeof col.fieldType === 'string') {
|
(e) => e.type === "attribute" && e.name === "relation"
|
||||||
const target = schema.findByType('model', { name: col.fieldType });
|
);
|
||||||
|
if (rel && typeof col.fieldType === "string") {
|
||||||
|
const target = schema.findByType("model", {
|
||||||
|
name: col.fieldType,
|
||||||
|
});
|
||||||
const { field, ref } = getFieldAndRef(rel, target, table);
|
const { field, ref } = getFieldAndRef(rel, target, table);
|
||||||
|
|
||||||
rels[col.name] = {
|
rels[col.name] = {
|
||||||
type: 'has-one',
|
type: "has-one",
|
||||||
to: {
|
to: {
|
||||||
table: field.table,
|
table: field.table,
|
||||||
fields: ref.fields
|
fields: ref.fields,
|
||||||
},
|
},
|
||||||
from: {
|
from: {
|
||||||
table: ref.table,
|
table: ref.table,
|
||||||
fields: field.fields
|
fields: field.fields,
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -108,10 +149,14 @@ export const execQuery = async (args: DBArg, prisma: any) => {
|
||||||
col.attributes &&
|
col.attributes &&
|
||||||
col.attributes?.length > 0
|
col.attributes?.length > 0
|
||||||
) {
|
) {
|
||||||
const attr = col.attributes.find(e => e.name !== 'id' && e.name !== 'default');
|
const attr = col.attributes.find(
|
||||||
|
(e) => e.name !== "id" && e.name !== "default"
|
||||||
|
);
|
||||||
|
|
||||||
const default_val = col.attributes.find(e => e.name === 'default');
|
const default_val = col.attributes.find(
|
||||||
const is_pk = col.attributes.find(e => e.name === 'id');
|
(e) => e.name === "default"
|
||||||
|
);
|
||||||
|
const is_pk = col.attributes.find((e) => e.name === "id");
|
||||||
|
|
||||||
if (attr && attr.name !== "relation") {
|
if (attr && attr.name !== "relation") {
|
||||||
let type = "String";
|
let type = "String";
|
||||||
|
|
@ -122,7 +167,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
|
||||||
type: type.toLowerCase(),
|
type: type.toLowerCase(),
|
||||||
optional: !!col.optional,
|
optional: !!col.optional,
|
||||||
db_type: attr.name.toLowerCase(),
|
db_type: attr.name.toLowerCase(),
|
||||||
default: default_val
|
default: default_val,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -165,27 +210,29 @@ export const execQuery = async (args: DBArg, prisma: any) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const getFieldAndRef = (rel: any, target: any, table: string) => {
|
const getFieldAndRef = (rel: any, target: any, table: string) => {
|
||||||
let field = null as unknown as { table: string, fields: string[] };
|
let field = null as unknown as { table: string; fields: string[] };
|
||||||
let ref = null as unknown as { table: string, fields: string[] };
|
let ref = null as unknown as { table: string; fields: string[] };
|
||||||
for (const e of rel.args) {
|
for (const e of rel.args) {
|
||||||
if (typeof e.value === 'object'
|
if (
|
||||||
&& !Array.isArray(e.value)
|
typeof e.value === "object" &&
|
||||||
&& e.value.type === 'keyValue'
|
!Array.isArray(e.value) &&
|
||||||
&& typeof e.value.value === 'object'
|
e.value.type === "keyValue" &&
|
||||||
&& !Array.isArray(e.value.value)
|
typeof e.value.value === "object" &&
|
||||||
&& e.value.value.type === 'array') {
|
!Array.isArray(e.value.value) &&
|
||||||
if (e.value.key === 'fields') {
|
e.value.value.type === "array"
|
||||||
|
) {
|
||||||
|
if (e.value.key === "fields") {
|
||||||
field = {
|
field = {
|
||||||
table: target.name,
|
table: target.name,
|
||||||
fields: e.value.value.args
|
fields: e.value.value.args,
|
||||||
}
|
};
|
||||||
} else if (e.value.key === 'references') {
|
} else if (e.value.key === "references") {
|
||||||
ref = {
|
ref = {
|
||||||
table: table,
|
table: table,
|
||||||
fields: e.value.value.args
|
fields: e.value.value.args,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return { field, ref };
|
return { field, ref };
|
||||||
}
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue