@adtrackify/at-service-common 1.1.8 → 1.1.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adtrackify/at-service-common",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "",
5
5
  "module": "./dist/index.js",
6
6
  "main": "./dist/index.js",
@@ -49,6 +49,7 @@
49
49
  "@adtrackify/at-tracking-event-types": "^1.1.2",
50
50
  "@aws-sdk/client-dynamodb": "^3.278.0",
51
51
  "@aws-sdk/client-eventbridge": "^3.278.0",
52
+ "@aws-sdk/lib-dynamodb": "^3.279.0",
52
53
  "@babel/cli": "^7.13.16",
53
54
  "@babel/core": "^7.19.1",
54
55
  "@babel/plugin-proposal-optional-chaining": "^7.13.8",
@@ -1,18 +1,31 @@
1
- import { DynamoDBClient, GetItemCommand, GetItemCommandInput, GetItemCommandOutput, PutItemCommand, PutItemCommandInput, PutItemCommandOutput, DeleteItemCommand, DeleteItemCommandInput, DeleteItemCommandOutput, BatchGetItemCommand, BatchGetItemCommandInput, BatchGetItemCommandOutput, QueryCommand, QueryCommandInput, QueryCommandOutput, ScanCommand, UpdateItemCommand } from '@aws-sdk/client-dynamodb';
1
+ import { DynamoDBClient, QueryOutput } from '@aws-sdk/client-dynamodb';
2
+ import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
2
3
  import * as log from 'lambda-log';
3
- const client = new DynamoDBClient({});
4
+
5
+ const marshallOptions = {
6
+ convertEmptyValues: false,
7
+ removeUndefinedValues: false,
8
+ convertClassInstanceToMap: false
9
+ };
10
+
11
+ const unmarshallOptions = {
12
+ wrapNumbers: false,
13
+ };
14
+
15
+ const translateConfig = { marshallOptions, unmarshallOptions };
16
+ const ddbClient = new DynamoDBClient({});
17
+ const client = DynamoDBDocument.from(ddbClient, translateConfig);
4
18
 
5
19
  export class DynamoDbClient {
6
20
  static safeGet = async (tableName: string, keyName: string, keyValue: any) => {
7
21
  try {
8
- const params: GetItemCommandInput = {
22
+ const params = {
9
23
  TableName: tableName,
10
24
  Key: {
11
25
  [ keyName ]: keyValue
12
26
  }
13
27
  };
14
- const getItemCommand = new GetItemCommand(params);
15
- const res: GetItemCommandOutput = await client.send(getItemCommand);
28
+ const res = await client.get(params);
16
29
  return res?.Item ?? null;
17
30
  } catch (e: any) {
18
31
  log.error(e, { message: 'Dynamo Client Get Failed' });
@@ -22,12 +35,11 @@ export class DynamoDbClient {
22
35
 
23
36
  static safePut = async (tableName: string, data: any) => {
24
37
  try {
25
- const params: PutItemCommandInput = {
38
+ const params = {
26
39
  TableName: tableName,
27
40
  Item: data
28
41
  };
29
- const putItemCommand = new PutItemCommand(params);
30
- const res: PutItemCommandOutput = await client.send(putItemCommand);
42
+ const res = await client.put(params);
31
43
  return res;
32
44
  } catch (e: any) {
33
45
  log.error(e, { message: 'Dynamo failed simplePut' });
@@ -37,14 +49,13 @@ export class DynamoDbClient {
37
49
 
38
50
  static safeDelete = async (tableName: string, keyName: string, keyValue: any) => {
39
51
  try {
40
- const params: DeleteItemCommandInput = {
52
+ const params = {
41
53
  TableName: tableName,
42
54
  Key: {
43
55
  [ keyName ]: keyValue
44
56
  }
45
57
  };
46
- const deleteItemCommand = new DeleteItemCommand(params);
47
- const res: DeleteItemCommandOutput = await client.send(deleteItemCommand);
58
+ const res = await client.delete(params);
48
59
  return res;
49
60
  } catch (e: any) {
50
61
  log.error(e, { message: 'Dynamo failed safeDelete' });
@@ -53,7 +64,7 @@ export class DynamoDbClient {
53
64
  };
54
65
 
55
66
  static safeQueryByGSI = async (tableName: string, gsiName: string, keyName: string, keyValue: any) => {
56
- const query: QueryCommandInput = {
67
+ const query = {
57
68
  TableName: tableName,
58
69
  IndexName: gsiName,
59
70
  KeyConditionExpression: `${keyName} = :value`,
@@ -67,15 +78,13 @@ export class DynamoDbClient {
67
78
 
68
79
  static safeBatchGet = async (tableName: string, keys: any) => {
69
80
  try {
70
- const params: BatchGetItemCommandInput = {
71
- RequestItems: {
72
- [tableName]: {
73
- Keys: keys
74
- }
75
- }
81
+ const params: any = {
82
+ RequestItems: {}
83
+ };
84
+ params.RequestItems[ tableName ] = {
85
+ Keys: keys
76
86
  };
77
- const batchGetItemCommand = new BatchGetItemCommand(params);
78
- const res: BatchGetItemCommandOutput = await client.send(batchGetItemCommand);
87
+ const res = await client.batchGet(params);
79
88
  log.info('batchget res', { batchGetRes: res });
80
89
  if (res?.Responses?.[ tableName ]) {
81
90
  return res?.Responses?.[ tableName ];
@@ -87,16 +96,15 @@ export class DynamoDbClient {
87
96
  }
88
97
  };
89
98
 
90
- static queryAll = async (params: QueryCommandInput) => {
99
+ static queryAll = async (params: any) => {
91
100
  try {
92
101
  log.info('Invoke Query All', { params });
93
- let currentResult: QueryCommandOutput, exclusiveStartKey;
102
+ let currentResult: QueryOutput, exclusiveStartKey;
94
103
  let accumulatedResults: any[] = [];
95
104
  do {
96
105
  params.ExclusiveStartKey = exclusiveStartKey;
97
106
  params.Limit = 200;
98
- const queryCommand = new QueryCommand(params);
99
- currentResult = await client.send(queryCommand);
107
+ currentResult = await client.query(params);
100
108
  if (currentResult.Items) {
101
109
  exclusiveStartKey = currentResult.LastEvaluatedKey;
102
110
  accumulatedResults = [ ...accumulatedResults, ...currentResult.Items ];
@@ -109,11 +117,11 @@ export class DynamoDbClient {
109
117
  }
110
118
  };
111
119
 
112
- static batchGet = (params: any) => client.send(new BatchGetItemCommand(params));
113
- static get = (params: any) => client.send(new GetItemCommand(params));
114
- static put = (params: any) => client.send(new PutItemCommand(params));
115
- static query = (params: any) => client.send(new QueryCommand(params));
116
- static scan = (params: any) => client.send(new ScanCommand(params));
117
- static update = (params: any) => client.send(new UpdateItemCommand(params));
118
- static delete = (params: any) => client.send(new DeleteItemCommand(params));
120
+ static batchGet = (params: any) => client.batchGet(params);
121
+ static get = (params: any) => client.get(params);
122
+ static put = (params: any) => client.put(params);
123
+ static query = (params: any) => client.query(params);
124
+ static scan = (params: any) => client.scan(params);
125
+ static update = (params: any) => client.update(params);
126
+ static delete = (params: any) => client.delete(params);
119
127
  }