From cd96c1181716317fee24fb64f5aea97b0c7db3ff Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Sun, 1 Jun 2025 12:14:49 +0100 Subject: [PATCH] Updates --- dist/package-shared/types/index.d.ts | 5 +++ .../utils/data-fetching/crud.d.ts | 2 +- .../utils/data-fetching/crud.js | 32 ++++++++++++----- package-shared/types/index.ts | 5 +++ package-shared/utils/data-fetching/crud.ts | 36 +++++++++++++------ package.json | 2 +- 6 files changed, 60 insertions(+), 22 deletions(-) diff --git a/dist/package-shared/types/index.d.ts b/dist/package-shared/types/index.d.ts index 76dd877..3fc8111 100644 --- a/dist/package-shared/types/index.d.ts +++ b/dist/package-shared/types/index.d.ts @@ -1365,6 +1365,7 @@ export type DsqlCrudParam T; debug?: boolean; count?: boolean; + countOnly?: boolean; }; export type ErrorCallback = (title: string, error: Error, data?: any) => void; export interface MariaDBUser { @@ -1421,6 +1422,10 @@ export type PagePropsType = { pageUrl?: string | null; query?: any; databases?: DSQL_DATASQUIREL_USER_DATABASES[] | null; + dbCount?: number | null; + tableCount?: number | null; + mediaCount?: number | null; + apiKeysCount?: number | null; }; export type APIResponseObject = { success: boolean; diff --git a/dist/package-shared/utils/data-fetching/crud.d.ts b/dist/package-shared/utils/data-fetching/crud.d.ts index df9ec4b..b302ed4 100644 --- a/dist/package-shared/utils/data-fetching/crud.d.ts +++ b/dist/package-shared/utils/data-fetching/crud.d.ts @@ -4,7 +4,7 @@ export default function dsqlCrud({ action, data, table, targetValue, query, sanitize, debug, targetField, targetId, count, }: DsqlCrudParam): Promise<(PostReturn & { +}>({ action, data, table, targetValue, query, sanitize, debug, targetField, targetId, count, countOnly, }: DsqlCrudParam): Promise<(PostReturn & { queryObject?: ReturnType>; count?: number; }) | null>; diff --git a/dist/package-shared/utils/data-fetching/crud.js b/dist/package-shared/utils/data-fetching/crud.js index 5252bd0..c30c95e 100644 --- a/dist/package-shared/utils/data-fetching/crud.js +++ b/dist/package-shared/utils/data-fetching/crud.js @@ -17,8 +17,8 @@ const post_1 = __importDefault(require("../../actions/post")); 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) { - return __awaiter(this, arguments, void 0, function* ({ action, data, table, targetValue, query, sanitize, debug, targetField, targetId, count, }) { - var _b, _c; + return __awaiter(this, arguments, void 0, function* ({ action, data, table, targetValue, query, sanitize, debug, targetField, targetId, count, countOnly, }) { + var _b, _c, _d, _e; const finalData = sanitize ? sanitize(data) : data; let queryObject; switch (action) { @@ -28,32 +28,46 @@ function dsqlCrud(_a) { genObject: query, }); const DB_CONN = global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN; - const connQueries = [ + let connQueries = [ { query: queryObject === null || queryObject === void 0 ? void 0 : queryObject.string, values: (queryObject === null || queryObject === void 0 ? void 0 : queryObject.values) || [], }, ]; - if (count) { - const countQueryObject = (0, sql_generator_1.default)({ + const countQueryObject = count || countOnly + ? (0, sql_generator_1.default)({ tableName: table, genObject: query, count: true, - }); + }) + : undefined; + if (count && countQueryObject) { connQueries.push({ query: countQueryObject.string, values: countQueryObject.values, }); } + else if (countOnly && countQueryObject) { + connQueries = [ + { + 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, + payload: isSuccess ? (countOnly ? null : 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(*)"] + 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(*)"] + : ((_e = (_d = res[0]) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e["COUNT(*)"]) + ? res[0][0]["COUNT(*)"] + : undefined : undefined, }; case "insert": diff --git a/package-shared/types/index.ts b/package-shared/types/index.ts index 95cc276..3ba04dc 100644 --- a/package-shared/types/index.ts +++ b/package-shared/types/index.ts @@ -1546,6 +1546,7 @@ export type DsqlCrudParam< sanitize?: (data?: T) => T; debug?: boolean; count?: boolean; + countOnly?: boolean; }; export type ErrorCallback = (title: string, error: Error, data?: any) => void; @@ -1605,6 +1606,10 @@ export type PagePropsType = { pageUrl?: string | null; query?: any; databases?: DSQL_DATASQUIREL_USER_DATABASES[] | null; + dbCount?: number | null; + tableCount?: number | null; + mediaCount?: number | null; + apiKeysCount?: number | null; }; export type APIResponseObject = { diff --git a/package-shared/utils/data-fetching/crud.ts b/package-shared/utils/data-fetching/crud.ts index b020eee..768315a 100644 --- a/package-shared/utils/data-fetching/crud.ts +++ b/package-shared/utils/data-fetching/crud.ts @@ -17,6 +17,7 @@ export default async function dsqlCrud< targetField, targetId, count, + countOnly, }: DsqlCrudParam): Promise< | (PostReturn & { queryObject?: ReturnType>; @@ -37,24 +38,34 @@ export default async function dsqlCrud< const DB_CONN = global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN; - const connQueries: ConnDBHandlerQueryObject[] = [ + let connQueries: ConnDBHandlerQueryObject[] = [ { query: queryObject?.string, values: queryObject?.values || [], }, ]; - if (count) { - const countQueryObject = sqlGenerator({ - tableName: table, - genObject: query, - count: true, - }); + const countQueryObject = + count || countOnly + ? sqlGenerator({ + tableName: table, + genObject: query, + count: true, + }) + : undefined; + if (count && countQueryObject) { connQueries.push({ query: countQueryObject.string, values: countQueryObject.values, }); + } else if (countOnly && countQueryObject) { + connQueries = [ + { + query: countQueryObject.string, + values: countQueryObject.values, + }, + ]; } const res = await connDbHandler(DB_CONN, connQueries); @@ -63,13 +74,16 @@ export default async function dsqlCrud< return { success: isSuccess, - payload: isSuccess ? res[0] : null, + payload: isSuccess ? (countOnly ? null : res[0]) : null, error: isSuccess ? undefined : res?.error, queryObject, - count: - isSuccess && res[1]?.[0]?.["COUNT(*)"] + count: isSuccess + ? res[1]?.[0]?.["COUNT(*)"] ? res[1][0]["COUNT(*)"] - : undefined, + : res[0]?.[0]?.["COUNT(*)"] + ? res[0][0]["COUNT(*)"] + : undefined + : undefined, }; case "insert": diff --git a/package.json b/package.json index 6acdc45..c957674 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/datasquirel", - "version": "4.6.7", + "version": "4.6.8", "description": "Cloud-based SQL data management tool", "main": "dist/index.js", "bin": {