diff --git a/dist/package-shared/utils/data-fetching/crud-get.d.ts b/dist/package-shared/utils/data-fetching/crud-get.d.ts index 8257d57..05d29b9 100644 --- a/dist/package-shared/utils/data-fetching/crud-get.d.ts +++ b/dist/package-shared/utils/data-fetching/crud-get.d.ts @@ -3,4 +3,4 @@ export default function ({ table, query, count, countOnly, dbFullName, tableSchema, dbConfig, }: Omit, "action" | "data" | "sanitize">): Promise; +}, K extends string = string>({ table, query, count, countOnly, dbFullName, tableSchema, dbConfig, targetId, targetField, targetValue, }: Omit, "action" | "data" | "sanitize">): Promise; diff --git a/dist/package-shared/utils/data-fetching/crud-get.js b/dist/package-shared/utils/data-fetching/crud-get.js index 471175d..f940d59 100644 --- a/dist/package-shared/utils/data-fetching/crud-get.js +++ b/dist/package-shared/utils/data-fetching/crud-get.js @@ -18,13 +18,34 @@ const sql_generator_1 = __importDefault(require("../../functions/dsql/sql/sql-ge const conn_db_handler_1 = __importDefault(require("../db/conn-db-handler")); const check_array_depth_1 = __importDefault(require("../check-array-depth")); const parseDbResults_1 = __importDefault(require("../../functions/backend/parseDbResults")); +const lodash_1 = __importDefault(require("lodash")); function default_1(_a) { - return __awaiter(this, arguments, void 0, function* ({ table, query, count, countOnly, dbFullName, tableSchema, dbConfig, }) { + return __awaiter(this, arguments, void 0, function* ({ table, query, count, countOnly, dbFullName, tableSchema, dbConfig, targetId, targetField, targetValue, }) { var _b, _c, _d, _e; let queryObject; + let crudQueryObj = lodash_1.default.cloneDeep(query); + if (targetId) { + crudQueryObj = lodash_1.default.merge(crudQueryObj || {}, { + query: { + id: { + value: String(targetId), + }, + }, + }); + } + if (targetField && + (typeof targetValue == "string" || typeof targetValue == "number")) { + crudQueryObj = lodash_1.default.merge(crudQueryObj || {}, { + query: { + [targetField]: { + value: String(targetValue), + }, + }, + }); + } queryObject = (0, sql_generator_1.default)({ tableName: table, - genObject: query, + genObject: crudQueryObj, dbFullName, }); let connQueries = [ @@ -36,7 +57,7 @@ function default_1(_a) { const countQueryObject = count || countOnly ? (0, sql_generator_1.default)({ tableName: table, - genObject: query, + genObject: crudQueryObj, count: true, dbFullName, }) diff --git a/package-shared/utils/data-fetching/crud-get.ts b/package-shared/utils/data-fetching/crud-get.ts index f13c203..6d45986 100644 --- a/package-shared/utils/data-fetching/crud-get.ts +++ b/package-shared/utils/data-fetching/crud-get.ts @@ -1,9 +1,15 @@ import { format } from "sql-formatter"; import sqlGenerator from "../../functions/dsql/sql/sql-generator"; -import { APIResponseObject, DsqlCrudParam } from "../../types"; +import { + APIResponseObject, + DsqlCrudParam, + DsqlCrudQueryObject, + ServerQueryQueryObject, +} from "../../types"; import connDbHandler, { ConnDBHandlerQueryObject } from "../db/conn-db-handler"; import checkArrayDepth from "../check-array-depth"; import parseDbResults from "../../functions/backend/parseDbResults"; +import _ from "lodash"; export default async function < T extends { [key: string]: any } = { [key: string]: any }, @@ -16,15 +22,49 @@ export default async function < dbFullName, tableSchema, dbConfig, + targetId, + targetField, + targetValue, }: Omit< DsqlCrudParam, "action" | "data" | "sanitize" >): Promise { let queryObject: ReturnType> | undefined; + let crudQueryObj = _.cloneDeep(query); + + if (targetId) { + crudQueryObj = _.merge< + DsqlCrudQueryObject, + DsqlCrudQueryObject + >(crudQueryObj || {}, { + query: { + id: { + value: String(targetId), + }, + } as ServerQueryQueryObject, + }); + } + + if ( + targetField && + (typeof targetValue == "string" || typeof targetValue == "number") + ) { + crudQueryObj = _.merge< + DsqlCrudQueryObject, + DsqlCrudQueryObject + >(crudQueryObj || {}, { + query: { + [targetField]: { + value: String(targetValue), + }, + } as ServerQueryQueryObject, + }); + } + queryObject = sqlGenerator({ tableName: table, - genObject: query, + genObject: crudQueryObj, dbFullName, }); @@ -39,7 +79,7 @@ export default async function < count || countOnly ? sqlGenerator({ tableName: table, - genObject: query, + genObject: crudQueryObj, count: true, dbFullName, }) diff --git a/package.json b/package.json index cbff4de..0217d9b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/datasquirel", - "version": "5.5.3", + "version": "5.5.4", "description": "Cloud-based SQL data management tool", "main": "dist/index.js", "bin": {