encryped query raw

This commit is contained in:
Rizky 2024-04-18 13:50:02 +07:00
parent 0fc9c6b3fb
commit 32a67fdcfe
1 changed files with 37 additions and 35 deletions

View File

@ -1,7 +1,8 @@
import { Attribute, createPrismaSchemaBuilder } from "@mrleebo/prisma-ast"; import { createPrismaSchemaBuilder } from "@mrleebo/prisma-ast";
import { readAsync } from "fs-jetpack"; import { readAsync } from "fs-jetpack";
import { Prisma } from "../../app/db/db"; import { Prisma } from "../../app/db/db";
import { dir } from "./dir"; import { dir } from "./dir";
import { gunzipAsync } from "./gzip";
export type DBArg = { export type DBArg = {
db: string; db: string;
@ -28,6 +29,8 @@ export const execQuery = async (args: DBArg, prisma: any) => {
for (const item of b) { for (const item of b) {
if ( if (
item.table && item.table &&
!!item.data &&
!!item.where &&
Object.entries(item.where).length > 0 && Object.entries(item.where).length > 0 &&
Object.entries(item.data).length > 0 Object.entries(item.data).length > 0
) { ) {
@ -147,40 +150,33 @@ export const execQuery = async (args: DBArg, prisma: any) => {
} }
return rels; return rels;
} else if (action === "schema_columns") { } else if (action === "schema_columns") {
console.log(schema_table.properties);
for (const col of schema_table.properties) { for (const col of schema_table.properties) {
if (col.type === "field" && !col.array) { if (
let is_pk = false; col.type === "field" &&
let type = col.fieldType; !col.array &&
let attr = undefined as Attribute | undefined; col.attributes &&
let default_val = undefined as Attribute | undefined; col.attributes?.length > 0
if (col.attributes && col.attributes?.length > 0) { ) {
attr = col.attributes.find( const attr = col.attributes.find(
(e) => e.name !== "id" && e.name !== "default" (e) => e.name !== "id" && e.name !== "default"
); );
default_val = col.attributes.find((e) => e.name === "default"); const default_val = col.attributes.find(
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";
if (typeof col.fieldType === "string") type = col.fieldType; if (typeof col.fieldType === "string") type = col.fieldType;
}
}
if (typeof type === "string") { columns[col.name] = {
const db_type = attr is_pk: !!is_pk,
? attr.name.toLowerCase() type: type.toLowerCase(),
: type.toLowerCase(); optional: !!col.optional,
if (db_type !== "relation") { db_type: attr.name.toLowerCase(),
columns[col.name] = { default: default_val,
is_pk: !!is_pk, };
type: type.toLowerCase(),
optional: !!col.optional,
db_type,
default: default_val,
};
}
} }
} }
} }
@ -195,11 +191,17 @@ export const execQuery = async (args: DBArg, prisma: any) => {
if (tableInstance) { if (tableInstance) {
if (action === "query" && table.startsWith("$query")) { if (action === "query" && table.startsWith("$query")) {
try { try {
if (table === "$queryRawUnsafe") { const gzip = params as unknown as string;
return await prisma.$queryRawUnsafe(params[0]);
const u8 = new Uint8Array([...atob(gzip)].map((c) => c.charCodeAt(0)));
const json = JSON.parse((await gunzipAsync(u8)).toString("utf8"));
if (Array.isArray(json)) {
const q = json.shift();
return await tableInstance.bind(prisma)(Prisma.sql(q, ...json));
} }
const q = params.shift();
return await tableInstance.bind(prisma)(Prisma.sql(q, ...params)); return [];
} catch (e) { } catch (e) {
console.log(e); console.log(e);
return e; return e;