@qrvey/data-persistence 0.3.6-beta.2 → 0.3.6-bundle
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/services/crud.service.js +1 -1
- package/dist/cjs/services/crud.service.js.map +1 -1
- package/dist/cjs/services/crudFactory.service.js +5 -34
- package/dist/cjs/services/crudFactory.service.js.map +1 -1
- package/dist/cjs/services/cruds/index.js +19 -0
- package/dist/cjs/services/cruds/index.js.map +1 -0
- package/dist/esm/index.mjs +1703 -14
- package/dist/esm/index.mjs.map +1 -1
- package/package.json +3 -17
- package/dist/esm/chunk-6MOAJFFY.mjs +0 -112
- package/dist/esm/chunk-6MOAJFFY.mjs.map +0 -1
- package/dist/esm/chunk-DHIGNHXS.mjs +0 -58
- package/dist/esm/chunk-DHIGNHXS.mjs.map +0 -1
- package/dist/esm/dynamoDbCrud.service-EFYPBZKN.mjs +0 -818
- package/dist/esm/dynamoDbCrud.service-EFYPBZKN.mjs.map +0 -1
- package/dist/esm/postgreSqlCrud.service-GXH7ZQZY.mjs +0 -730
- package/dist/esm/postgreSqlCrud.service-GXH7ZQZY.mjs.map +0 -1
|
@@ -1,730 +0,0 @@
|
|
|
1
|
-
import { findIdColumnName, buildAggFunctionAlias, PersistenceErrorWrapper, getTableName } from './chunk-DHIGNHXS.mjs';
|
|
2
|
-
import { __spreadValues, __spreadProps, DEFAULT_PG_SCHEMA, POSTGRES_FILTER_OPERATOR_MAP, DYNAMO_DB_UPDATE_ACTIONS } from './chunk-6MOAJFFY.mjs';
|
|
3
|
-
import format from 'pg-format';
|
|
4
|
-
import { Client } from 'pg';
|
|
5
|
-
|
|
6
|
-
var ConnectionService = class {
|
|
7
|
-
get connectionString() {
|
|
8
|
-
const connectionString = process.env.MULTIPLATFORM_PG_CONNECTION_STRING || "";
|
|
9
|
-
if (!connectionString) {
|
|
10
|
-
throw new Error(
|
|
11
|
-
"MULTIPLATFORM_PG_CONNECTION_STRING environment variable must be configured"
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
return connectionString;
|
|
15
|
-
}
|
|
16
|
-
async getClient() {
|
|
17
|
-
const client = new Client({
|
|
18
|
-
connectionString: this.connectionString
|
|
19
|
-
});
|
|
20
|
-
await client.connect();
|
|
21
|
-
return client;
|
|
22
|
-
}
|
|
23
|
-
releaseClient(client) {
|
|
24
|
-
try {
|
|
25
|
-
client.end();
|
|
26
|
-
} catch (e) {
|
|
27
|
-
console.log("Error releasing client");
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
// src/services/cruds/postgresql/query.service.ts
|
|
33
|
-
var QueryService = class {
|
|
34
|
-
constructor(poolClient) {
|
|
35
|
-
this.poolClient = poolClient;
|
|
36
|
-
this.connectionService = new ConnectionService();
|
|
37
|
-
}
|
|
38
|
-
async runQuery(queryText, values) {
|
|
39
|
-
const client = await (this.poolClient ? this.poolClient : this.connectionService.getClient());
|
|
40
|
-
try {
|
|
41
|
-
const result = await client.query(queryText, values);
|
|
42
|
-
return result;
|
|
43
|
-
} catch (error) {
|
|
44
|
-
console.log("[Postgresql-Client] Query Execution Failed:", error);
|
|
45
|
-
throw error;
|
|
46
|
-
} finally {
|
|
47
|
-
if (!this.poolClient)
|
|
48
|
-
this.connectionService.releaseClient(client);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// src/services/cruds/postgresql/postgreSqlClient.service.ts
|
|
54
|
-
var PostgresqlClientService = class extends QueryService {
|
|
55
|
-
constructor(tableSchema, poolClient) {
|
|
56
|
-
super(poolClient);
|
|
57
|
-
this.isCompositeFilter = function(value) {
|
|
58
|
-
return "OR" in value || "AND" in value;
|
|
59
|
-
};
|
|
60
|
-
this.crudSchema = tableSchema;
|
|
61
|
-
}
|
|
62
|
-
get dbSchema() {
|
|
63
|
-
return this.crudSchema.schema || DEFAULT_PG_SCHEMA;
|
|
64
|
-
}
|
|
65
|
-
get tableName() {
|
|
66
|
-
return getTableName(this.crudSchema.table, "alias") || getTableName(this.crudSchema.table);
|
|
67
|
-
}
|
|
68
|
-
getWildcardValue(operator, value) {
|
|
69
|
-
if (operator === "CONTAINS" /* CONTAINS */ || operator === "NOT_CONTAINS" /* NOT_CONTAINS */) {
|
|
70
|
-
return "%" + value + "%";
|
|
71
|
-
} else if (operator === "STARTS_WITH" /* STARTS_WITH */) {
|
|
72
|
-
return value + "%";
|
|
73
|
-
} else {
|
|
74
|
-
return value;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
buildClause(operator, attribute, value) {
|
|
78
|
-
var _a;
|
|
79
|
-
const formattedValue = format.literal(value);
|
|
80
|
-
operator = operator ? operator.toUpperCase() : "EQUAL" /* EQUAL */;
|
|
81
|
-
const postgresOperator = POSTGRES_FILTER_OPERATOR_MAP[operator];
|
|
82
|
-
if (!postgresOperator)
|
|
83
|
-
throw new Error(`Unsupported filter operator: ${operator}`);
|
|
84
|
-
const filterProperty = format.ident(attribute);
|
|
85
|
-
const columnExists = !!this.crudSchema.columns[attribute];
|
|
86
|
-
const columnType = columnExists && ((_a = this.crudSchema.columns[attribute]) == null ? void 0 : _a.type);
|
|
87
|
-
const property = columnExists ? filterProperty : `("qvAttributes" ->> '${attribute}')`;
|
|
88
|
-
if (operator === "IN" /* IN */) {
|
|
89
|
-
const formattedValues = Array.isArray(value) ? value.map(format.literal) : [formattedValue];
|
|
90
|
-
return `${property} ${postgresOperator} (${formattedValues.join(
|
|
91
|
-
", "
|
|
92
|
-
)})`;
|
|
93
|
-
}
|
|
94
|
-
if (operator === "BETWEEN" /* BETWEEN */) {
|
|
95
|
-
return `${property} ${postgresOperator} ${value[0]} AND ${value[1]}`;
|
|
96
|
-
}
|
|
97
|
-
if (operator === "NOT_EQUAL" /* NOT_EQUAL */ && value !== null) {
|
|
98
|
-
return `(${property} ${postgresOperator} ${format.literal(
|
|
99
|
-
value
|
|
100
|
-
)} OR ${property} IS NULL)`;
|
|
101
|
-
}
|
|
102
|
-
if (operator === "NOT_EXIST" /* NOT_EXIST */ || operator === "EXIST" /* EXIST */) {
|
|
103
|
-
return `${property} ${postgresOperator}`;
|
|
104
|
-
}
|
|
105
|
-
if ((operator === "CONTAINS" /* CONTAINS */ || operator === "NOT_CONTAINS" /* NOT_CONTAINS */) && columnType === "array") {
|
|
106
|
-
const filterValue = typeof value === "number" ? value : `'${value}'`;
|
|
107
|
-
let filterString = `${filterValue} = ANY(${property})`;
|
|
108
|
-
if (operator === "NOT_CONTAINS" /* NOT_CONTAINS */) {
|
|
109
|
-
if (value === null) {
|
|
110
|
-
filterString = `(NOT (${filterString}))`;
|
|
111
|
-
} else {
|
|
112
|
-
filterString = `(NOT (${filterString}) or ${property} IS NULL)`;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return filterString;
|
|
116
|
-
}
|
|
117
|
-
const wildcardValue = this.getWildcardValue(operator, value);
|
|
118
|
-
return `${property} ${postgresOperator} ${format.literal(
|
|
119
|
-
wildcardValue
|
|
120
|
-
)}`;
|
|
121
|
-
}
|
|
122
|
-
buildFilterClause(filters, logicOperator) {
|
|
123
|
-
if (Array.isArray(filters)) {
|
|
124
|
-
const filterClauses = filters.map((filter) => {
|
|
125
|
-
return this.buildClause(
|
|
126
|
-
filter.operator,
|
|
127
|
-
filter.attribute,
|
|
128
|
-
filter.value
|
|
129
|
-
);
|
|
130
|
-
});
|
|
131
|
-
return filterClauses.join(
|
|
132
|
-
` ${logicOperator != null ? logicOperator : "AND" /* AND */} `
|
|
133
|
-
);
|
|
134
|
-
} else {
|
|
135
|
-
return this.buildQueryByClause(filters);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
buildQueryByClause(filters) {
|
|
139
|
-
let filterClauses = "";
|
|
140
|
-
let isFirstFilter = true;
|
|
141
|
-
for (const [key, value] of Object.entries(filters)) {
|
|
142
|
-
if (!isFirstFilter) {
|
|
143
|
-
filterClauses += key === "AND" ? " AND " : " OR ";
|
|
144
|
-
}
|
|
145
|
-
if (this.isCompositeFilter(value)) {
|
|
146
|
-
filterClauses += "(";
|
|
147
|
-
filterClauses += this.buildQueryByClause(
|
|
148
|
-
value
|
|
149
|
-
);
|
|
150
|
-
filterClauses += ")";
|
|
151
|
-
} else {
|
|
152
|
-
value.forEach((filter) => {
|
|
153
|
-
let clause = "";
|
|
154
|
-
if (this.isCompositeFilter(filter)) {
|
|
155
|
-
clause = `(${this.buildQueryByClause(
|
|
156
|
-
filter
|
|
157
|
-
)})`;
|
|
158
|
-
} else {
|
|
159
|
-
clause = this.buildClause(
|
|
160
|
-
filter.operator,
|
|
161
|
-
filter.attribute,
|
|
162
|
-
filter.value
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
filterClauses += `${clause} ${key} `;
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
isFirstFilter = false;
|
|
169
|
-
}
|
|
170
|
-
filterClauses = filterClauses.replace(/\s+(AND|OR)\s*$/, "");
|
|
171
|
-
return filterClauses;
|
|
172
|
-
}
|
|
173
|
-
formatOrderByItem(sort) {
|
|
174
|
-
return `${format.ident(sort.column)} ${sort.direction || "ASC" /* ASC */}`;
|
|
175
|
-
}
|
|
176
|
-
buildOrderByClause(querySorting) {
|
|
177
|
-
try {
|
|
178
|
-
return querySorting.map(this.formatOrderByItem).join(", ");
|
|
179
|
-
} catch (error) {
|
|
180
|
-
return "";
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
formatArray(array) {
|
|
184
|
-
const isNumberArray = typeof array[0] === "number";
|
|
185
|
-
if (isNumberArray) {
|
|
186
|
-
return `{${array.join(",")}}`;
|
|
187
|
-
} else {
|
|
188
|
-
return `{${array.map((val) => `"${val}"`).join(",")}}`;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
formatValue(value) {
|
|
192
|
-
if (Array.isArray(value)) {
|
|
193
|
-
if (!(value == null ? void 0 : value.length))
|
|
194
|
-
return "{}";
|
|
195
|
-
const isNumberArray = typeof value[0] === "number";
|
|
196
|
-
if (isNumberArray) {
|
|
197
|
-
return `{${value.join(",")}}`;
|
|
198
|
-
} else {
|
|
199
|
-
return `{${value.map((val) => `"${val}"`).join(",")}}`;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
return value;
|
|
203
|
-
}
|
|
204
|
-
async createCommand(data) {
|
|
205
|
-
const keys = Object.keys(data[0]);
|
|
206
|
-
const values = data.map(
|
|
207
|
-
(item) => keys.map((key) => this.formatValue(item[key]))
|
|
208
|
-
);
|
|
209
|
-
const query = format(
|
|
210
|
-
`INSERT INTO ${format.ident(this.dbSchema)}.${format.ident(
|
|
211
|
-
this.tableName
|
|
212
|
-
)} (%I) VALUES %L RETURNING *;`,
|
|
213
|
-
keys,
|
|
214
|
-
values
|
|
215
|
-
);
|
|
216
|
-
return this.runQuery(query);
|
|
217
|
-
}
|
|
218
|
-
addFiltersToQuery(query, filters) {
|
|
219
|
-
if (!filters)
|
|
220
|
-
return query;
|
|
221
|
-
const isValidArrayFilters = Array.isArray(filters) && (filters == null ? void 0 : filters.length);
|
|
222
|
-
const isValidCompositeFilters = this.isCompositeFilter(filters);
|
|
223
|
-
if (isValidArrayFilters || isValidCompositeFilters)
|
|
224
|
-
query += ` WHERE ${this.buildFilterClause(filters)}`;
|
|
225
|
-
return query;
|
|
226
|
-
}
|
|
227
|
-
addOrderByToQuery(query, orderBy) {
|
|
228
|
-
if (orderBy)
|
|
229
|
-
query += ` ORDER BY ${this.buildOrderByClause(orderBy)}`;
|
|
230
|
-
return query;
|
|
231
|
-
}
|
|
232
|
-
addPaginationToQuery(query, pagination) {
|
|
233
|
-
if (pagination) {
|
|
234
|
-
const { limit, from } = pagination;
|
|
235
|
-
if (limit)
|
|
236
|
-
query += ` LIMIT ${limit}`;
|
|
237
|
-
if (from)
|
|
238
|
-
query += ` OFFSET ${from}`;
|
|
239
|
-
}
|
|
240
|
-
return query;
|
|
241
|
-
}
|
|
242
|
-
getSelectClause(aggregateFunction, fields = []) {
|
|
243
|
-
if (aggregateFunction)
|
|
244
|
-
return `CAST(${aggregateFunction}(1) AS INTEGER) AS "${buildAggFunctionAlias(
|
|
245
|
-
aggregateFunction
|
|
246
|
-
)}"`;
|
|
247
|
-
if (!(fields == null ? void 0 : fields.length))
|
|
248
|
-
return "*";
|
|
249
|
-
return this.parseFields(fields).join(", ");
|
|
250
|
-
}
|
|
251
|
-
parseFields(fields) {
|
|
252
|
-
const columnsFromSchema = Object.keys(
|
|
253
|
-
this.crudSchema.columns
|
|
254
|
-
);
|
|
255
|
-
const attributes = fields.filter((field) => columnsFromSchema.indexOf(field) !== -1).map((field) => `"${field}"`);
|
|
256
|
-
fields.filter((field) => columnsFromSchema.indexOf(field) === -1).forEach((field) => {
|
|
257
|
-
attributes.push(`"qvAttributes" ->> '${field}' as "${field}"`);
|
|
258
|
-
});
|
|
259
|
-
return attributes;
|
|
260
|
-
}
|
|
261
|
-
async findCommand(options = {}) {
|
|
262
|
-
let query = `SELECT ${this.getSelectClause(
|
|
263
|
-
options.aggregateFunction,
|
|
264
|
-
options.fields
|
|
265
|
-
)} FROM ${format.ident(this.dbSchema)}.${format.ident(this.tableName)}`;
|
|
266
|
-
query = this.addFiltersToQuery(query, options.filters);
|
|
267
|
-
if (!options.aggregateFunction) {
|
|
268
|
-
query = this.addOrderByToQuery(query, options.sorting);
|
|
269
|
-
query = this.addPaginationToQuery(query, options.pagination);
|
|
270
|
-
}
|
|
271
|
-
return (await this.runQuery(query)).rows;
|
|
272
|
-
}
|
|
273
|
-
sanitizeValue(value) {
|
|
274
|
-
if (Array.isArray(value)) {
|
|
275
|
-
if (value.length === 0)
|
|
276
|
-
;
|
|
277
|
-
const formattedArray = value.map((item) => {
|
|
278
|
-
if (typeof item === "string") {
|
|
279
|
-
return `'${item}'`;
|
|
280
|
-
} else if (typeof item === "object") {
|
|
281
|
-
return JSON.stringify(item);
|
|
282
|
-
} else {
|
|
283
|
-
return item;
|
|
284
|
-
}
|
|
285
|
-
}).join(",");
|
|
286
|
-
return JSON.stringify(formattedArray);
|
|
287
|
-
} else {
|
|
288
|
-
return format.literal(value);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
async updateCommand(filters, data) {
|
|
292
|
-
let query = `UPDATE ${format.ident(this.dbSchema)}.${format.ident(
|
|
293
|
-
this.tableName
|
|
294
|
-
)} SET`;
|
|
295
|
-
const updateClauses = Object.entries(data).map(([key, value]) => {
|
|
296
|
-
const dbValue = format.literal(this.formatValue(value));
|
|
297
|
-
return `${format.ident(key)} = ${dbValue}`;
|
|
298
|
-
});
|
|
299
|
-
query += ` ${updateClauses.join(", ")}`;
|
|
300
|
-
query += " WHERE ";
|
|
301
|
-
query += this.buildFilterClause(filters);
|
|
302
|
-
return this.runQuery(query);
|
|
303
|
-
}
|
|
304
|
-
buildFilterClauseForFilterGroups(filterGroups) {
|
|
305
|
-
const filterClauses = filterGroups.map((filterGroup) => {
|
|
306
|
-
return `(${this.buildFilterClause(filterGroup)})`;
|
|
307
|
-
});
|
|
308
|
-
return filterClauses.join(" OR ");
|
|
309
|
-
}
|
|
310
|
-
async deleteCommand(filters, useFilterGroups = false) {
|
|
311
|
-
let query = `DELETE FROM ${format.ident(this.dbSchema)}.${format.ident(
|
|
312
|
-
this.tableName
|
|
313
|
-
)}`;
|
|
314
|
-
if (filters) {
|
|
315
|
-
query += " WHERE ";
|
|
316
|
-
if (useFilterGroups) {
|
|
317
|
-
query += this.buildFilterClauseForFilterGroups(
|
|
318
|
-
filters
|
|
319
|
-
);
|
|
320
|
-
} else {
|
|
321
|
-
query += this.buildFilterClause(
|
|
322
|
-
filters
|
|
323
|
-
);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
return this.runQuery(query);
|
|
327
|
-
}
|
|
328
|
-
query(queryText, values) {
|
|
329
|
-
return this.runQuery(queryText, values);
|
|
330
|
-
}
|
|
331
|
-
async updateExpressionCommand(filters, actions, options = {}) {
|
|
332
|
-
let query = `UPDATE ${format.ident(this.dbSchema)}.${format.ident(
|
|
333
|
-
this.tableName
|
|
334
|
-
)} SET`;
|
|
335
|
-
const set = actions.SET || [];
|
|
336
|
-
const add = actions.ADD || [];
|
|
337
|
-
const columns = this.crudSchema.columns;
|
|
338
|
-
const setValues = this.replacePathAndValueByAttributeNames(
|
|
339
|
-
set,
|
|
340
|
-
options,
|
|
341
|
-
columns,
|
|
342
|
-
DYNAMO_DB_UPDATE_ACTIONS.SET
|
|
343
|
-
);
|
|
344
|
-
const addValues = this.replacePathAndValueByAttributeNames(
|
|
345
|
-
add,
|
|
346
|
-
options,
|
|
347
|
-
columns,
|
|
348
|
-
DYNAMO_DB_UPDATE_ACTIONS.ADD
|
|
349
|
-
);
|
|
350
|
-
const setValuesAndAddValues = setValues.concat(addValues);
|
|
351
|
-
const updateClauses = [];
|
|
352
|
-
const jsonSetExpressionGroup = {};
|
|
353
|
-
const queryFunctions = [];
|
|
354
|
-
setValuesAndAddValues.forEach((expression) => {
|
|
355
|
-
const { path, value, createNewColumn, actionName, dynamoFuncName } = expression;
|
|
356
|
-
if (dynamoFuncName) {
|
|
357
|
-
queryFunctions.push({ path, value, dynamoFuncName });
|
|
358
|
-
}
|
|
359
|
-
if (path.includes(".") && !dynamoFuncName) {
|
|
360
|
-
const jsonExpr = this.getJSONBSetExpressionByAction(
|
|
361
|
-
path,
|
|
362
|
-
value,
|
|
363
|
-
{
|
|
364
|
-
createNewColumn,
|
|
365
|
-
actionName
|
|
366
|
-
}
|
|
367
|
-
);
|
|
368
|
-
const columnName = jsonExpr.columnName;
|
|
369
|
-
if (!jsonSetExpressionGroup[columnName]) {
|
|
370
|
-
jsonSetExpressionGroup[columnName] = [jsonExpr];
|
|
371
|
-
} else {
|
|
372
|
-
jsonSetExpressionGroup[columnName].push(jsonExpr);
|
|
373
|
-
}
|
|
374
|
-
} else if (!dynamoFuncName) {
|
|
375
|
-
let expValue;
|
|
376
|
-
const column = this.crudSchema.columns[path];
|
|
377
|
-
if ((column == null ? void 0 : column.type) == void 0)
|
|
378
|
-
throw `Column type definition for column: (${path}) must be in the CrudSchema`;
|
|
379
|
-
let formattedValue;
|
|
380
|
-
switch (column.type) {
|
|
381
|
-
case "object":
|
|
382
|
-
{
|
|
383
|
-
const valueSerialized = `${JSON.stringify(
|
|
384
|
-
value
|
|
385
|
-
).replace(/'/g, "''")}`;
|
|
386
|
-
expValue = `'${valueSerialized}'::jsonb`;
|
|
387
|
-
}
|
|
388
|
-
break;
|
|
389
|
-
case "array":
|
|
390
|
-
formattedValue = format.literal(value);
|
|
391
|
-
expValue = `ARRAY[${formattedValue}]`;
|
|
392
|
-
break;
|
|
393
|
-
default:
|
|
394
|
-
formattedValue = format.literal(value);
|
|
395
|
-
expValue = formattedValue;
|
|
396
|
-
break;
|
|
397
|
-
}
|
|
398
|
-
this.crudSchema.columns;
|
|
399
|
-
updateClauses.push(`${format.ident(path)} = ${expValue}`);
|
|
400
|
-
}
|
|
401
|
-
});
|
|
402
|
-
if (Object.keys(jsonSetExpressionGroup).length > 0) {
|
|
403
|
-
Object.keys(jsonSetExpressionGroup).forEach((groupIndex) => {
|
|
404
|
-
const jsonSetExpression = this.buildJSONBExpression(
|
|
405
|
-
jsonSetExpressionGroup[groupIndex],
|
|
406
|
-
"jsonb_set"
|
|
407
|
-
);
|
|
408
|
-
updateClauses.push(`${groupIndex} = ${jsonSetExpression}`);
|
|
409
|
-
});
|
|
410
|
-
}
|
|
411
|
-
if (queryFunctions.length > 0) {
|
|
412
|
-
queryFunctions.forEach((queryFunction) => {
|
|
413
|
-
updateClauses.push(
|
|
414
|
-
`${format.ident(queryFunction.path)} = ${queryFunction.value}`
|
|
415
|
-
);
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
query += ` ${updateClauses.join(", ")}`;
|
|
419
|
-
query += " WHERE ";
|
|
420
|
-
query += this.buildFilterClause(filters);
|
|
421
|
-
return this.runQuery(query);
|
|
422
|
-
}
|
|
423
|
-
/**
|
|
424
|
-
* @description Builds a jsonb expression like jsonb_insert, or jsonb_set
|
|
425
|
-
* @param jsonSetExpressions
|
|
426
|
-
* @param functionName
|
|
427
|
-
* @returns
|
|
428
|
-
*/
|
|
429
|
-
buildJSONBExpression(jsonSetExpressions, functionName) {
|
|
430
|
-
let jsonSetStringExpr = "";
|
|
431
|
-
jsonSetExpressions.forEach((expression, index) => {
|
|
432
|
-
const { columnName, jsonExpr } = expression;
|
|
433
|
-
if (index === 0) {
|
|
434
|
-
jsonSetStringExpr = `${functionName}(${columnName},${jsonExpr})`;
|
|
435
|
-
} else {
|
|
436
|
-
jsonSetStringExpr = `${functionName}(${jsonSetStringExpr},${jsonExpr})`;
|
|
437
|
-
}
|
|
438
|
-
});
|
|
439
|
-
return jsonSetStringExpr;
|
|
440
|
-
}
|
|
441
|
-
/**
|
|
442
|
-
* @description Serializes a JSON value
|
|
443
|
-
* @param value
|
|
444
|
-
* @returns
|
|
445
|
-
*/
|
|
446
|
-
serializeJSONValue(value) {
|
|
447
|
-
const valueSerialized = typeof value == "object" ? `${JSON.stringify(value).replace(/'/g, "''")}` : value;
|
|
448
|
-
return valueSerialized;
|
|
449
|
-
}
|
|
450
|
-
getJSONBSetExpressionByAction(path, value, options) {
|
|
451
|
-
path = path.replace(/\[(\d+)\]/g, ".$1");
|
|
452
|
-
const pathSplitted = path.split(".");
|
|
453
|
-
const parentPath = pathSplitted[0];
|
|
454
|
-
const { createNewColumn, actionName } = options;
|
|
455
|
-
const pathSerialized = pathSplitted.slice(1).join(",");
|
|
456
|
-
const valueSerialized = `'${JSON.stringify(value).replace(
|
|
457
|
-
/'/g,
|
|
458
|
-
"''"
|
|
459
|
-
)}'`;
|
|
460
|
-
if (actionName == DYNAMO_DB_UPDATE_ACTIONS.ADD) {
|
|
461
|
-
if (typeof value != "string" && !isNaN(value)) {
|
|
462
|
-
const resultExpr = {
|
|
463
|
-
jsonExpr: `'{${pathSerialized}}',to_jsonb(COALESCE(("${parentPath}"#>'{${pathSerialized}}')::numeric,0) + ${valueSerialized})`,
|
|
464
|
-
columnName: `"${parentPath}"`
|
|
465
|
-
};
|
|
466
|
-
return resultExpr;
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
return {
|
|
470
|
-
jsonExpr: `'{${pathSerialized}}',${valueSerialized},${createNewColumn}`,
|
|
471
|
-
columnName: `"${parentPath}"`
|
|
472
|
-
};
|
|
473
|
-
}
|
|
474
|
-
getListAppendDefFromValue(queryValue, columnType) {
|
|
475
|
-
const regexListAppend = /list_append\(([^)]+)\)/gm;
|
|
476
|
-
const listAppendString = "list_append(";
|
|
477
|
-
const matchList = queryValue.match(regexListAppend) || [];
|
|
478
|
-
const groupResult = matchList[0];
|
|
479
|
-
if (groupResult) {
|
|
480
|
-
const attributesFromGroup = groupResult.slice(listAppendString.length, -1).split(",");
|
|
481
|
-
const attributes = {
|
|
482
|
-
originalString: groupResult,
|
|
483
|
-
path: attributesFromGroup[0].trim(),
|
|
484
|
-
value: attributesFromGroup.slice(1).join(",").trim(),
|
|
485
|
-
functionExpr: ""
|
|
486
|
-
};
|
|
487
|
-
if (columnType == "array") {
|
|
488
|
-
attributes["functionExpr"] = this.buildArrayAppendExpr(attributes);
|
|
489
|
-
} else {
|
|
490
|
-
attributes["functionExpr"] = this.buildJsonbInsertExpr(attributes);
|
|
491
|
-
}
|
|
492
|
-
return attributes;
|
|
493
|
-
}
|
|
494
|
-
return null;
|
|
495
|
-
}
|
|
496
|
-
buildArrayAppendExpr(params) {
|
|
497
|
-
const arrayPath = params.path.split(".");
|
|
498
|
-
const columnName = arrayPath.shift();
|
|
499
|
-
return `ARRAY_APPEND("${columnName}",${params.value})`;
|
|
500
|
-
}
|
|
501
|
-
buildJsonbInsertExpr(params) {
|
|
502
|
-
const arrayPath = params.path.split(".");
|
|
503
|
-
const columnName = arrayPath.shift();
|
|
504
|
-
const jsonbInsertExpressions = this.getExpressionsByDefinitionForJSONBInsert(
|
|
505
|
-
columnName,
|
|
506
|
-
params.value
|
|
507
|
-
);
|
|
508
|
-
const expressions = this.buildJSONBExpression(
|
|
509
|
-
jsonbInsertExpressions,
|
|
510
|
-
"jsonb_insert"
|
|
511
|
-
);
|
|
512
|
-
return expressions;
|
|
513
|
-
}
|
|
514
|
-
getExpressionsByDefinitionForJSONBInsert(columnName, value) {
|
|
515
|
-
const jsonbInsertExpressions = [];
|
|
516
|
-
try {
|
|
517
|
-
const parsedValue = JSON.parse(value);
|
|
518
|
-
if (Array.isArray(parsedValue)) {
|
|
519
|
-
parsedValue.forEach((arrayValue) => {
|
|
520
|
-
jsonbInsertExpressions.push({
|
|
521
|
-
jsonExpr: `'{0}','${this.serializeJSONValue(
|
|
522
|
-
arrayValue
|
|
523
|
-
)}'`,
|
|
524
|
-
columnName: `"${columnName}"`
|
|
525
|
-
});
|
|
526
|
-
});
|
|
527
|
-
} else {
|
|
528
|
-
jsonbInsertExpressions.push({
|
|
529
|
-
jsonExpr: `'{0}','${this.serializeJSONValue(parsedValue)}'`,
|
|
530
|
-
columnName: `"${columnName}"`
|
|
531
|
-
});
|
|
532
|
-
}
|
|
533
|
-
} catch (error) {
|
|
534
|
-
jsonbInsertExpressions.push({
|
|
535
|
-
jsonExpr: `'{0}','${value}'`,
|
|
536
|
-
columnName: `"${columnName}"`
|
|
537
|
-
});
|
|
538
|
-
}
|
|
539
|
-
return jsonbInsertExpressions;
|
|
540
|
-
}
|
|
541
|
-
getInsertExprFromJsonbDef(queryValue, columnType) {
|
|
542
|
-
const listAppendParams = this.getListAppendDefFromValue(
|
|
543
|
-
queryValue,
|
|
544
|
-
columnType
|
|
545
|
-
);
|
|
546
|
-
if (listAppendParams != null) {
|
|
547
|
-
queryValue = queryValue.replace(
|
|
548
|
-
listAppendParams.originalString,
|
|
549
|
-
listAppendParams.functionExpr
|
|
550
|
-
);
|
|
551
|
-
}
|
|
552
|
-
return queryValue;
|
|
553
|
-
}
|
|
554
|
-
replacePathAndValueByAttributeNames(actions, options, columns, actionName) {
|
|
555
|
-
return actions.map((action) => {
|
|
556
|
-
action.path = this.replaceExpressionAttributeNames(
|
|
557
|
-
action.path,
|
|
558
|
-
options
|
|
559
|
-
);
|
|
560
|
-
if (typeof action.value == "string" && action.value.includes("list_append")) {
|
|
561
|
-
action.path = action.path.split(".")[0];
|
|
562
|
-
const column = columns[action.path];
|
|
563
|
-
action.value = this.replaceExpressionAttributeValuesForDynamoFunctions(
|
|
564
|
-
action.value,
|
|
565
|
-
options
|
|
566
|
-
);
|
|
567
|
-
action.value = this.getInsertExprFromJsonbDef(
|
|
568
|
-
action.value,
|
|
569
|
-
column.type
|
|
570
|
-
);
|
|
571
|
-
action.dynamoFuncName = "list_append";
|
|
572
|
-
} else {
|
|
573
|
-
action.value = this.replaceExpressionAttributeValues(
|
|
574
|
-
action.value,
|
|
575
|
-
options
|
|
576
|
-
);
|
|
577
|
-
}
|
|
578
|
-
action.actionName = actionName;
|
|
579
|
-
action.createNewColumn = true;
|
|
580
|
-
return action;
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
|
-
replaceExpressionAttributeValuesForDynamoFunctions(value, options) {
|
|
584
|
-
const { expressionAttributeNames, expressionAttributeValues } = options;
|
|
585
|
-
const exprAttributeNamesKeys = expressionAttributeNames ? Object.keys(expressionAttributeNames) : [];
|
|
586
|
-
const exprAttributeValuesKeys = expressionAttributeValues ? Object.keys(expressionAttributeValues) : [];
|
|
587
|
-
if (exprAttributeNamesKeys.length > 0) {
|
|
588
|
-
exprAttributeNamesKeys.forEach((exprAttribute) => {
|
|
589
|
-
value = value.replace(
|
|
590
|
-
exprAttribute,
|
|
591
|
-
expressionAttributeNames[exprAttribute]
|
|
592
|
-
);
|
|
593
|
-
});
|
|
594
|
-
}
|
|
595
|
-
if (exprAttributeValuesKeys.length > 0) {
|
|
596
|
-
exprAttributeValuesKeys.forEach((exprAttribute) => {
|
|
597
|
-
const valueSerialized = this.serializeJSONValue(
|
|
598
|
-
expressionAttributeValues[exprAttribute]
|
|
599
|
-
);
|
|
600
|
-
value = value.replace(exprAttribute, `${valueSerialized}`);
|
|
601
|
-
});
|
|
602
|
-
}
|
|
603
|
-
return value;
|
|
604
|
-
}
|
|
605
|
-
replaceExpressionAttributeNames(path, options) {
|
|
606
|
-
const { expressionAttributeNames } = options;
|
|
607
|
-
if (expressionAttributeNames) {
|
|
608
|
-
Object.keys(expressionAttributeNames).forEach(
|
|
609
|
-
(attributeName) => {
|
|
610
|
-
const attributeNameValue = expressionAttributeNames[attributeName];
|
|
611
|
-
path = path.replace(attributeName, attributeNameValue);
|
|
612
|
-
}
|
|
613
|
-
);
|
|
614
|
-
}
|
|
615
|
-
return path;
|
|
616
|
-
}
|
|
617
|
-
replaceExpressionAttributeValues(value, options) {
|
|
618
|
-
const { expressionAttributeValues } = options;
|
|
619
|
-
if (expressionAttributeValues[value] != void 0) {
|
|
620
|
-
return expressionAttributeValues[value];
|
|
621
|
-
}
|
|
622
|
-
return value;
|
|
623
|
-
}
|
|
624
|
-
};
|
|
625
|
-
|
|
626
|
-
// src/services/cruds/postgresql/postgreSqlCrud.service.ts
|
|
627
|
-
var PostgreSqlCrudService = class extends PostgresqlClientService {
|
|
628
|
-
constructor(tableSchema) {
|
|
629
|
-
super(tableSchema);
|
|
630
|
-
this.tableSchema = tableSchema;
|
|
631
|
-
}
|
|
632
|
-
get idColumnName() {
|
|
633
|
-
return findIdColumnName(this.tableSchema.columns);
|
|
634
|
-
}
|
|
635
|
-
normalizeInputData(inputData) {
|
|
636
|
-
var _a;
|
|
637
|
-
inputData.qvAttributes = {};
|
|
638
|
-
for (const key in inputData) {
|
|
639
|
-
if (!this.tableSchema.columns[key] && key !== "qvAttributes") {
|
|
640
|
-
inputData.qvAttributes[key] = inputData[key];
|
|
641
|
-
delete inputData[key];
|
|
642
|
-
} else if (Array.isArray(inputData[key]) && ((_a = this.tableSchema.columns[key]) == null ? void 0 : _a.type) !== "array") {
|
|
643
|
-
inputData[key] = JSON.stringify(inputData[key]);
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
getItem(data) {
|
|
648
|
-
const resultItem = __spreadValues(__spreadValues({}, data), data.qvAttributes);
|
|
649
|
-
delete resultItem["qvAttributes"];
|
|
650
|
-
return resultItem;
|
|
651
|
-
}
|
|
652
|
-
prepareData(data) {
|
|
653
|
-
const inputData = __spreadValues({}, data);
|
|
654
|
-
this.normalizeInputData(inputData);
|
|
655
|
-
return inputData;
|
|
656
|
-
}
|
|
657
|
-
create(data) {
|
|
658
|
-
if (Array.isArray(data)) {
|
|
659
|
-
const inputDataArray = data.map((item) => this.prepareData(item));
|
|
660
|
-
return this.createCommand(inputDataArray).then((result) => ({
|
|
661
|
-
unprocessedItems: result.filter((r) => !r.success).map((r) => r.inputData)
|
|
662
|
-
}));
|
|
663
|
-
} else {
|
|
664
|
-
const inputData = this.prepareData(data);
|
|
665
|
-
return this.createCommand([inputData]).then(
|
|
666
|
-
(result) => result.rowCount ? this.getItem(result.rows[0]) : null
|
|
667
|
-
);
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
findItem(findOptions) {
|
|
671
|
-
return this.findCommand(findOptions).then((data) => {
|
|
672
|
-
return (data == null ? void 0 : data.length) ? this.getItem(data[0]) : null;
|
|
673
|
-
});
|
|
674
|
-
}
|
|
675
|
-
async processQueryResult(findOptions, omitPagination = false) {
|
|
676
|
-
const rows = await this.findCommand(findOptions);
|
|
677
|
-
const items = rows.map(
|
|
678
|
-
(row) => this.getItem(row)
|
|
679
|
-
);
|
|
680
|
-
const { limit, from } = (findOptions == null ? void 0 : findOptions.pagination) || {};
|
|
681
|
-
const hasMoreRecords = items.length && items.length === limit;
|
|
682
|
-
const newFrom = limit && hasMoreRecords ? limit + (from || 0) : null;
|
|
683
|
-
const result = {
|
|
684
|
-
items,
|
|
685
|
-
pagination: omitPagination ? null : { limit, from: newFrom },
|
|
686
|
-
count: items.length
|
|
687
|
-
};
|
|
688
|
-
return result;
|
|
689
|
-
}
|
|
690
|
-
async find(findOptions) {
|
|
691
|
-
return this.processQueryResult(findOptions);
|
|
692
|
-
}
|
|
693
|
-
async findAll(findOptions) {
|
|
694
|
-
return this.processQueryResult(findOptions, true);
|
|
695
|
-
}
|
|
696
|
-
async findCount(findOptions) {
|
|
697
|
-
const items = await this.findCommand(__spreadProps(__spreadValues({}, findOptions), {
|
|
698
|
-
aggregateFunction: "COUNT" /* COUNT */
|
|
699
|
-
}));
|
|
700
|
-
const aggFunctionProperty = buildAggFunctionAlias(
|
|
701
|
-
"COUNT" /* COUNT */
|
|
702
|
-
);
|
|
703
|
-
const item = items.length ? items[0] : {};
|
|
704
|
-
return item[aggFunctionProperty] || 0;
|
|
705
|
-
}
|
|
706
|
-
async update(filters, data) {
|
|
707
|
-
const savedRecord = await this.findItem({ filters });
|
|
708
|
-
const inputData = __spreadValues(__spreadValues({}, savedRecord), data);
|
|
709
|
-
await this.updateCommand(filters, this.prepareData(inputData));
|
|
710
|
-
return this.getItem(inputData);
|
|
711
|
-
}
|
|
712
|
-
async remove(filters, options) {
|
|
713
|
-
await this.deleteCommand(filters, options == null ? void 0 : options.filterGroups);
|
|
714
|
-
}
|
|
715
|
-
runQuery(querySentence, values) {
|
|
716
|
-
return super.runQuery(querySentence, values);
|
|
717
|
-
}
|
|
718
|
-
async updateExpressions(filters, actions, options) {
|
|
719
|
-
const result = await this.updateExpressionCommand(
|
|
720
|
-
filters,
|
|
721
|
-
actions,
|
|
722
|
-
options
|
|
723
|
-
);
|
|
724
|
-
return PersistenceErrorWrapper(result);
|
|
725
|
-
}
|
|
726
|
-
};
|
|
727
|
-
|
|
728
|
-
export { PostgreSqlCrudService };
|
|
729
|
-
//# sourceMappingURL=out.js.map
|
|
730
|
-
//# sourceMappingURL=postgreSqlCrud.service-GXH7ZQZY.mjs.map
|