43 lines
864 B
TypeScript
43 lines
864 B
TypeScript
import { Prisma } from "../../app/db/db";
|
|
|
|
export type DBArg = {
|
|
db: string;
|
|
table: string;
|
|
action: string;
|
|
params: any[];
|
|
};
|
|
|
|
export const execQuery = async (args: DBArg, prisma: any) => {
|
|
const { table, action, params } = args;
|
|
|
|
const tableInstance = prisma[table];
|
|
|
|
if (tableInstance) {
|
|
if (action === "query" && table.startsWith("$query")) {
|
|
try {
|
|
const q = params.shift();
|
|
return await tableInstance.bind(prisma)(Prisma.sql(q, ...params));
|
|
} catch (e) {
|
|
console.log(e);
|
|
return e;
|
|
}
|
|
}
|
|
|
|
const method = tableInstance[action];
|
|
|
|
if (method) {
|
|
try {
|
|
const result = await method(...params);
|
|
|
|
if (!result) {
|
|
return JSON.stringify(result);
|
|
}
|
|
|
|
return result;
|
|
} catch (e: any) {
|
|
throw new Error(e.message);
|
|
}
|
|
}
|
|
}
|
|
};
|