This commit is contained in:
Benjamin Toby 2025-06-01 12:14:49 +01:00
parent 9b7c98cff6
commit cd96c11817
6 changed files with 60 additions and 22 deletions

View File

@ -1365,6 +1365,7 @@ export type DsqlCrudParam<T extends {
sanitize?: (data?: T) => 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<T extends any = any> = {
success: boolean;

View File

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

View File

@ -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":

View File

@ -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<T extends any = any> = {

View File

@ -17,6 +17,7 @@ export default async function dsqlCrud<
targetField,
targetId,
count,
countOnly,
}: DsqlCrudParam<T>): Promise<
| (PostReturn & {
queryObject?: ReturnType<Awaited<typeof sqlGenerator>>;
@ -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":

View File

@ -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": {