This commit is contained in:
Benjamin Toby 2025-06-01 11:39:56 +01:00
parent b28b05956b
commit 9b7c98cff6
18 changed files with 151 additions and 54 deletions

View File

@ -6,5 +6,6 @@ type Param = {
}; };
export default function addQueue({ queue, userId, dummy }: Param): Promise<(import("../../../types").PostReturn & { export default function addQueue({ queue, userId, dummy }: Param): Promise<(import("../../../types").PostReturn & {
queryObject?: ReturnType<Awaited<typeof import("../../dsql/sql/sql-generator").default>>; queryObject?: ReturnType<Awaited<typeof import("../../dsql/sql/sql-generator").default>>;
count?: number;
}) | null | undefined>; }) | null | undefined>;
export {}; export {};

View File

@ -7,6 +7,7 @@ type Param<T extends {
genObject?: ServerQueryParam<T>; genObject?: ServerQueryParam<T>;
tableName: string; tableName: string;
dbFullName?: string; dbFullName?: string;
count?: boolean;
}; };
type Return = { type Return = {
string: string; string: string;
@ -20,5 +21,5 @@ export default function sqlGenerator<T extends {
[key: string]: any; [key: string]: any;
} = { } = {
[key: string]: any; [key: string]: any;
}>({ tableName, genObject, dbFullName }: Param<T>): Return; }>({ tableName, genObject, dbFullName, count }: Param<T>): Return;
export {}; export {};

View File

@ -5,7 +5,7 @@ exports.default = sqlGenerator;
* # SQL Query Generator * # SQL Query Generator
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql` * @description Generates an SQL Query for node module `mysql` or `serverless-mysql`
*/ */
function sqlGenerator({ tableName, genObject, dbFullName }) { function sqlGenerator({ tableName, genObject, dbFullName, count }) {
const finalQuery = (genObject === null || genObject === void 0 ? void 0 : genObject.query) ? genObject.query : undefined; const finalQuery = (genObject === null || genObject === void 0 ? void 0 : genObject.query) ? genObject.query : undefined;
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined; const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
const sqlSearhValues = []; const sqlSearhValues = [];
@ -109,7 +109,10 @@ function sqlGenerator({ tableName, genObject, dbFullName }) {
let queryString = (() => { let queryString = (() => {
var _a, _b, _c; var _a, _b, _c;
let str = "SELECT"; let str = "SELECT";
if ((_a = genObject === null || genObject === void 0 ? void 0 : genObject.selectFields) === null || _a === void 0 ? void 0 : _a[0]) { if (count) {
str += ` COUNT(*)`;
}
else if ((_a = genObject === null || genObject === void 0 ? void 0 : genObject.selectFields) === null || _a === void 0 ? void 0 : _a[0]) {
if (genObject.join) { if (genObject.join) {
str += ` ${(_b = genObject.selectFields) === null || _b === void 0 ? void 0 : _b.map((fld) => `${finalDbName}${tableName}.${fld}`).join(",")}`; str += ` ${(_b = genObject.selectFields) === null || _b === void 0 ? void 0 : _b.map((fld) => `${finalDbName}${tableName}.${fld}`).join(",")}`;
} }
@ -125,7 +128,7 @@ function sqlGenerator({ tableName, genObject, dbFullName }) {
str += " *"; str += " *";
} }
} }
if (genObject === null || genObject === void 0 ? void 0 : genObject.join) { if ((genObject === null || genObject === void 0 ? void 0 : genObject.join) && !count) {
const existingJoinTableNames = [tableName]; const existingJoinTableNames = [tableName];
str += str +=
"," + "," +
@ -198,13 +201,13 @@ function sqlGenerator({ tableName, genObject, dbFullName }) {
const stringOperator = (genObject === null || genObject === void 0 ? void 0 : genObject.searchOperator) || "AND"; const stringOperator = (genObject === null || genObject === void 0 ? void 0 : genObject.searchOperator) || "AND";
queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)}`; queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)}`;
} }
if (genObject === null || genObject === void 0 ? void 0 : genObject.order) if ((genObject === null || genObject === void 0 ? void 0 : genObject.order) && !count)
queryString += ` ORDER BY ${genObject.join queryString += ` ORDER BY ${genObject.join
? `${finalDbName}${tableName}.${String(genObject.order.field)}` ? `${finalDbName}${tableName}.${String(genObject.order.field)}`
: String(genObject.order.field)} ${genObject.order.strategy}`; : String(genObject.order.field)} ${genObject.order.strategy}`;
if (genObject === null || genObject === void 0 ? void 0 : genObject.limit) if ((genObject === null || genObject === void 0 ? void 0 : genObject.limit) && !count)
queryString += ` LIMIT ${genObject.limit}`; queryString += ` LIMIT ${genObject.limit}`;
if (genObject === null || genObject === void 0 ? void 0 : genObject.offset) if ((genObject === null || genObject === void 0 ? void 0 : genObject.offset) && !count)
queryString += ` OFFSET ${genObject.offset}`; queryString += ` OFFSET ${genObject.offset}`;
return { return {
string: queryString, string: queryString,

View File

@ -58,22 +58,23 @@ function createDbFromSchema(_a) {
}); });
for (let tb = 0; tb < allTables.length; tb++) { for (let tb = 0; tb < allTables.length; tb++) {
const { TABLE_NAME } = allTables[tb]; const { TABLE_NAME } = allTables[tb];
const targetTableSchema = tables.find((_table) => _table.tableName === TABLE_NAME);
/** /**
* @description Check if TABLE_NAME is part of the tables contained * @description Check if TABLE_NAME is part of the tables contained
* in the user schema JSON. If it's not, the table is either deleted * in the user schema JSON. If it's not, the table is either deleted
* or the table name has been recently changed * or the table name has been recently changed
*/ */
if (!tables.filter((_table) => _table.tableName === TABLE_NAME)[0]) { if (!targetTableSchema) {
const oldTableFilteredArray = tables.filter((_table) => _table.tableNameOld && const oldTable = tables.find((_table) => _table.tableNameOld &&
_table.tableNameOld === TABLE_NAME); _table.tableNameOld === TABLE_NAME);
/** /**
* @description Check if this table has been recently renamed. Rename * @description Check if this table has been recently renamed. Rename
* table id true. Drop table if false * table id true. Drop table if false
*/ */
if (oldTableFilteredArray && oldTableFilteredArray[0]) { if (oldTable) {
console.log("Renaming Table"); console.log("Renaming Table");
yield (0, varDatabaseDbHandler_1.default)({ yield (0, varDatabaseDbHandler_1.default)({
queryString: `RENAME TABLE \`${dbFullName}\`.\`${oldTableFilteredArray[0].tableNameOld}\` TO \`${oldTableFilteredArray[0].tableName}\``, queryString: `RENAME TABLE \`${dbFullName}\`.\`${oldTable.tableNameOld}\` TO \`${oldTable.tableName}\``,
}); });
} }
else { else {

View File

@ -1,5 +1,5 @@
import type { RequestOptions } from "https"; import type { RequestOptions } from "https";
import { DSQL_DATASQUIREL_PROCESS_QUEUE } from "./dsql"; import { DSQL_DATASQUIREL_PROCESS_QUEUE, DSQL_DATASQUIREL_USER_DATABASES } from "./dsql";
import { Editor } from "tinymce"; import { Editor } from "tinymce";
export type DSQL_DatabaseFullName = string; export type DSQL_DatabaseFullName = string;
export interface DSQL_DatabaseSchemaType { export interface DSQL_DatabaseSchemaType {
@ -1364,6 +1364,7 @@ export type DsqlCrudParam<T extends {
query?: DsqlCrudQueryObject<T>; query?: DsqlCrudQueryObject<T>;
sanitize?: (data?: T) => T; sanitize?: (data?: T) => T;
debug?: boolean; debug?: boolean;
count?: boolean;
}; };
export type ErrorCallback = (title: string, error: Error, data?: any) => void; export type ErrorCallback = (title: string, error: Error, data?: any) => void;
export interface MariaDBUser { export interface MariaDBUser {
@ -1419,6 +1420,7 @@ export type PagePropsType = {
user?: UserType | null; user?: UserType | null;
pageUrl?: string | null; pageUrl?: string | null;
query?: any; query?: any;
databases?: DSQL_DATASQUIREL_USER_DATABASES[] | null;
}; };
export type APIResponseObject<T extends any = any> = { export type APIResponseObject<T extends any = any> = {
success: boolean; success: boolean;

View File

@ -25,8 +25,11 @@ export default function grabDirNames(param?: Param): {
userPrivateDbImportZipFilePath: string | undefined; userPrivateDbImportZipFilePath: string | undefined;
dbNginxLoadBalancerConfigFile: string; dbNginxLoadBalancerConfigFile: string;
dockerComposeFile: string; dockerComposeFile: string;
dockerComposeFileAlt: string;
testDockerComposeFile: string; testDockerComposeFile: string;
testDockerComposeFileAlt: string;
extraDockerComposeFile: string; extraDockerComposeFile: string;
extraDockerComposeFileAlt: string;
siteSetupFile: string; siteSetupFile: string;
envFile: string; envFile: string;
testEnvFile: string; testEnvFile: string;

View File

@ -52,9 +52,12 @@ function grabDirNames(param) {
? path_1.default.join(userPrivateSQLExportsDir, userPrivateDbImportZipFileName) ? path_1.default.join(userPrivateSQLExportsDir, userPrivateDbImportZipFileName)
: undefined; : undefined;
const dbNginxLoadBalancerConfigFile = path_1.default.join(appDir, "docker/services/mariadb/load-balancer/config/template/nginx.conf"); const dbNginxLoadBalancerConfigFile = path_1.default.join(appDir, "docker/services/mariadb/load-balancer/config/template/nginx.conf");
const dockerComposeFile = path_1.default.join(appDir, "docker-compose.yml"); let dockerComposeFile = path_1.default.join(appDir, "docker-compose.yml");
let dockerComposeFileAlt = path_1.default.join(appDir, "docker-compose.yaml");
const testDockerComposeFile = path_1.default.join(appDir, "test.docker-compose.yml"); const testDockerComposeFile = path_1.default.join(appDir, "test.docker-compose.yml");
const testDockerComposeFileAlt = path_1.default.join(appDir, "test.docker-compose.yaml");
const extraDockerComposeFile = path_1.default.join(appDir, "extra.docker-compose.yml"); const extraDockerComposeFile = path_1.default.join(appDir, "extra.docker-compose.yml");
const extraDockerComposeFileAlt = path_1.default.join(appDir, "extra.docker-compose.yaml");
const siteSetupFile = path_1.default.join(appDir, "site-setup.json"); const siteSetupFile = path_1.default.join(appDir, "site-setup.json");
const envFile = path_1.default.join(appDir, ".env"); const envFile = path_1.default.join(appDir, ".env");
const testEnvFile = path_1.default.join(appDir, "test.env"); const testEnvFile = path_1.default.join(appDir, "test.env");
@ -79,8 +82,11 @@ function grabDirNames(param) {
userPrivateDbImportZipFilePath, userPrivateDbImportZipFilePath,
dbNginxLoadBalancerConfigFile, dbNginxLoadBalancerConfigFile,
dockerComposeFile, dockerComposeFile,
dockerComposeFileAlt,
testDockerComposeFile, testDockerComposeFile,
testDockerComposeFileAlt,
extraDockerComposeFile, extraDockerComposeFile,
extraDockerComposeFileAlt,
siteSetupFile, siteSetupFile,
envFile, envFile,
testEnvFile, testEnvFile,

View File

@ -4,6 +4,7 @@ export default function dsqlCrud<T extends {
[key: string]: any; [key: string]: any;
} = { } = {
[key: string]: any; [key: string]: any;
}>({ action, data, table, targetValue, query, sanitize, debug, targetField, targetId, }: DsqlCrudParam<T>): Promise<(PostReturn & { }>({ action, data, table, targetValue, query, sanitize, debug, targetField, targetId, count, }: DsqlCrudParam<T>): Promise<(PostReturn & {
queryObject?: ReturnType<Awaited<typeof sqlGenerator>>; queryObject?: ReturnType<Awaited<typeof sqlGenerator>>;
count?: number;
}) | null>; }) | null>;

View File

@ -13,11 +13,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.default = dsqlCrud; exports.default = dsqlCrud;
const get_1 = __importDefault(require("../../actions/get"));
const post_1 = __importDefault(require("../../actions/post")); const post_1 = __importDefault(require("../../actions/post"));
const sql_generator_1 = __importDefault(require("../../functions/dsql/sql/sql-generator")); const sql_generator_1 = __importDefault(require("../../functions/dsql/sql/sql-generator"));
const conn_db_handler_1 = __importDefault(require("../db/conn-db-handler"));
function dsqlCrud(_a) { function dsqlCrud(_a) {
return __awaiter(this, arguments, void 0, function* ({ action, data, table, targetValue, query, sanitize, debug, targetField, targetId, }) { return __awaiter(this, arguments, void 0, function* ({ action, data, table, targetValue, query, sanitize, debug, targetField, targetId, count, }) {
var _b, _c;
const finalData = sanitize ? sanitize(data) : data; const finalData = sanitize ? sanitize(data) : data;
let queryObject; let queryObject;
switch (action) { switch (action) {
@ -26,13 +27,35 @@ function dsqlCrud(_a) {
tableName: table, tableName: table,
genObject: query, genObject: query,
}); });
const GET_RES = yield (0, get_1.default)({ const DB_CONN = global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
query: (queryObject === null || queryObject === void 0 ? void 0 : queryObject.string) || "", const connQueries = [
queryValues: (queryObject === null || queryObject === void 0 ? void 0 : queryObject.values) || [], {
debug, query: queryObject === null || queryObject === void 0 ? void 0 : queryObject.string,
forceLocal: true, values: (queryObject === null || queryObject === void 0 ? void 0 : queryObject.values) || [],
}); },
return Object.assign(Object.assign({}, GET_RES), { queryObject }); ];
if (count) {
const countQueryObject = (0, sql_generator_1.default)({
tableName: table,
genObject: query,
count: true,
});
connQueries.push({
query: countQueryObject.string,
values: countQueryObject.values,
});
}
const res = yield (0, conn_db_handler_1.default)(DB_CONN, connQueries);
const isSuccess = Array.isArray(res) && Array.isArray(res[0]);
return {
success: isSuccess,
payload: isSuccess ? res[0] : null,
error: isSuccess ? undefined : res === null || res === void 0 ? void 0 : res.error,
queryObject,
count: isSuccess && ((_c = (_b = res[1]) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c["COUNT(*)"])
? res[1][0]["COUNT(*)"]
: undefined,
};
case "insert": case "insert":
return yield (0, post_1.default)({ return yield (0, post_1.default)({
query: { query: {

View File

@ -1,5 +1,5 @@
import { ServerlessMysql } from "serverless-mysql"; import { ServerlessMysql } from "serverless-mysql";
type QueryObject = { export type ConnDBHandlerQueryObject = {
query: string; query: string;
values?: (string | number | undefined)[]; values?: (string | number | undefined)[];
}; };
@ -17,11 +17,11 @@ export default function connDbHandler<ReturnType = any>(
*/ */
conn?: ServerlessMysql, conn?: ServerlessMysql,
/** /**
* String Or `QueryObject` Array * String Or `ConnDBHandlerQueryObject` Array
*/ */
query?: QueryObject["query"] | QueryObject[], query?: ConnDBHandlerQueryObject["query"] | ConnDBHandlerQueryObject[],
/** /**
* Array of Values to Sanitize and Inject * Array of Values to Sanitize and Inject
*/ */
values?: QueryObject["values"], debug?: boolean): Promise<Return<ReturnType>>; values?: ConnDBHandlerQueryObject["values"], debug?: boolean): Promise<Return<ReturnType>>;
export {}; export {};

View File

@ -25,7 +25,7 @@ function connDbHandler(
*/ */
conn, conn,
/** /**
* String Or `QueryObject` Array * String Or `ConnDBHandlerQueryObject` Array
*/ */
query, query,
/** /**

View File

@ -8,6 +8,7 @@ type Param<T extends { [key: string]: any } = { [key: string]: any }> = {
genObject?: ServerQueryParam<T>; genObject?: ServerQueryParam<T>;
tableName: string; tableName: string;
dbFullName?: string; dbFullName?: string;
count?: boolean;
}; };
type Return = { type Return = {
@ -21,7 +22,7 @@ type Return = {
*/ */
export default function sqlGenerator< export default function sqlGenerator<
T extends { [key: string]: any } = { [key: string]: any } T extends { [key: string]: any } = { [key: string]: any }
>({ tableName, genObject, dbFullName }: Param<T>): Return { >({ tableName, genObject, dbFullName, count }: Param<T>): Return {
const finalQuery = genObject?.query ? genObject.query : undefined; const finalQuery = genObject?.query ? genObject.query : undefined;
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined; const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
@ -162,7 +163,10 @@ export default function sqlGenerator<
let queryString = (() => { let queryString = (() => {
let str = "SELECT"; let str = "SELECT";
if (genObject?.selectFields?.[0]) {
if (count) {
str += ` COUNT(*)`;
} else if (genObject?.selectFields?.[0]) {
if (genObject.join) { if (genObject.join) {
str += ` ${genObject.selectFields str += ` ${genObject.selectFields
?.map((fld) => `${finalDbName}${tableName}.${fld}`) ?.map((fld) => `${finalDbName}${tableName}.${fld}`)
@ -178,7 +182,7 @@ export default function sqlGenerator<
} }
} }
if (genObject?.join) { if (genObject?.join && !count) {
const existingJoinTableNames: string[] = [tableName]; const existingJoinTableNames: string[] = [tableName];
str += str +=
@ -264,15 +268,16 @@ export default function sqlGenerator<
queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)}`; queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)}`;
} }
if (genObject?.order) if (genObject?.order && !count)
queryString += ` ORDER BY ${ queryString += ` ORDER BY ${
genObject.join genObject.join
? `${finalDbName}${tableName}.${String(genObject.order.field)}` ? `${finalDbName}${tableName}.${String(genObject.order.field)}`
: String(genObject.order.field) : String(genObject.order.field)
} ${genObject.order.strategy}`; } ${genObject.order.strategy}`;
if (genObject?.limit) queryString += ` LIMIT ${genObject.limit}`; if (genObject?.limit && !count) queryString += ` LIMIT ${genObject.limit}`;
if (genObject?.offset) queryString += ` OFFSET ${genObject.offset}`; if (genObject?.offset && !count)
queryString += ` OFFSET ${genObject.offset}`;
return { return {
string: queryString, string: queryString,

View File

@ -75,15 +75,17 @@ export default async function createDbFromSchema({
for (let tb = 0; tb < allTables.length; tb++) { for (let tb = 0; tb < allTables.length; tb++) {
const { TABLE_NAME } = allTables[tb]; const { TABLE_NAME } = allTables[tb];
const targetTableSchema = tables.find(
(_table) => _table.tableName === TABLE_NAME
);
/** /**
* @description Check if TABLE_NAME is part of the tables contained * @description Check if TABLE_NAME is part of the tables contained
* in the user schema JSON. If it's not, the table is either deleted * in the user schema JSON. If it's not, the table is either deleted
* or the table name has been recently changed * or the table name has been recently changed
*/ */
if ( if (!targetTableSchema) {
!tables.filter((_table) => _table.tableName === TABLE_NAME)[0] const oldTable = tables.find(
) {
const oldTableFilteredArray = tables.filter(
(_table) => (_table) =>
_table.tableNameOld && _table.tableNameOld &&
_table.tableNameOld === TABLE_NAME _table.tableNameOld === TABLE_NAME
@ -93,10 +95,10 @@ export default async function createDbFromSchema({
* @description Check if this table has been recently renamed. Rename * @description Check if this table has been recently renamed. Rename
* table id true. Drop table if false * table id true. Drop table if false
*/ */
if (oldTableFilteredArray && oldTableFilteredArray[0]) { if (oldTable) {
console.log("Renaming Table"); console.log("Renaming Table");
await varDatabaseDbHandler({ await varDatabaseDbHandler({
queryString: `RENAME TABLE \`${dbFullName}\`.\`${oldTableFilteredArray[0].tableNameOld}\` TO \`${oldTableFilteredArray[0].tableName}\``, queryString: `RENAME TABLE \`${dbFullName}\`.\`${oldTable.tableNameOld}\` TO \`${oldTable.tableName}\``,
}); });
} else { } else {
console.log(`Dropping Table from ${dbFullName}`); console.log(`Dropping Table from ${dbFullName}`);

View File

@ -1,5 +1,8 @@
import type { RequestOptions } from "https"; import type { RequestOptions } from "https";
import { DSQL_DATASQUIREL_PROCESS_QUEUE } from "./dsql"; import {
DSQL_DATASQUIREL_PROCESS_QUEUE,
DSQL_DATASQUIREL_USER_DATABASES,
} from "./dsql";
import { Editor } from "tinymce"; import { Editor } from "tinymce";
export type DSQL_DatabaseFullName = string; export type DSQL_DatabaseFullName = string;
@ -1542,6 +1545,7 @@ export type DsqlCrudParam<
query?: DsqlCrudQueryObject<T>; query?: DsqlCrudQueryObject<T>;
sanitize?: (data?: T) => T; sanitize?: (data?: T) => T;
debug?: boolean; debug?: boolean;
count?: boolean;
}; };
export type ErrorCallback = (title: string, error: Error, data?: any) => void; export type ErrorCallback = (title: string, error: Error, data?: any) => void;
@ -1600,6 +1604,7 @@ export type PagePropsType = {
user?: UserType | null; user?: UserType | null;
pageUrl?: string | null; pageUrl?: string | null;
query?: any; query?: any;
databases?: DSQL_DATASQUIREL_USER_DATABASES[] | null;
}; };
export type APIResponseObject<T extends any = any> = { export type APIResponseObject<T extends any = any> = {

View File

@ -71,12 +71,21 @@ export default function grabDirNames(param?: Param) {
"docker/services/mariadb/load-balancer/config/template/nginx.conf" "docker/services/mariadb/load-balancer/config/template/nginx.conf"
); );
const dockerComposeFile = path.join(appDir, "docker-compose.yml"); let dockerComposeFile = path.join(appDir, "docker-compose.yml");
let dockerComposeFileAlt = path.join(appDir, "docker-compose.yaml");
const testDockerComposeFile = path.join(appDir, "test.docker-compose.yml"); const testDockerComposeFile = path.join(appDir, "test.docker-compose.yml");
const testDockerComposeFileAlt = path.join(
appDir,
"test.docker-compose.yaml"
);
const extraDockerComposeFile = path.join( const extraDockerComposeFile = path.join(
appDir, appDir,
"extra.docker-compose.yml" "extra.docker-compose.yml"
); );
const extraDockerComposeFileAlt = path.join(
appDir,
"extra.docker-compose.yaml"
);
const siteSetupFile = path.join(appDir, "site-setup.json"); const siteSetupFile = path.join(appDir, "site-setup.json");
@ -104,8 +113,11 @@ export default function grabDirNames(param?: Param) {
userPrivateDbImportZipFilePath, userPrivateDbImportZipFilePath,
dbNginxLoadBalancerConfigFile, dbNginxLoadBalancerConfigFile,
dockerComposeFile, dockerComposeFile,
dockerComposeFileAlt,
testDockerComposeFile, testDockerComposeFile,
testDockerComposeFileAlt,
extraDockerComposeFile, extraDockerComposeFile,
extraDockerComposeFileAlt,
siteSetupFile, siteSetupFile,
envFile, envFile,
testEnvFile, testEnvFile,

View File

@ -2,6 +2,7 @@ import get from "../../actions/get";
import post from "../../actions/post"; import post from "../../actions/post";
import sqlGenerator from "../../functions/dsql/sql/sql-generator"; import sqlGenerator from "../../functions/dsql/sql/sql-generator";
import { DsqlCrudParam, PostReturn } from "../../types"; import { DsqlCrudParam, PostReturn } from "../../types";
import connDbHandler, { ConnDBHandlerQueryObject } from "../db/conn-db-handler";
export default async function dsqlCrud< export default async function dsqlCrud<
T extends { [key: string]: any } = { [key: string]: any } T extends { [key: string]: any } = { [key: string]: any }
@ -15,9 +16,11 @@ export default async function dsqlCrud<
debug, debug,
targetField, targetField,
targetId, targetId,
count,
}: DsqlCrudParam<T>): Promise< }: DsqlCrudParam<T>): Promise<
| (PostReturn & { | (PostReturn & {
queryObject?: ReturnType<Awaited<typeof sqlGenerator>>; queryObject?: ReturnType<Awaited<typeof sqlGenerator>>;
count?: number;
}) })
| null | null
> { > {
@ -31,14 +34,43 @@ export default async function dsqlCrud<
genObject: query, genObject: query,
}); });
const GET_RES = await get({ const DB_CONN =
query: queryObject?.string || "", global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
queryValues: queryObject?.values || [],
debug,
forceLocal: true,
});
return { ...GET_RES, queryObject }; const connQueries: ConnDBHandlerQueryObject[] = [
{
query: queryObject?.string,
values: queryObject?.values || [],
},
];
if (count) {
const countQueryObject = sqlGenerator({
tableName: table,
genObject: query,
count: true,
});
connQueries.push({
query: countQueryObject.string,
values: countQueryObject.values,
});
}
const res = await connDbHandler(DB_CONN, connQueries);
const isSuccess = Array.isArray(res) && Array.isArray(res[0]);
return {
success: isSuccess,
payload: isSuccess ? res[0] : null,
error: isSuccess ? undefined : res?.error,
queryObject,
count:
isSuccess && res[1]?.[0]?.["COUNT(*)"]
? res[1][0]["COUNT(*)"]
: undefined,
};
case "insert": case "insert":
return await post({ return await post({

View File

@ -1,7 +1,7 @@
import { ServerlessMysql } from "serverless-mysql"; import { ServerlessMysql } from "serverless-mysql";
import debugLog from "../logging/debug-log"; import debugLog from "../logging/debug-log";
type QueryObject = { export type ConnDBHandlerQueryObject = {
query: string; query: string;
values?: (string | number | undefined)[]; values?: (string | number | undefined)[];
}; };
@ -19,13 +19,13 @@ export default async function connDbHandler<ReturnType = any>(
*/ */
conn?: ServerlessMysql, conn?: ServerlessMysql,
/** /**
* String Or `QueryObject` Array * String Or `ConnDBHandlerQueryObject` Array
*/ */
query?: QueryObject["query"] | QueryObject[], query?: ConnDBHandlerQueryObject["query"] | ConnDBHandlerQueryObject[],
/** /**
* Array of Values to Sanitize and Inject * Array of Values to Sanitize and Inject
*/ */
values?: QueryObject["values"], values?: ConnDBHandlerQueryObject["values"],
debug?: boolean debug?: boolean
): Promise<Return<ReturnType>> { ): Promise<Return<ReturnType>> {
try { try {

View File

@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/datasquirel", "name": "@moduletrace/datasquirel",
"version": "4.6.6", "version": "4.6.7",
"description": "Cloud-based SQL data management tool", "description": "Cloud-based SQL data management tool",
"main": "dist/index.js", "main": "dist/index.js",
"bin": { "bin": {