@qrvey/data-persistence 0.3.6-beta.1 → 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/cjs/services/cruds/postgresql/postgreSqlClient.service.js +1 -1
- package/dist/cjs/services/cruds/postgresql/postgreSqlClient.service.js.map +1 -1
- 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-WTKLHYDV.mjs +0 -728
- package/dist/esm/postgreSqlCrud.service-WTKLHYDV.mjs.map +0 -1
|
@@ -1,818 +0,0 @@
|
|
|
1
|
-
import { getTableName, findIdColumnName, getPrimaryKeyColumns, PersistenceErrorWrapper } from './chunk-DHIGNHXS.mjs';
|
|
2
|
-
import { __privateAdd, __spreadProps, __spreadValues, FILTER_OPERATOR_MAP, DYNAMODB_OPERATORS, __privateMethod, DYNAMO_DB_UPDATE_ACTIONS } from './chunk-6MOAJFFY.mjs';
|
|
3
|
-
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
|
|
4
|
-
import { DynamoDBDocumentClient, GetCommand, QueryCommand, ScanCommand, PutCommand, UpdateCommand, DeleteCommand, BatchWriteCommand } from '@aws-sdk/lib-dynamodb';
|
|
5
|
-
|
|
6
|
-
var AWS_REGION = process.env.AWS_DEFAULT_REGION;
|
|
7
|
-
var DynamoDbClientService = class {
|
|
8
|
-
constructor(tableName) {
|
|
9
|
-
if (!tableName)
|
|
10
|
-
throw new Error(
|
|
11
|
-
'The "tableName" is required to use a DynamoDbClientService.'
|
|
12
|
-
);
|
|
13
|
-
this.tableName = tableName;
|
|
14
|
-
const client = new DynamoDBClient({ region: AWS_REGION });
|
|
15
|
-
this.dynamoDBClient = DynamoDBDocumentClient.from(client, {
|
|
16
|
-
marshallOptions: {
|
|
17
|
-
removeUndefinedValues: true
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Get an item by key
|
|
23
|
-
* @param {Object} keyObject - Ex: { jobId: 1234 }
|
|
24
|
-
* @param {GetCommandInput} options
|
|
25
|
-
*/
|
|
26
|
-
async getByKey(keyObject, options = {}) {
|
|
27
|
-
const params = __spreadValues({
|
|
28
|
-
TableName: this.tableName,
|
|
29
|
-
Key: keyObject
|
|
30
|
-
}, options);
|
|
31
|
-
const result = await this.dynamoDBClient.send(new GetCommand(params));
|
|
32
|
-
return result.Item;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Query a table
|
|
36
|
-
* @param {QueryCommandInput} options
|
|
37
|
-
*/
|
|
38
|
-
async query(options = {}) {
|
|
39
|
-
const params = __spreadValues({
|
|
40
|
-
TableName: this.tableName
|
|
41
|
-
}, options);
|
|
42
|
-
const result = await this.dynamoDBClient.send(new QueryCommand(params));
|
|
43
|
-
if (result.$metadata)
|
|
44
|
-
delete result.$metadata;
|
|
45
|
-
return result;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Scan a table
|
|
49
|
-
* @param {ScanInput} options
|
|
50
|
-
*/
|
|
51
|
-
async scan(input) {
|
|
52
|
-
const params = __spreadProps(__spreadValues({}, input), {
|
|
53
|
-
TableName: this.tableName
|
|
54
|
-
});
|
|
55
|
-
const command = new ScanCommand(params);
|
|
56
|
-
const response = await this.dynamoDBClient.send(command);
|
|
57
|
-
if (response.$metadata)
|
|
58
|
-
delete response.$metadata;
|
|
59
|
-
return response;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Create/Replace a item object
|
|
63
|
-
* To take care:
|
|
64
|
-
* - https://stackoverflow.com/questions/43667229/difference-between-dynamodb-putitem-vs-updateitem
|
|
65
|
-
* @param {Object} input
|
|
66
|
-
*/
|
|
67
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
68
|
-
async put(input) {
|
|
69
|
-
const params = {
|
|
70
|
-
TableName: this.tableName,
|
|
71
|
-
Item: input,
|
|
72
|
-
ReturnValues: "NONE"
|
|
73
|
-
};
|
|
74
|
-
return await this.dynamoDBClient.send(new PutCommand(params));
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Update a item object
|
|
78
|
-
* To take care:
|
|
79
|
-
* - https://stackoverflow.com/questions/43667229/difference-between-dynamodb-putitem-vs-updateitem
|
|
80
|
-
* @param {Object} keyObject - Ex: { jobId: 1234 }
|
|
81
|
-
* @param {UpdateCommandInput} updateObject
|
|
82
|
-
*/
|
|
83
|
-
async update(keyObject, options = {}) {
|
|
84
|
-
const params = __spreadValues({
|
|
85
|
-
TableName: this.tableName,
|
|
86
|
-
Key: keyObject,
|
|
87
|
-
ReturnValues: "NONE"
|
|
88
|
-
}, options);
|
|
89
|
-
return await this.dynamoDBClient.send(new UpdateCommand(params));
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Delete/Remove an item object
|
|
93
|
-
*/
|
|
94
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
95
|
-
async remove(keyObject) {
|
|
96
|
-
const params = {
|
|
97
|
-
TableName: this.tableName,
|
|
98
|
-
Key: keyObject,
|
|
99
|
-
ReturnValues: "NONE"
|
|
100
|
-
};
|
|
101
|
-
return await this.dynamoDBClient.send(new DeleteCommand(params));
|
|
102
|
-
}
|
|
103
|
-
batchWrittenPut(data) {
|
|
104
|
-
const putRequests = data.map((item) => ({
|
|
105
|
-
PutRequest: {
|
|
106
|
-
Item: item
|
|
107
|
-
}
|
|
108
|
-
}));
|
|
109
|
-
const params = {
|
|
110
|
-
RequestItems: {
|
|
111
|
-
[this.tableName]: putRequests
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
const insertCommand = new BatchWriteCommand(params);
|
|
115
|
-
return this.dynamoDBClient.send(insertCommand);
|
|
116
|
-
}
|
|
117
|
-
buildDeleteRequestKeys(filters) {
|
|
118
|
-
const deleteRequestKeys = {};
|
|
119
|
-
filters.forEach((filter) => {
|
|
120
|
-
deleteRequestKeys[filter.attribute] = filter.value;
|
|
121
|
-
});
|
|
122
|
-
return deleteRequestKeys;
|
|
123
|
-
}
|
|
124
|
-
async batchRemove(filterGroups) {
|
|
125
|
-
if (!(filterGroups == null ? void 0 : filterGroups.length))
|
|
126
|
-
return;
|
|
127
|
-
const deleteRequests = filterGroups.map((filterGroup) => ({
|
|
128
|
-
DeleteRequest: {
|
|
129
|
-
Key: this.buildDeleteRequestKeys(filterGroup)
|
|
130
|
-
}
|
|
131
|
-
}));
|
|
132
|
-
const params = {
|
|
133
|
-
RequestItems: {
|
|
134
|
-
[this.tableName]: deleteRequests
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
await this.dynamoDBClient.send(new BatchWriteCommand(params));
|
|
138
|
-
}
|
|
139
|
-
async updateExpressions(keyObject, options = {}) {
|
|
140
|
-
const params = __spreadValues({
|
|
141
|
-
TableName: this.tableName,
|
|
142
|
-
Key: keyObject
|
|
143
|
-
}, options);
|
|
144
|
-
return await this.dynamoDBClient.send(new UpdateCommand(params));
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
// src/services/cruds/dynamodb/queryBuilderCondition.service.ts
|
|
149
|
-
var QueryBuilderConditionService = class {
|
|
150
|
-
constructor(query) {
|
|
151
|
-
this.tempKey = "";
|
|
152
|
-
this.tempLogicOperator = null;
|
|
153
|
-
this.wheres = [];
|
|
154
|
-
this.filters = [];
|
|
155
|
-
this.updates = [];
|
|
156
|
-
this.attributeNames = {};
|
|
157
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
158
|
-
this.attributeValues = {};
|
|
159
|
-
this.query = query;
|
|
160
|
-
this.command = {};
|
|
161
|
-
}
|
|
162
|
-
get() {
|
|
163
|
-
this.build();
|
|
164
|
-
return this.command;
|
|
165
|
-
}
|
|
166
|
-
setKey(key) {
|
|
167
|
-
this.tempKey = key;
|
|
168
|
-
return this;
|
|
169
|
-
}
|
|
170
|
-
setTmpLogicOp(logicOp) {
|
|
171
|
-
this.tempLogicOperator = logicOp;
|
|
172
|
-
return this;
|
|
173
|
-
}
|
|
174
|
-
setConfig(config) {
|
|
175
|
-
this.config = config;
|
|
176
|
-
return this;
|
|
177
|
-
}
|
|
178
|
-
from(methodName) {
|
|
179
|
-
this.queryFrom = methodName;
|
|
180
|
-
return this;
|
|
181
|
-
}
|
|
182
|
-
eq(keyValue) {
|
|
183
|
-
const { key, value } = this.generateKeyValue(keyValue);
|
|
184
|
-
let expression = `${key} = ${value}`;
|
|
185
|
-
if (this.tempLogicOperator)
|
|
186
|
-
expression = { expression, logicOperator: this.tempLogicOperator, config: this.config };
|
|
187
|
-
this.setExpression(expression);
|
|
188
|
-
return this.query;
|
|
189
|
-
}
|
|
190
|
-
notEq(keyValue) {
|
|
191
|
-
const { key, value } = this.generateKeyValue(keyValue);
|
|
192
|
-
let expression = `${key} <> ${value}`;
|
|
193
|
-
if (this.tempLogicOperator)
|
|
194
|
-
expression = { expression, logicOperator: this.tempLogicOperator, config: this.config };
|
|
195
|
-
this.setExpression(expression);
|
|
196
|
-
return this.query;
|
|
197
|
-
}
|
|
198
|
-
contains(keyValue) {
|
|
199
|
-
const { key, value } = this.generateKeyValue(keyValue);
|
|
200
|
-
let expression = `contains(${key}, ${value})`;
|
|
201
|
-
if (this.tempLogicOperator)
|
|
202
|
-
expression = { expression, logicOperator: this.tempLogicOperator, config: this.config };
|
|
203
|
-
this.setExpression(expression);
|
|
204
|
-
return this.query;
|
|
205
|
-
}
|
|
206
|
-
notContains(keyValue) {
|
|
207
|
-
const { key, value } = this.generateKeyValue(keyValue);
|
|
208
|
-
let expression = `NOT contains(${key}, ${value})`;
|
|
209
|
-
if (this.tempLogicOperator)
|
|
210
|
-
expression = { expression, logicOperator: this.tempLogicOperator, config: this.config };
|
|
211
|
-
this.setExpression(expression);
|
|
212
|
-
return this.query;
|
|
213
|
-
}
|
|
214
|
-
in(keyValue) {
|
|
215
|
-
const keyValues = Array.isArray(keyValue) ? keyValue : [keyValue];
|
|
216
|
-
const { key, value } = this.generateKeyValue(keyValues);
|
|
217
|
-
let expression = `${key} IN (${value})`;
|
|
218
|
-
if (this.tempLogicOperator)
|
|
219
|
-
expression = { expression, logicOperator: this.tempLogicOperator, config: this.config };
|
|
220
|
-
this.setExpression(expression);
|
|
221
|
-
return this.query;
|
|
222
|
-
}
|
|
223
|
-
beginsWith(keyValue) {
|
|
224
|
-
const { key, value } = this.generateKeyValue(keyValue);
|
|
225
|
-
let expression = `begins_with(${key}, ${value})`;
|
|
226
|
-
if (this.tempLogicOperator)
|
|
227
|
-
expression = { expression, logicOperator: this.tempLogicOperator, config: this.config };
|
|
228
|
-
this.setExpression(expression);
|
|
229
|
-
return this.query;
|
|
230
|
-
}
|
|
231
|
-
project(keyValue) {
|
|
232
|
-
const key = `#${keyValue}`;
|
|
233
|
-
this.attributeNames[key] = keyValue;
|
|
234
|
-
return key;
|
|
235
|
-
}
|
|
236
|
-
gt(keyValue) {
|
|
237
|
-
const { key, value } = this.generateKeyValue(keyValue);
|
|
238
|
-
let expression = `${key} > ${value}`;
|
|
239
|
-
if (this.tempLogicOperator)
|
|
240
|
-
expression = { expression, logicOperator: this.tempLogicOperator, config: this.config };
|
|
241
|
-
this.setExpression(expression);
|
|
242
|
-
return this.query;
|
|
243
|
-
}
|
|
244
|
-
gte(keyValue) {
|
|
245
|
-
const { key, value } = this.generateKeyValue(keyValue);
|
|
246
|
-
let expression = `${key} >= ${value}`;
|
|
247
|
-
if (this.tempLogicOperator)
|
|
248
|
-
expression = { expression, logicOperator: this.tempLogicOperator, config: this.config };
|
|
249
|
-
this.setExpression(expression);
|
|
250
|
-
return this.query;
|
|
251
|
-
}
|
|
252
|
-
lte(keyValue) {
|
|
253
|
-
const { key, value } = this.generateKeyValue(keyValue);
|
|
254
|
-
let expression = `${key} <= ${value}`;
|
|
255
|
-
if (this.tempLogicOperator)
|
|
256
|
-
expression = { expression, logicOperator: this.tempLogicOperator, config: this.config };
|
|
257
|
-
this.setExpression(expression);
|
|
258
|
-
return this.query;
|
|
259
|
-
}
|
|
260
|
-
lt(keyValue) {
|
|
261
|
-
const { key, value } = this.generateKeyValue(keyValue);
|
|
262
|
-
let expression = `${key} < ${value}`;
|
|
263
|
-
if (this.tempLogicOperator)
|
|
264
|
-
expression = { expression, logicOperator: this.tempLogicOperator, config: this.config };
|
|
265
|
-
this.setExpression(expression);
|
|
266
|
-
return this.query;
|
|
267
|
-
}
|
|
268
|
-
attribute_exists(keyValue) {
|
|
269
|
-
const { key } = this.generateKeyValue(keyValue, null, true);
|
|
270
|
-
let expression = `attribute_exists(${key})`;
|
|
271
|
-
if (this.tempLogicOperator)
|
|
272
|
-
expression = { expression, logicOperator: this.tempLogicOperator, config: this.config };
|
|
273
|
-
this.setExpression(expression);
|
|
274
|
-
return this.query;
|
|
275
|
-
}
|
|
276
|
-
attribute_not_exists(keyValue) {
|
|
277
|
-
const { key } = this.generateKeyValue(keyValue, null, true);
|
|
278
|
-
let expression = `attribute_not_exists(${key})`;
|
|
279
|
-
if (this.tempLogicOperator)
|
|
280
|
-
expression = { expression, logicOperator: this.tempLogicOperator, config: this.config };
|
|
281
|
-
this.setExpression(expression);
|
|
282
|
-
return this.query;
|
|
283
|
-
}
|
|
284
|
-
between(keyValues) {
|
|
285
|
-
const isValidValues = Array.isArray(keyValues) && (keyValues == null ? void 0 : keyValues.length) === 2;
|
|
286
|
-
if (!isValidValues)
|
|
287
|
-
throw new Error(
|
|
288
|
-
"The value for between filter operator should be an Array with 2 values."
|
|
289
|
-
);
|
|
290
|
-
const { key, value } = this.generateKeyValue(keyValues, " AND");
|
|
291
|
-
let expression = `${key} between ${value}`;
|
|
292
|
-
if (this.tempLogicOperator)
|
|
293
|
-
expression = { expression, logicOperator: this.tempLogicOperator, config: this.config };
|
|
294
|
-
this.setExpression(expression);
|
|
295
|
-
return this.query;
|
|
296
|
-
}
|
|
297
|
-
setExpression(expression) {
|
|
298
|
-
switch (this.queryFrom) {
|
|
299
|
-
case "filter" /* filter */:
|
|
300
|
-
this.filters.push(expression);
|
|
301
|
-
break;
|
|
302
|
-
case "update" /* update */:
|
|
303
|
-
this.updates.push(expression);
|
|
304
|
-
break;
|
|
305
|
-
default:
|
|
306
|
-
this.wheres.push(expression);
|
|
307
|
-
break;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
generateKeyValue(value, separatorCharacter = ",", omitAttributeValues = false) {
|
|
311
|
-
const keyExpression = `#${this.tempKey}1`;
|
|
312
|
-
this.attributeNames[keyExpression] = this.tempKey;
|
|
313
|
-
if (Array.isArray(value)) {
|
|
314
|
-
const valueExpressions = value.map((val, index) => {
|
|
315
|
-
let valueExpression = `:${this.tempKey}${index + 1}1`;
|
|
316
|
-
for (const [i] of Object.entries(this.attributeValues)) {
|
|
317
|
-
if (i === valueExpression)
|
|
318
|
-
valueExpression += "1";
|
|
319
|
-
}
|
|
320
|
-
this.attributeValues[valueExpression] = val;
|
|
321
|
-
return valueExpression;
|
|
322
|
-
});
|
|
323
|
-
return {
|
|
324
|
-
key: keyExpression,
|
|
325
|
-
value: valueExpressions.join(`${separatorCharacter} `)
|
|
326
|
-
};
|
|
327
|
-
} else {
|
|
328
|
-
let valueExpression = `:${this.tempKey}1`;
|
|
329
|
-
if (valueExpression in this.attributeValues) {
|
|
330
|
-
for (const [index] of Object.entries(this.attributeValues)) {
|
|
331
|
-
if (index === valueExpression)
|
|
332
|
-
valueExpression += "1";
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
if (!omitAttributeValues)
|
|
336
|
-
this.attributeValues[valueExpression] = value;
|
|
337
|
-
return { key: keyExpression, value: valueExpression };
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
build() {
|
|
341
|
-
var _a;
|
|
342
|
-
if (this.wheres.length > 0) {
|
|
343
|
-
const keyConditionExpression = this.wheres.join(" AND ");
|
|
344
|
-
this.command["KeyConditionExpression"] = keyConditionExpression;
|
|
345
|
-
}
|
|
346
|
-
if (this.filters.length > 0) {
|
|
347
|
-
let filterExpression = "";
|
|
348
|
-
(_a = this.filters) == null ? void 0 : _a.forEach((filter, index) => {
|
|
349
|
-
var _a2, _b;
|
|
350
|
-
if (filter == null ? void 0 : filter.logicOperator) {
|
|
351
|
-
if ((_a2 = filter == null ? void 0 : filter.config) == null ? void 0 : _a2.openExpression) {
|
|
352
|
-
filterExpression = filterExpression.replace(/\s+(AND|OR)\s*$/, ` ${filter.config.parentKey} (`);
|
|
353
|
-
if (filterExpression === "")
|
|
354
|
-
filterExpression += "(";
|
|
355
|
-
filterExpression += `${filter.expression} ${filter.logicOperator} `;
|
|
356
|
-
} else if ((_b = filter == null ? void 0 : filter.config) == null ? void 0 : _b.closeExpression) {
|
|
357
|
-
filterExpression += `${filter.expression}) ${filter.config.parentKey} `;
|
|
358
|
-
} else {
|
|
359
|
-
filterExpression += `${filter.expression} ${filter.logicOperator} `;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
});
|
|
363
|
-
filterExpression = filterExpression.replace(/\s+(AND|OR)\s*$/, "");
|
|
364
|
-
this.command["FilterExpression"] = filterExpression;
|
|
365
|
-
}
|
|
366
|
-
if (this.updates.length > 0) {
|
|
367
|
-
const filterExpression = this.updates.join(", ");
|
|
368
|
-
this.command["UpdateExpression"] = `SET ${filterExpression}`;
|
|
369
|
-
}
|
|
370
|
-
if (Object.values(this.attributeNames).length > 0)
|
|
371
|
-
this.command["ExpressionAttributeNames"] = this.attributeNames;
|
|
372
|
-
if (Object.values(this.attributeValues).length > 0)
|
|
373
|
-
this.command["ExpressionAttributeValues"] = this.attributeValues;
|
|
374
|
-
}
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
// src/services/cruds/dynamodb/queryBuilder.service.ts
|
|
378
|
-
var QueryBuilderService = class {
|
|
379
|
-
constructor(useScan = false) {
|
|
380
|
-
this.useScan = useScan;
|
|
381
|
-
this.command = {};
|
|
382
|
-
this.condition = new QueryBuilderConditionService(this);
|
|
383
|
-
}
|
|
384
|
-
get() {
|
|
385
|
-
const condition = this.condition.get();
|
|
386
|
-
return __spreadValues(__spreadValues({}, this.command), condition);
|
|
387
|
-
}
|
|
388
|
-
limit(num) {
|
|
389
|
-
this.command.Limit = num;
|
|
390
|
-
return this;
|
|
391
|
-
}
|
|
392
|
-
usingIndex(indexName) {
|
|
393
|
-
this.command.IndexName = indexName;
|
|
394
|
-
return this;
|
|
395
|
-
}
|
|
396
|
-
ascending() {
|
|
397
|
-
if (!this.useScan)
|
|
398
|
-
this.command.ScanIndexForward = true;
|
|
399
|
-
return this;
|
|
400
|
-
}
|
|
401
|
-
descending() {
|
|
402
|
-
if (!this.useScan)
|
|
403
|
-
this.command.ScanIndexForward = false;
|
|
404
|
-
return this;
|
|
405
|
-
}
|
|
406
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
407
|
-
startKey(lastEvaluatedKey) {
|
|
408
|
-
this.command.ExclusiveStartKey = lastEvaluatedKey;
|
|
409
|
-
return this;
|
|
410
|
-
}
|
|
411
|
-
projection(fields = []) {
|
|
412
|
-
if (!fields.length)
|
|
413
|
-
return this;
|
|
414
|
-
const expression = [];
|
|
415
|
-
fields.forEach((field) => {
|
|
416
|
-
const key = this.condition.project(field);
|
|
417
|
-
expression.push(key);
|
|
418
|
-
});
|
|
419
|
-
this.command.ProjectionExpression = expression.join(",");
|
|
420
|
-
return this;
|
|
421
|
-
}
|
|
422
|
-
where(keyName) {
|
|
423
|
-
this.condition.setKey(keyName).setTmpLogicOp(null).from("where" /* where */);
|
|
424
|
-
return this.condition;
|
|
425
|
-
}
|
|
426
|
-
filter(keyName, logicOperator = "AND", config) {
|
|
427
|
-
this.condition.setKey(keyName).setTmpLogicOp(logicOperator).setConfig(config).from("filter" /* filter */);
|
|
428
|
-
return this.condition;
|
|
429
|
-
}
|
|
430
|
-
update(attribute) {
|
|
431
|
-
for (const [key, value] of Object.entries(attribute)) {
|
|
432
|
-
this.condition.setKey(key).from("update" /* update */);
|
|
433
|
-
this.condition.eq(value);
|
|
434
|
-
}
|
|
435
|
-
return this;
|
|
436
|
-
}
|
|
437
|
-
consistentRead(consistentRead) {
|
|
438
|
-
this.command.ConsistentRead = consistentRead;
|
|
439
|
-
return this;
|
|
440
|
-
}
|
|
441
|
-
count() {
|
|
442
|
-
this.command.Select = "COUNT" /* COUNT */;
|
|
443
|
-
return this;
|
|
444
|
-
}
|
|
445
|
-
};
|
|
446
|
-
|
|
447
|
-
// src/services/cruds/dynamodb/dynamoDbCrud.service.ts
|
|
448
|
-
var _prepareAndExecuteUpdateExpression, prepareAndExecuteUpdateExpression_fn, _buildUpdateExpressionQuery, buildUpdateExpressionQuery_fn, _getKeyObjectForUpdateExpression, getKeyObjectForUpdateExpression_fn, _getUpdateExpressionOptions, getUpdateExpressionOptions_fn, _extractUpdateExpressionAttributesAndNames, extractUpdateExpressionAttributesAndNames_fn;
|
|
449
|
-
var DynamoDbCrudService = class {
|
|
450
|
-
constructor(tableSchema) {
|
|
451
|
-
this.tableSchema = tableSchema;
|
|
452
|
-
__privateAdd(this, _prepareAndExecuteUpdateExpression);
|
|
453
|
-
__privateAdd(this, _buildUpdateExpressionQuery);
|
|
454
|
-
__privateAdd(this, _getKeyObjectForUpdateExpression);
|
|
455
|
-
__privateAdd(this, _getUpdateExpressionOptions);
|
|
456
|
-
__privateAdd(this, _extractUpdateExpressionAttributesAndNames);
|
|
457
|
-
this.dynamoDbClientService = new DynamoDbClientService(this.tableName);
|
|
458
|
-
}
|
|
459
|
-
get tableName() {
|
|
460
|
-
return getTableName(this.tableSchema.table);
|
|
461
|
-
}
|
|
462
|
-
get idColumnName() {
|
|
463
|
-
return findIdColumnName(this.tableSchema.columns);
|
|
464
|
-
}
|
|
465
|
-
get defaultPrimaryKeys() {
|
|
466
|
-
return getPrimaryKeyColumns(this.tableSchema.columns);
|
|
467
|
-
}
|
|
468
|
-
async create(data) {
|
|
469
|
-
var _a;
|
|
470
|
-
if (Array.isArray(data)) {
|
|
471
|
-
const response = await this.dynamoDbClientService.batchWrittenPut(data);
|
|
472
|
-
return {
|
|
473
|
-
unprocessedItems: (_a = response.UnprocessedItems) != null ? _a : []
|
|
474
|
-
};
|
|
475
|
-
} else {
|
|
476
|
-
await this.dynamoDbClientService.put(data);
|
|
477
|
-
return data;
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
|
|
481
|
-
runQuery(queryCommand) {
|
|
482
|
-
throw new Error("Method not implemented.");
|
|
483
|
-
}
|
|
484
|
-
find(options = {}) {
|
|
485
|
-
var _a, _b, _c, _d;
|
|
486
|
-
const query = new QueryBuilderService(options.useScan);
|
|
487
|
-
if ((_a = options.index) == null ? void 0 : _a.indexName)
|
|
488
|
-
query.usingIndex((_b = options.index) == null ? void 0 : _b.indexName);
|
|
489
|
-
this.applySorting(query, options.sorting, (_c = options.index) == null ? void 0 : _c.indexName);
|
|
490
|
-
this.applyPagination(query, options.pagination);
|
|
491
|
-
if (options.consistentRead)
|
|
492
|
-
query.consistentRead(options.consistentRead);
|
|
493
|
-
if (options.fields)
|
|
494
|
-
query.projection(options.fields);
|
|
495
|
-
this.applyFilters(query, options);
|
|
496
|
-
if (options.aggregateFunction === "COUNT" /* COUNT */)
|
|
497
|
-
query.count();
|
|
498
|
-
return this.fetchResults(
|
|
499
|
-
query.get(),
|
|
500
|
-
(_d = options.pagination) == null ? void 0 : _d.limit,
|
|
501
|
-
options.useScan
|
|
502
|
-
).then((res) => {
|
|
503
|
-
var _a2, _b2;
|
|
504
|
-
const pagination = {};
|
|
505
|
-
if (res.lastEvaluatedKey)
|
|
506
|
-
pagination.from = res.lastEvaluatedKey;
|
|
507
|
-
if ((_a2 = options.pagination) == null ? void 0 : _a2.limit)
|
|
508
|
-
pagination.limit = (_b2 = options.pagination) == null ? void 0 : _b2.limit;
|
|
509
|
-
return {
|
|
510
|
-
items: res.items,
|
|
511
|
-
pagination,
|
|
512
|
-
count: res.count
|
|
513
|
-
};
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
|
-
async fetchResults(command, limit = 100, useScan = false) {
|
|
517
|
-
var _a, _b;
|
|
518
|
-
let results = [];
|
|
519
|
-
let lastEvaluatedKey = {};
|
|
520
|
-
let rowsCount = 0;
|
|
521
|
-
do {
|
|
522
|
-
const result = await this.fetchBatch(
|
|
523
|
-
command,
|
|
524
|
-
useScan,
|
|
525
|
-
lastEvaluatedKey
|
|
526
|
-
);
|
|
527
|
-
const rows = (_a = result.Items) != null ? _a : [];
|
|
528
|
-
results = results.concat(rows);
|
|
529
|
-
lastEvaluatedKey = (_b = result.LastEvaluatedKey) != null ? _b : {};
|
|
530
|
-
rowsCount += result.Count || rows.length;
|
|
531
|
-
} while (rowsCount < limit && this.isNotEmptyObject(lastEvaluatedKey));
|
|
532
|
-
const encryptedLastEvaluatedKey = this.isNotEmptyObject(
|
|
533
|
-
lastEvaluatedKey
|
|
534
|
-
) ? this.encryptPaginationKey(lastEvaluatedKey) : null;
|
|
535
|
-
return {
|
|
536
|
-
items: results,
|
|
537
|
-
lastEvaluatedKey: encryptedLastEvaluatedKey,
|
|
538
|
-
count: rowsCount
|
|
539
|
-
};
|
|
540
|
-
}
|
|
541
|
-
async fetchBatch(command, useScan, lastEvaluatedKey) {
|
|
542
|
-
var _a, _b, _c;
|
|
543
|
-
if (this.isNotEmptyObject(lastEvaluatedKey))
|
|
544
|
-
command.ExclusiveStartKey = lastEvaluatedKey;
|
|
545
|
-
const result = await (useScan ? this.dynamoDbClientService.scan(command) : this.dynamoDbClientService.query(command));
|
|
546
|
-
if (command.Select !== "COUNT" /* COUNT */) {
|
|
547
|
-
command.Limit = ((_a = command.Limit) != null ? _a : 0) - ((_c = (_b = result.Items) == null ? void 0 : _b.length) != null ? _c : 0);
|
|
548
|
-
}
|
|
549
|
-
return result;
|
|
550
|
-
}
|
|
551
|
-
applyPagination(query, pagination) {
|
|
552
|
-
if (pagination == null ? void 0 : pagination.limit)
|
|
553
|
-
query.limit(pagination.limit);
|
|
554
|
-
if (pagination == null ? void 0 : pagination.from)
|
|
555
|
-
query.startKey(this.decryptPaginationKey(pagination.from));
|
|
556
|
-
}
|
|
557
|
-
async findAll(options = {}, allResults = [], rowsCount = 0) {
|
|
558
|
-
const { items, pagination, count } = await this.find(options);
|
|
559
|
-
allResults.push(...items);
|
|
560
|
-
rowsCount += count;
|
|
561
|
-
if (pagination == null ? void 0 : pagination.from)
|
|
562
|
-
await this.findAll(__spreadProps(__spreadValues({}, options), { pagination }), allResults);
|
|
563
|
-
return {
|
|
564
|
-
items: allResults,
|
|
565
|
-
pagination: null,
|
|
566
|
-
count: rowsCount
|
|
567
|
-
};
|
|
568
|
-
}
|
|
569
|
-
async findCount(options = {}) {
|
|
570
|
-
var _a;
|
|
571
|
-
const findOptions = __spreadProps(__spreadValues({}, options), {
|
|
572
|
-
aggregateFunction: "COUNT" /* COUNT */
|
|
573
|
-
});
|
|
574
|
-
if ((_a = options.pagination) == null ? void 0 : _a.from) {
|
|
575
|
-
return this.find(findOptions).then((res) => res.count);
|
|
576
|
-
} else {
|
|
577
|
-
return this.findAll(findOptions).then((res) => res.count);
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
buildFindItemQuery(options) {
|
|
581
|
-
var _a;
|
|
582
|
-
const query = new QueryBuilderService();
|
|
583
|
-
if ((_a = options.index) == null ? void 0 : _a.indexName)
|
|
584
|
-
query.usingIndex(options.index.indexName);
|
|
585
|
-
this.applyFilters(query, options);
|
|
586
|
-
query.projection(options.fields);
|
|
587
|
-
query.limit(1);
|
|
588
|
-
return query;
|
|
589
|
-
}
|
|
590
|
-
findItem(options) {
|
|
591
|
-
const query = this.buildFindItemQuery(options);
|
|
592
|
-
return this.dynamoDbClientService.query(query.get()).then((result) => {
|
|
593
|
-
var _a;
|
|
594
|
-
if ((_a = result.Items) == null ? void 0 : _a.length)
|
|
595
|
-
return result.Items[0];
|
|
596
|
-
if (options.throwErrorIfNull)
|
|
597
|
-
throw new Error("NOT_FOUND");
|
|
598
|
-
return null;
|
|
599
|
-
});
|
|
600
|
-
}
|
|
601
|
-
applyWhereFilter(query, filter) {
|
|
602
|
-
var _a, _b;
|
|
603
|
-
const operator = FILTER_OPERATOR_MAP[(_b = (_a = filter.operator) == null ? void 0 : _a.toUpperCase()) != null ? _b : DYNAMODB_OPERATORS.EQUAL];
|
|
604
|
-
query.where(filter.attribute)[operator](filter.value);
|
|
605
|
-
}
|
|
606
|
-
applyFilterFilter(query, filter, logicOperator = "AND", config) {
|
|
607
|
-
var _a, _b;
|
|
608
|
-
const operator = FILTER_OPERATOR_MAP[(_b = (_a = filter.operator) == null ? void 0 : _a.toUpperCase()) != null ? _b : DYNAMODB_OPERATORS.EQUAL];
|
|
609
|
-
query.filter(filter.attribute, logicOperator, config)[operator](filter.value);
|
|
610
|
-
}
|
|
611
|
-
applyFilters(query, options) {
|
|
612
|
-
if (Array.isArray(options.filters)) {
|
|
613
|
-
this.applySimpleFilters(query, options);
|
|
614
|
-
} else {
|
|
615
|
-
this.applyCompoundFilters(query, options);
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
applySimpleFilters(query, options) {
|
|
619
|
-
var _a, _b;
|
|
620
|
-
const queryIndexColumns = (_b = (_a = options.index) == null ? void 0 : _a.columns) != null ? _b : [];
|
|
621
|
-
const defaultWhereProperties = this.defaultPrimaryKeys;
|
|
622
|
-
const whereProperties = (queryIndexColumns == null ? void 0 : queryIndexColumns.length) ? queryIndexColumns : defaultWhereProperties;
|
|
623
|
-
const filters = options.filters;
|
|
624
|
-
filters.forEach((filter) => {
|
|
625
|
-
const isWhereProperty = whereProperties.includes(filter.attribute);
|
|
626
|
-
isWhereProperty && !options.useScan ? this.applyWhereFilter(query, filter) : this.applyFilterFilter(query, filter);
|
|
627
|
-
});
|
|
628
|
-
}
|
|
629
|
-
applyCompoundFilters(query, options) {
|
|
630
|
-
if (!options.filters)
|
|
631
|
-
return;
|
|
632
|
-
this.buildFilterExpression(query, options);
|
|
633
|
-
}
|
|
634
|
-
buildFilterExpression(query, options, parentKey) {
|
|
635
|
-
var _a;
|
|
636
|
-
const compositeFilters = options.filters;
|
|
637
|
-
const queryIndexColumns = ((_a = options.index) == null ? void 0 : _a.columns) || [];
|
|
638
|
-
const defaultWhereProperties = this.defaultPrimaryKeys;
|
|
639
|
-
const whereProperties = (queryIndexColumns == null ? void 0 : queryIndexColumns.length) ? queryIndexColumns : defaultWhereProperties;
|
|
640
|
-
for (const [key, value] of Object.entries(compositeFilters)) {
|
|
641
|
-
value.forEach(
|
|
642
|
-
(filter, index) => {
|
|
643
|
-
const isCompositeFilter = "OR" in filter || "AND" in filter;
|
|
644
|
-
if (isCompositeFilter) {
|
|
645
|
-
const newOptions = __spreadProps(__spreadValues({}, options), {
|
|
646
|
-
filters: filter
|
|
647
|
-
});
|
|
648
|
-
this.buildFilterExpression(query, newOptions, key);
|
|
649
|
-
} else {
|
|
650
|
-
const simpleFilter = filter;
|
|
651
|
-
const isWhereProperty = whereProperties.includes(
|
|
652
|
-
simpleFilter.attribute
|
|
653
|
-
);
|
|
654
|
-
let config;
|
|
655
|
-
if (parentKey) {
|
|
656
|
-
config = {
|
|
657
|
-
parentKey,
|
|
658
|
-
openExpression: index === 0,
|
|
659
|
-
closeExpression: index === value.length - 1
|
|
660
|
-
};
|
|
661
|
-
}
|
|
662
|
-
isWhereProperty && !options.useScan ? this.applyWhereFilter(query, simpleFilter) : this.applyFilterFilter(
|
|
663
|
-
query,
|
|
664
|
-
simpleFilter,
|
|
665
|
-
key,
|
|
666
|
-
config
|
|
667
|
-
);
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
);
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
applySorting(query, sorting, sortIndex) {
|
|
674
|
-
if (sorting == null ? void 0 : sorting.length) {
|
|
675
|
-
if (sortIndex)
|
|
676
|
-
query.usingIndex(sortIndex);
|
|
677
|
-
if (sorting[0].direction === "DESC") {
|
|
678
|
-
query.descending();
|
|
679
|
-
} else {
|
|
680
|
-
query.ascending();
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
isNotEmptyObject(obj) {
|
|
685
|
-
return obj !== null && typeof obj === "object" && Object.keys(obj).length > 0;
|
|
686
|
-
}
|
|
687
|
-
encryptPaginationKey(key) {
|
|
688
|
-
const jsonKey = JSON.stringify(key);
|
|
689
|
-
return Buffer.from(jsonKey).toString("base64");
|
|
690
|
-
}
|
|
691
|
-
decryptPaginationKey(encodedKey) {
|
|
692
|
-
const decodedKey = Buffer.from(encodedKey, "base64").toString("utf-8");
|
|
693
|
-
return JSON.parse(decodedKey);
|
|
694
|
-
}
|
|
695
|
-
async update(filters, data, { replace = false }) {
|
|
696
|
-
const savedRecord = await this.findItem({
|
|
697
|
-
filters
|
|
698
|
-
});
|
|
699
|
-
const newData = __spreadValues(__spreadValues({}, savedRecord), data);
|
|
700
|
-
await this.dynamoDbClientService.put(newData);
|
|
701
|
-
return newData;
|
|
702
|
-
}
|
|
703
|
-
async remove(filters, options) {
|
|
704
|
-
if (options == null ? void 0 : options.filterGroups) {
|
|
705
|
-
await this.dynamoDbClientService.batchRemove(
|
|
706
|
-
filters
|
|
707
|
-
);
|
|
708
|
-
} else {
|
|
709
|
-
const key = filters.reduce((obj, item) => {
|
|
710
|
-
const property = item.attribute;
|
|
711
|
-
obj[property] = item.value;
|
|
712
|
-
return obj;
|
|
713
|
-
}, {});
|
|
714
|
-
await this.dynamoDbClientService.remove(key);
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
async updateExpressions(filters, actions, options) {
|
|
718
|
-
try {
|
|
719
|
-
return await __privateMethod(this, _prepareAndExecuteUpdateExpression, prepareAndExecuteUpdateExpression_fn).call(this, filters, actions, options);
|
|
720
|
-
} catch (error) {
|
|
721
|
-
PersistenceErrorWrapper(error);
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
};
|
|
725
|
-
_prepareAndExecuteUpdateExpression = new WeakSet();
|
|
726
|
-
prepareAndExecuteUpdateExpression_fn = async function(filters, actions, options) {
|
|
727
|
-
const queryObject = __privateMethod(this, _buildUpdateExpressionQuery, buildUpdateExpressionQuery_fn).call(this, { filters }).get();
|
|
728
|
-
const primaryKeys = this.defaultPrimaryKeys;
|
|
729
|
-
const keyObject = __privateMethod(this, _getKeyObjectForUpdateExpression, getKeyObjectForUpdateExpression_fn).call(this, queryObject, primaryKeys);
|
|
730
|
-
const updateExpressions = [];
|
|
731
|
-
Object.keys(actions).forEach((action) => {
|
|
732
|
-
const actionUpdateExpression = __privateMethod(this, _extractUpdateExpressionAttributesAndNames, extractUpdateExpressionAttributesAndNames_fn).call(this, actions, action);
|
|
733
|
-
updateExpressions.push(actionUpdateExpression);
|
|
734
|
-
});
|
|
735
|
-
const dbParams = __spreadValues({
|
|
736
|
-
UpdateExpression: updateExpressions.join(" ")
|
|
737
|
-
}, __privateMethod(this, _getUpdateExpressionOptions, getUpdateExpressionOptions_fn).call(this, options));
|
|
738
|
-
const { ExpressionAttributeValues, ExpressionAttributeNames } = queryObject;
|
|
739
|
-
if (Object.keys(ExpressionAttributeValues).length > 0) {
|
|
740
|
-
dbParams["ExpressionAttributeValues"] = __spreadValues(__spreadValues({}, ExpressionAttributeValues), dbParams.ExpressionAttributeValues);
|
|
741
|
-
}
|
|
742
|
-
if (Object.keys(ExpressionAttributeNames).length > 0) {
|
|
743
|
-
dbParams["ExpressionAttributeNames"] = __spreadValues(__spreadValues({}, ExpressionAttributeNames), dbParams.ExpressionAttributeNames);
|
|
744
|
-
}
|
|
745
|
-
if (queryObject.FilterExpression)
|
|
746
|
-
dbParams["ConditionExpression"] = queryObject.FilterExpression;
|
|
747
|
-
return this.dynamoDbClientService.updateExpressions(
|
|
748
|
-
keyObject,
|
|
749
|
-
dbParams
|
|
750
|
-
);
|
|
751
|
-
};
|
|
752
|
-
_buildUpdateExpressionQuery = new WeakSet();
|
|
753
|
-
buildUpdateExpressionQuery_fn = function(options) {
|
|
754
|
-
var _a;
|
|
755
|
-
const query = new QueryBuilderService();
|
|
756
|
-
if ((_a = options.index) == null ? void 0 : _a.indexName)
|
|
757
|
-
query.usingIndex(options.index.indexName);
|
|
758
|
-
this.applyFilters(query, options);
|
|
759
|
-
return query;
|
|
760
|
-
};
|
|
761
|
-
_getKeyObjectForUpdateExpression = new WeakSet();
|
|
762
|
-
getKeyObjectForUpdateExpression_fn = function(queryObject, primaryKeys) {
|
|
763
|
-
const keyObject = {};
|
|
764
|
-
Object.keys(queryObject.ExpressionAttributeNames).forEach(
|
|
765
|
-
(attribute) => {
|
|
766
|
-
const sanitizedAttribute = attribute.replace("#", "").replace("1", "");
|
|
767
|
-
if (primaryKeys.includes(sanitizedAttribute)) {
|
|
768
|
-
const valueName = `:${sanitizedAttribute}1`;
|
|
769
|
-
keyObject[sanitizedAttribute] = queryObject.ExpressionAttributeValues[valueName];
|
|
770
|
-
delete queryObject.ExpressionAttributeValues[valueName];
|
|
771
|
-
delete queryObject.ExpressionAttributeNames[attribute];
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
);
|
|
775
|
-
return keyObject;
|
|
776
|
-
};
|
|
777
|
-
_getUpdateExpressionOptions = new WeakSet();
|
|
778
|
-
getUpdateExpressionOptions_fn = function(options) {
|
|
779
|
-
const updateExprOptions = {
|
|
780
|
-
ReturnValues: options.returnValues
|
|
781
|
-
};
|
|
782
|
-
if (options.expressionAttributeNames)
|
|
783
|
-
updateExprOptions.ExpressionAttributeNames = options.expressionAttributeNames;
|
|
784
|
-
if (options.expressionAttributeValues)
|
|
785
|
-
updateExprOptions.ExpressionAttributeValues = options.expressionAttributeValues;
|
|
786
|
-
return updateExprOptions;
|
|
787
|
-
};
|
|
788
|
-
_extractUpdateExpressionAttributesAndNames = new WeakSet();
|
|
789
|
-
extractUpdateExpressionAttributesAndNames_fn = function(actions, actionType) {
|
|
790
|
-
const actionUpdateExpressions = [];
|
|
791
|
-
actions[actionType].forEach((action) => {
|
|
792
|
-
switch (actionType) {
|
|
793
|
-
case DYNAMO_DB_UPDATE_ACTIONS.DELETE:
|
|
794
|
-
case DYNAMO_DB_UPDATE_ACTIONS.REMOVE:
|
|
795
|
-
actionUpdateExpressions.push(`${action.path}`);
|
|
796
|
-
break;
|
|
797
|
-
case DYNAMO_DB_UPDATE_ACTIONS.ADD:
|
|
798
|
-
case DYNAMO_DB_UPDATE_ACTIONS.SET:
|
|
799
|
-
{
|
|
800
|
-
let operator = "";
|
|
801
|
-
if (actionType == DYNAMO_DB_UPDATE_ACTIONS.SET)
|
|
802
|
-
operator = "=";
|
|
803
|
-
actionUpdateExpressions.push(
|
|
804
|
-
`${action.path} ${operator}${action.value}`
|
|
805
|
-
);
|
|
806
|
-
}
|
|
807
|
-
break;
|
|
808
|
-
}
|
|
809
|
-
});
|
|
810
|
-
const actionUpdateExpression = `${actionType} ${actionUpdateExpressions.join(
|
|
811
|
-
", "
|
|
812
|
-
)}`;
|
|
813
|
-
return actionUpdateExpression;
|
|
814
|
-
};
|
|
815
|
-
|
|
816
|
-
export { DynamoDbCrudService };
|
|
817
|
-
//# sourceMappingURL=out.js.map
|
|
818
|
-
//# sourceMappingURL=dynamoDbCrud.service-EFYPBZKN.mjs.map
|