UNPKG

@minimaltech/node-infra

Version:

Minimal Technology NodeJS Infrastructure - Loopback 4 Framework

143 lines 6.17 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildBatchUpdateQuery = exports.getValue = exports.getTableDefinition = void 0; const helpers_1 = require("../helpers"); const get_1 = __importDefault(require("lodash/get")); const pick_1 = __importDefault(require("lodash/pick")); const snakeCase_1 = __importDefault(require("lodash/snakeCase")); const error_utility_1 = require("./error.utility"); const getTableDefinition = (opts) => { const { model, alias } = opts; const tableAlias = alias !== null && alias !== void 0 ? alias : model.name.toLowerCase(); const tableNameWithAlias = `"${model.name}" ${tableAlias}`; const tableColumns = Object.entries(model.definition.properties) .map(([key, value]) => { var _a, _b, _c; const columnName = (_c = (_a = value === null || value === void 0 ? void 0 : value.name) !== null && _a !== void 0 ? _a : (_b = value === null || value === void 0 ? void 0 : value.postgresql) === null || _b === void 0 ? void 0 : _b.columnName) !== null && _c !== void 0 ? _c : key; return { [`${key}`]: `${tableAlias}.${columnName}`, }; }) .reduce((acc, cur) => { return Object.assign(Object.assign({}, acc), cur); }, {}); const tableColumnsNoAlias = Object.entries(model.definition.properties) .map(([key, value]) => { var _a, _b, _c; const columnName = (_c = (_a = value === null || value === void 0 ? void 0 : value.name) !== null && _a !== void 0 ? _a : (_b = value === null || value === void 0 ? void 0 : value.postgresql) === null || _b === void 0 ? void 0 : _b.columnName) !== null && _c !== void 0 ? _c : key; return { [`${key}`]: `${columnName}`, }; }) .reduce((acc, cur) => { return Object.assign(Object.assign({}, acc), cur); }, {}); return { table: { name: model.name, alias: tableAlias, nameWithQuotation: `"${model.name}"` }, tableWithAlias: tableNameWithAlias, columns: tableColumns, columnsNoAlias: tableColumnsNoAlias, }; }; exports.getTableDefinition = getTableDefinition; // -------------------------------------------------------------------------------- const getValue = (value) => { switch (typeof value) { case null: { return 'null'; } case 'number': { return value === null || value === void 0 ? void 0 : value.toString(); } case 'boolean': { return value; } case 'string': { if ((value === null || value === void 0 ? void 0 : value.toString().trim()) === '') { return "''"; } return `'${value === null || value === void 0 ? void 0 : value.toString().replace(/'/g, "''")}'`; } default: { return `'${value}'`; } } }; exports.getValue = getValue; // -------------------------------------------------------------------------------- const buildBatchUpdateQuery = (opts) => { const { data, tableName, keys, setKeys, whereKeys, whereRaws = [] } = opts; const withAlias = 't'; if (!data.length || !keys.length || !setKeys.length || !whereKeys.length) { helpers_1.applicationLogger.error('[batchUpdate] Missing required fields | Data: %d | Keys: %d | Set Keys: %d | Where Keys: %d', data.length, keys.length, setKeys.length, whereKeys.length); throw (0, error_utility_1.getError)({ message: '[batchUpdate] Missing required fields', }); } // ---------------------------------------- const formattedData = data.map(obj => (0, pick_1.default)(obj, ...keys)); const withValues = formattedData.map(obj => { const values = []; for (const k of Object.keys(obj).sort()) { const value = (0, get_1.default)(obj, k, null); switch (typeof value) { case 'object': { values.push((0, exports.getValue)(JSON.stringify(value))); break; } default: { values.push((0, exports.getValue)(value)); break; } } } return `(${values.toString()})`; }); const withKeys = keys.map(key => (0, snakeCase_1.default)(String(key))).sort(); // ---------------------------------------- const updateSets = []; for (const key of setKeys) { switch (typeof key) { case 'string': { updateSets.push(`"${(0, snakeCase_1.default)(key)}" = ${withAlias}."${(0, snakeCase_1.default)(key)}"`); break; } case 'object': { updateSets.push(`"${(0, snakeCase_1.default)(String(key.targetKey))}" = ${withAlias}."${(0, snakeCase_1.default)(String(key.sourceKey))}"::JSONB`); break; } default: { break; } } } // ---------------------------------------- const updateWheres = []; for (const key of whereKeys) { switch (typeof key) { case 'string': { updateWheres.push(`"${tableName}"."${(0, snakeCase_1.default)(key)}" = ${withAlias}."${(0, snakeCase_1.default)(key)}"`); break; } case 'object': { updateWheres.push(`${(0, snakeCase_1.default)(String(key.targetKey))} = ${withAlias}."${(0, snakeCase_1.default)(String(key.sourceKey))}"`); break; } default: { break; } } } // ---------------------------------------- return `WITH ${withAlias} (${withKeys}) AS (VALUES ${withValues}) UPDATE "${tableName}" SET ${updateSets.join(', ')} FROM ${withAlias} WHERE ${updateWheres.join(' AND ')} ${whereRaws.length ? `AND ${whereRaws.join(', AND')}` : ''};`; }; exports.buildBatchUpdateQuery = buildBatchUpdateQuery; //# sourceMappingURL=query.utility.js.map