inlaweb-lib-dynamodb 1.0.5 → 1.0.7
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/index.d.ts +3 -2
- package/index.js +57 -26
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -5,14 +5,15 @@ export declare class DynamoLib {
|
|
|
5
5
|
private readonly logger;
|
|
6
6
|
constructor();
|
|
7
7
|
putItem(tableName: string, item: KeyValue): Promise<void>;
|
|
8
|
-
getItem<T>(tableName: string, key: KeyValue, projectionExpression?: string): Promise<T
|
|
8
|
+
getItem<T>(tableName: string, key: KeyValue, projectionExpression?: string): Promise<T>;
|
|
9
9
|
updateItem(params: InputUpdate): Promise<boolean>;
|
|
10
10
|
query<T>(params: {
|
|
11
11
|
tableName: string;
|
|
12
12
|
searchParameters: SearchParameters;
|
|
13
13
|
permission?: string;
|
|
14
14
|
username?: string;
|
|
15
|
-
}): Promise<T[]
|
|
15
|
+
}): Promise<T[]>;
|
|
16
|
+
deleteItem(tableName: string, key: KeyValue): Promise<void>;
|
|
16
17
|
private buildQueryParams;
|
|
17
18
|
private buildExpressions;
|
|
18
19
|
private buildFilterExpression;
|
package/index.js
CHANGED
|
@@ -51,10 +51,7 @@ class DynamoLib {
|
|
|
51
51
|
this.logger.debug(`GetItem: ${JSON.stringify(params)}`);
|
|
52
52
|
const response = await this.docClient.send(new lib_dynamodb_1.GetCommand(params));
|
|
53
53
|
this.logger.debug(`GetItem Result: ${JSON.stringify(response)}`);
|
|
54
|
-
|
|
55
|
-
return response.Item;
|
|
56
|
-
}
|
|
57
|
-
return;
|
|
54
|
+
return response.Item;
|
|
58
55
|
}
|
|
59
56
|
catch (error) {
|
|
60
57
|
this.logger.error(error.message);
|
|
@@ -80,10 +77,23 @@ class DynamoLib {
|
|
|
80
77
|
this.logger.debug(`Query: ${JSON.stringify(parameters)}`);
|
|
81
78
|
const response = await this.docClient.send(new lib_dynamodb_1.QueryCommand(parameters));
|
|
82
79
|
this.logger.debug(`Query Result: ${JSON.stringify(response)}`);
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
80
|
+
return response.Items;
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
this.logger.error(error.message);
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async deleteItem(tableName, key) {
|
|
88
|
+
try {
|
|
89
|
+
const params = {
|
|
90
|
+
TableName: tableName,
|
|
91
|
+
Key: key,
|
|
92
|
+
ReturnConsumedCapacity: "TOTAL",
|
|
93
|
+
};
|
|
94
|
+
this.logger.debug(`DeleteItem: ${JSON.stringify(params)}`);
|
|
95
|
+
const response = await this.docClient.send(new lib_dynamodb_1.DeleteCommand(params));
|
|
96
|
+
this.logger.debug(`DeleteItem Result: ${JSON.stringify(response)}`);
|
|
87
97
|
}
|
|
88
98
|
catch (error) {
|
|
89
99
|
this.logger.error(error.message);
|
|
@@ -96,8 +106,11 @@ class DynamoLib {
|
|
|
96
106
|
TableName: params.tableName,
|
|
97
107
|
KeyConditionExpression: "",
|
|
98
108
|
ExpressionAttributeNames: projection?.expressionAttributeNames || {},
|
|
99
|
-
ExpressionAttributeValues: {}
|
|
109
|
+
ExpressionAttributeValues: {},
|
|
100
110
|
};
|
|
111
|
+
if (projection) {
|
|
112
|
+
parameters.ProjectionExpression = projection.projectionExpression;
|
|
113
|
+
}
|
|
101
114
|
if (params.searchParameters.exclusiveStartKey) {
|
|
102
115
|
parameters.ExclusiveStartKey = params.searchParameters.exclusiveStartKey;
|
|
103
116
|
}
|
|
@@ -132,7 +145,8 @@ class DynamoLib {
|
|
|
132
145
|
break;
|
|
133
146
|
case "BETWEEN":
|
|
134
147
|
params.KeyConditionExpression += `#key${index} ${searchParameter.operator} :value${index} AND :value1${index}`;
|
|
135
|
-
params.ExpressionAttributeValues[`:value1${index}`] =
|
|
148
|
+
params.ExpressionAttributeValues[`:value1${index}`] =
|
|
149
|
+
searchParameter.value1;
|
|
136
150
|
break;
|
|
137
151
|
case "FILTER":
|
|
138
152
|
this.buildFilterExpression(searchParameter, index, params);
|
|
@@ -146,14 +160,14 @@ class DynamoLib {
|
|
|
146
160
|
}
|
|
147
161
|
buildFilterExpression(searchParameter, index, params) {
|
|
148
162
|
if (params.FilterExpression)
|
|
149
|
-
params.FilterExpression +=
|
|
163
|
+
params.FilterExpression += " and ";
|
|
150
164
|
else
|
|
151
|
-
params.FilterExpression =
|
|
165
|
+
params.FilterExpression = "";
|
|
152
166
|
switch (searchParameter.filterOperator) {
|
|
153
|
-
case
|
|
167
|
+
case "contains":
|
|
154
168
|
params.FilterExpression += `contains (#key${index}, :value${index})`;
|
|
155
169
|
break;
|
|
156
|
-
case
|
|
170
|
+
case "begins_with":
|
|
157
171
|
params.FilterExpression += `${searchParameter.filterOperator}(#key${index}, :value${index})`;
|
|
158
172
|
break;
|
|
159
173
|
case "attribute_not_exists":
|
|
@@ -189,7 +203,7 @@ class DynamoLib {
|
|
|
189
203
|
for (let i = 0; i < executions.length; i++) {
|
|
190
204
|
const params = {
|
|
191
205
|
RequestItems: {
|
|
192
|
-
[table]: executions[i]
|
|
206
|
+
[table]: executions[i],
|
|
193
207
|
},
|
|
194
208
|
ReturnConsumedCapacity: "TOTAL",
|
|
195
209
|
};
|
|
@@ -248,10 +262,12 @@ class DynamoLib {
|
|
|
248
262
|
return executions;
|
|
249
263
|
}
|
|
250
264
|
createUpdateParams(params) {
|
|
251
|
-
const { table, PK, SK, item, setAttributes, addAttributes, removeAttributes, username } = params;
|
|
265
|
+
const { table, PK, SK, item, setAttributes, addAttributes, removeAttributes, username, } = params;
|
|
252
266
|
// Se agregan los datos de auditoría para la actualización
|
|
253
267
|
item.updateUser = username;
|
|
254
|
-
item.updateDate = moment_timezone_1.default
|
|
268
|
+
item.updateDate = moment_timezone_1.default
|
|
269
|
+
.tz(new Date(), "America/Bogota")
|
|
270
|
+
.format("YYYY-MM-DD");
|
|
255
271
|
if (!setAttributes.includes("updateUser"))
|
|
256
272
|
setAttributes.push("updateUser");
|
|
257
273
|
if (!setAttributes.includes("updateDate"))
|
|
@@ -280,7 +296,8 @@ class DynamoLib {
|
|
|
280
296
|
this.formatUpdateExpressionRemove(itemUpdate, removeAttributes);
|
|
281
297
|
}
|
|
282
298
|
// Se remueve el espacio al final de la expresión
|
|
283
|
-
itemUpdate.Update.UpdateExpression =
|
|
299
|
+
itemUpdate.Update.UpdateExpression =
|
|
300
|
+
itemUpdate.Update.UpdateExpression.slice(0, -1);
|
|
284
301
|
return itemUpdate;
|
|
285
302
|
}
|
|
286
303
|
formatUpdateExpression(operation, updateObject, item, atributes) {
|
|
@@ -290,14 +307,18 @@ class DynamoLib {
|
|
|
290
307
|
let operationTemplate = templatesUpdateExpresion[operation];
|
|
291
308
|
// Se añaden los items afectados por la operación.
|
|
292
309
|
for (let atribute of atributes) {
|
|
293
|
-
if (typeof item[atribute] === "boolean" ||
|
|
310
|
+
if (typeof item[atribute] === "boolean" ||
|
|
311
|
+
typeof item[atribute] === "number" ||
|
|
312
|
+
item[atribute]) {
|
|
294
313
|
updateObject.Update.UpdateExpression += operationTemplate(atribute);
|
|
295
314
|
updateObject.Update.ExpressionAttributeNames[`#${atribute}`] = atribute;
|
|
296
|
-
updateObject.Update.ExpressionAttributeValues[`:${atribute}`] =
|
|
315
|
+
updateObject.Update.ExpressionAttributeValues[`:${atribute}`] =
|
|
316
|
+
item[atribute];
|
|
297
317
|
}
|
|
298
318
|
}
|
|
299
319
|
// Se remueve la coma final para evitar errores con el SDK.
|
|
300
|
-
updateObject.Update.UpdateExpression =
|
|
320
|
+
updateObject.Update.UpdateExpression =
|
|
321
|
+
updateObject.Update.UpdateExpression.slice(0, -1);
|
|
301
322
|
// Se añade un espacio para poder agregar nuevas operaciones.
|
|
302
323
|
updateObject.Update.UpdateExpression += " ";
|
|
303
324
|
}
|
|
@@ -313,7 +334,8 @@ class DynamoLib {
|
|
|
313
334
|
updateObject.Update.ExpressionAttributeNames[`#${atribute}`] = atribute;
|
|
314
335
|
}
|
|
315
336
|
// Se remueve la coma final para evitar errores con el SDK.
|
|
316
|
-
updateObject.Update.UpdateExpression =
|
|
337
|
+
updateObject.Update.UpdateExpression =
|
|
338
|
+
updateObject.Update.UpdateExpression.slice(0, -1);
|
|
317
339
|
// Se añade un espacio para poder agregar nuevas operaciones.
|
|
318
340
|
updateObject.Update.UpdateExpression += " ";
|
|
319
341
|
}
|
|
@@ -327,7 +349,9 @@ class DynamoLib {
|
|
|
327
349
|
entity: "COUN",
|
|
328
350
|
order: 0,
|
|
329
351
|
creationUser: username,
|
|
330
|
-
creationDate: moment_timezone_1.default
|
|
352
|
+
creationDate: moment_timezone_1.default
|
|
353
|
+
.tz(new Date(), "America/Bogota")
|
|
354
|
+
.format("YYYY-MM-DD"),
|
|
331
355
|
};
|
|
332
356
|
await this.putItem(table, item);
|
|
333
357
|
}
|
|
@@ -337,7 +361,7 @@ class DynamoLib {
|
|
|
337
361
|
UpdateExpression: `SET #id = #id + :increment`,
|
|
338
362
|
ExpressionAttributeNames: { "#id": "order" },
|
|
339
363
|
ExpressionAttributeValues: { ":increment": quantity || 1 },
|
|
340
|
-
ReturnValues: "UPDATED_OLD"
|
|
364
|
+
ReturnValues: "UPDATED_OLD",
|
|
341
365
|
};
|
|
342
366
|
this.logger.debug(`GetId: ${JSON.stringify(params)}`);
|
|
343
367
|
const result = await this.docClient.send(new lib_dynamodb_1.UpdateCommand(params));
|
|
@@ -358,10 +382,17 @@ class DynamoLib {
|
|
|
358
382
|
indexName: "GSI2",
|
|
359
383
|
parameters: [
|
|
360
384
|
{ name: "entity", value: "PERM", operator: "=" },
|
|
361
|
-
{
|
|
385
|
+
{
|
|
386
|
+
name: "relation2",
|
|
387
|
+
value: `${profile}|${entity}|${action}`,
|
|
388
|
+
operator: "=",
|
|
389
|
+
},
|
|
362
390
|
],
|
|
363
391
|
};
|
|
364
|
-
let permissionQuery = await this.query({
|
|
392
|
+
let permissionQuery = await this.query({
|
|
393
|
+
tableName: table,
|
|
394
|
+
searchParameters,
|
|
395
|
+
});
|
|
365
396
|
if (permissionQuery && permissionQuery.length > 0) {
|
|
366
397
|
result = permissionQuery[0];
|
|
367
398
|
return result.type === "ALL" ? "ALL" : "OWNS";
|