inlaweb-lib-dynamodb 1.0.5 → 1.0.6

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.
Files changed (3) hide show
  1. package/index.d.ts +2 -2
  2. package/index.js +41 -26
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -5,14 +5,14 @@ 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 | undefined>;
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[] | undefined>;
15
+ }): Promise<T[]>;
16
16
  private buildQueryParams;
17
17
  private buildExpressions;
18
18
  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
- if (response.Item) {
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,7 @@ 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
- if (response.Items) {
84
- return response.Items;
85
- }
86
- return;
80
+ return response.Items;
87
81
  }
88
82
  catch (error) {
89
83
  this.logger.error(error.message);
@@ -96,8 +90,11 @@ class DynamoLib {
96
90
  TableName: params.tableName,
97
91
  KeyConditionExpression: "",
98
92
  ExpressionAttributeNames: projection?.expressionAttributeNames || {},
99
- ExpressionAttributeValues: {}
93
+ ExpressionAttributeValues: {},
100
94
  };
95
+ if (projection) {
96
+ parameters.ProjectionExpression = projection.projectionExpression;
97
+ }
101
98
  if (params.searchParameters.exclusiveStartKey) {
102
99
  parameters.ExclusiveStartKey = params.searchParameters.exclusiveStartKey;
103
100
  }
@@ -132,7 +129,8 @@ class DynamoLib {
132
129
  break;
133
130
  case "BETWEEN":
134
131
  params.KeyConditionExpression += `#key${index} ${searchParameter.operator} :value${index} AND :value1${index}`;
135
- params.ExpressionAttributeValues[`:value1${index}`] = searchParameter.value1;
132
+ params.ExpressionAttributeValues[`:value1${index}`] =
133
+ searchParameter.value1;
136
134
  break;
137
135
  case "FILTER":
138
136
  this.buildFilterExpression(searchParameter, index, params);
@@ -146,14 +144,14 @@ class DynamoLib {
146
144
  }
147
145
  buildFilterExpression(searchParameter, index, params) {
148
146
  if (params.FilterExpression)
149
- params.FilterExpression += ' and ';
147
+ params.FilterExpression += " and ";
150
148
  else
151
- params.FilterExpression = '';
149
+ params.FilterExpression = "";
152
150
  switch (searchParameter.filterOperator) {
153
- case 'contains':
151
+ case "contains":
154
152
  params.FilterExpression += `contains (#key${index}, :value${index})`;
155
153
  break;
156
- case 'begins_with':
154
+ case "begins_with":
157
155
  params.FilterExpression += `${searchParameter.filterOperator}(#key${index}, :value${index})`;
158
156
  break;
159
157
  case "attribute_not_exists":
@@ -189,7 +187,7 @@ class DynamoLib {
189
187
  for (let i = 0; i < executions.length; i++) {
190
188
  const params = {
191
189
  RequestItems: {
192
- [table]: executions[i]
190
+ [table]: executions[i],
193
191
  },
194
192
  ReturnConsumedCapacity: "TOTAL",
195
193
  };
@@ -248,10 +246,12 @@ class DynamoLib {
248
246
  return executions;
249
247
  }
250
248
  createUpdateParams(params) {
251
- const { table, PK, SK, item, setAttributes, addAttributes, removeAttributes, username } = params;
249
+ const { table, PK, SK, item, setAttributes, addAttributes, removeAttributes, username, } = params;
252
250
  // Se agregan los datos de auditoría para la actualización
253
251
  item.updateUser = username;
254
- item.updateDate = moment_timezone_1.default.tz(new Date(), "America/Bogota").format("YYYY-MM-DD");
252
+ item.updateDate = moment_timezone_1.default
253
+ .tz(new Date(), "America/Bogota")
254
+ .format("YYYY-MM-DD");
255
255
  if (!setAttributes.includes("updateUser"))
256
256
  setAttributes.push("updateUser");
257
257
  if (!setAttributes.includes("updateDate"))
@@ -280,7 +280,8 @@ class DynamoLib {
280
280
  this.formatUpdateExpressionRemove(itemUpdate, removeAttributes);
281
281
  }
282
282
  // Se remueve el espacio al final de la expresión
283
- itemUpdate.Update.UpdateExpression = itemUpdate.Update.UpdateExpression.slice(0, -1);
283
+ itemUpdate.Update.UpdateExpression =
284
+ itemUpdate.Update.UpdateExpression.slice(0, -1);
284
285
  return itemUpdate;
285
286
  }
286
287
  formatUpdateExpression(operation, updateObject, item, atributes) {
@@ -290,14 +291,18 @@ class DynamoLib {
290
291
  let operationTemplate = templatesUpdateExpresion[operation];
291
292
  // Se añaden los items afectados por la operación.
292
293
  for (let atribute of atributes) {
293
- if (typeof item[atribute] === "boolean" || typeof item[atribute] === "number" || item[atribute]) {
294
+ if (typeof item[atribute] === "boolean" ||
295
+ typeof item[atribute] === "number" ||
296
+ item[atribute]) {
294
297
  updateObject.Update.UpdateExpression += operationTemplate(atribute);
295
298
  updateObject.Update.ExpressionAttributeNames[`#${atribute}`] = atribute;
296
- updateObject.Update.ExpressionAttributeValues[`:${atribute}`] = item[atribute];
299
+ updateObject.Update.ExpressionAttributeValues[`:${atribute}`] =
300
+ item[atribute];
297
301
  }
298
302
  }
299
303
  // Se remueve la coma final para evitar errores con el SDK.
300
- updateObject.Update.UpdateExpression = updateObject.Update.UpdateExpression.slice(0, -1);
304
+ updateObject.Update.UpdateExpression =
305
+ updateObject.Update.UpdateExpression.slice(0, -1);
301
306
  // Se añade un espacio para poder agregar nuevas operaciones.
302
307
  updateObject.Update.UpdateExpression += " ";
303
308
  }
@@ -313,7 +318,8 @@ class DynamoLib {
313
318
  updateObject.Update.ExpressionAttributeNames[`#${atribute}`] = atribute;
314
319
  }
315
320
  // Se remueve la coma final para evitar errores con el SDK.
316
- updateObject.Update.UpdateExpression = updateObject.Update.UpdateExpression.slice(0, -1);
321
+ updateObject.Update.UpdateExpression =
322
+ updateObject.Update.UpdateExpression.slice(0, -1);
317
323
  // Se añade un espacio para poder agregar nuevas operaciones.
318
324
  updateObject.Update.UpdateExpression += " ";
319
325
  }
@@ -327,7 +333,9 @@ class DynamoLib {
327
333
  entity: "COUN",
328
334
  order: 0,
329
335
  creationUser: username,
330
- creationDate: moment_timezone_1.default.tz(new Date(), "America/Bogota").format("YYYY-MM-DD"),
336
+ creationDate: moment_timezone_1.default
337
+ .tz(new Date(), "America/Bogota")
338
+ .format("YYYY-MM-DD"),
331
339
  };
332
340
  await this.putItem(table, item);
333
341
  }
@@ -337,7 +345,7 @@ class DynamoLib {
337
345
  UpdateExpression: `SET #id = #id + :increment`,
338
346
  ExpressionAttributeNames: { "#id": "order" },
339
347
  ExpressionAttributeValues: { ":increment": quantity || 1 },
340
- ReturnValues: "UPDATED_OLD"
348
+ ReturnValues: "UPDATED_OLD",
341
349
  };
342
350
  this.logger.debug(`GetId: ${JSON.stringify(params)}`);
343
351
  const result = await this.docClient.send(new lib_dynamodb_1.UpdateCommand(params));
@@ -358,10 +366,17 @@ class DynamoLib {
358
366
  indexName: "GSI2",
359
367
  parameters: [
360
368
  { name: "entity", value: "PERM", operator: "=" },
361
- { name: "relation2", value: `${profile}|${entity}|${action}`, operator: "=" },
369
+ {
370
+ name: "relation2",
371
+ value: `${profile}|${entity}|${action}`,
372
+ operator: "=",
373
+ },
362
374
  ],
363
375
  };
364
- let permissionQuery = await this.query({ tableName: table, searchParameters });
376
+ let permissionQuery = await this.query({
377
+ tableName: table,
378
+ searchParameters,
379
+ });
365
380
  if (permissionQuery && permissionQuery.length > 0) {
366
381
  result = permissionQuery[0];
367
382
  return result.type === "ALL" ? "ALL" : "OWNS";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inlaweb-lib-dynamodb",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "scripts": {