inlaweb-lib-dynamodb 1.0.2 → 1.0.4

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/index.js CHANGED
@@ -57,14 +57,20 @@ class DynamoLib {
57
57
  return true;
58
58
  }
59
59
  async query(params) {
60
- const parameters = this.buildQueryParams(params);
61
- this.logger.debug(`Query: ${JSON.stringify(parameters)}`);
62
- const response = await this.docClient.send(new lib_dynamodb_1.QueryCommand(parameters));
63
- this.logger.debug(`Query Result: ${JSON.stringify(response)}`);
64
- if (response.Items) {
65
- return response.Items;
60
+ try {
61
+ const parameters = this.buildQueryParams(params);
62
+ this.logger.debug(`Query: ${JSON.stringify(parameters)}`);
63
+ const response = await this.docClient.send(new lib_dynamodb_1.QueryCommand(parameters));
64
+ this.logger.debug(`Query Result: ${JSON.stringify(response)}`);
65
+ if (response.Items) {
66
+ return response.Items;
67
+ }
68
+ return;
69
+ }
70
+ catch (error) {
71
+ this.logger.error(error.message);
72
+ throw error;
66
73
  }
67
- return;
68
74
  }
69
75
  buildQueryParams(params) {
70
76
  const projection = this.buildProjectionExpression(params.searchParameters.projectionExpression);
@@ -74,6 +80,18 @@ class DynamoLib {
74
80
  ExpressionAttributeNames: projection?.expressionAttributeNames || {},
75
81
  ExpressionAttributeValues: {}
76
82
  };
83
+ if (params.searchParameters.exclusiveStartKey) {
84
+ parameters.ExclusiveStartKey = params.searchParameters.exclusiveStartKey;
85
+ }
86
+ if (params.searchParameters.limit) {
87
+ parameters.Limit = params.searchParameters.limit;
88
+ }
89
+ if (params.searchParameters.scanIndexForward) {
90
+ parameters.ScanIndexForward = params.searchParameters.scanIndexForward;
91
+ }
92
+ if (params.searchParameters.indexName !== "") {
93
+ parameters.IndexName = params.searchParameters.indexName;
94
+ }
77
95
  if (params.permission && params.permission === "OWNS") {
78
96
  params.searchParameters.parameters.push({
79
97
  name: "creationUser",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inlaweb-lib-dynamodb",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -62,14 +62,20 @@ export class DynamoLib {
62
62
  }
63
63
 
64
64
  async query<T>(params: {tableName: string, searchParameters: SearchParameters, permission?: string, username?: string}): Promise<T[] | undefined> {
65
- const parameters: ParamsQuery = this.buildQueryParams(params);
66
- this.logger.debug(`Query: ${JSON.stringify(parameters)}`);
67
- const response = await this.docClient.send(new QueryCommand(parameters));
68
- this.logger.debug(`Query Result: ${JSON.stringify(response)}`);
69
- if(response.Items) {
70
- return response.Items as T[];
65
+ try {
66
+ const parameters: ParamsQuery = this.buildQueryParams(params);
67
+ this.logger.debug(`Query: ${JSON.stringify(parameters)}`);
68
+ const response = await this.docClient.send(new QueryCommand(parameters));
69
+ this.logger.debug(`Query Result: ${JSON.stringify(response)}`);
70
+ if(response.Items) {
71
+ return response.Items as T[];
72
+ }
73
+ return;
74
+ } catch (error) {
75
+ this.logger.error((error as Error).message);
76
+ throw error;
71
77
  }
72
- return;
78
+
73
79
  }
74
80
 
75
81
  private buildQueryParams(params: {tableName: string, searchParameters: SearchParameters, permission?: string, username?: string}): ParamsQuery {
@@ -81,6 +87,19 @@ export class DynamoLib {
81
87
  ExpressionAttributeNames: projection?.expressionAttributeNames || {},
82
88
  ExpressionAttributeValues: {}
83
89
  };
90
+
91
+ if (params.searchParameters.exclusiveStartKey) {
92
+ parameters.ExclusiveStartKey = params.searchParameters.exclusiveStartKey;
93
+ }
94
+ if (params.searchParameters.limit) {
95
+ parameters.Limit = params.searchParameters.limit;
96
+ }
97
+ if (params.searchParameters.scanIndexForward) {
98
+ parameters.ScanIndexForward = params.searchParameters.scanIndexForward;
99
+ }
100
+ if (params.searchParameters.indexName !== "") {
101
+ parameters.IndexName = params.searchParameters.indexName;
102
+ }
84
103
 
85
104
  if (params.permission && params.permission === "OWNS") {
86
105
  params.searchParameters.parameters.push({
@@ -102,8 +102,8 @@ describe('DynamoLib', () => {
102
102
 
103
103
  const result = await dynamoLib.query({tableName, searchParameters});
104
104
 
105
- expect(QueryCommand).toHaveBeenCalledWith({"ExpressionAttributeNames": {"#key0": "entity", "#key1": "filter1"}, "ExpressionAttributeValues": {":value0": "entity", ":value1": "filter1"}, "KeyConditionExpression": "#key0 = :value0 and #key1 = :value1", "TableName": "testTable"});
106
- expect(mockSend).toHaveBeenCalledWith({"ExpressionAttributeNames": {"#key0": "entity", "#key1": "filter1"}, "ExpressionAttributeValues": {":value0": "entity", ":value1": "filter1"}, "KeyConditionExpression": "#key0 = :value0 and #key1 = :value1", "TableName": "testTable"});
105
+ expect(QueryCommand).toHaveBeenCalledWith({"ExpressionAttributeNames": {"#key0": "entity", "#key1": "filter1"}, "ExpressionAttributeValues": {":value0": "entity", ":value1": "filter1"}, "IndexName": "GSI2", "KeyConditionExpression": "#key0 = :value0 and #key1 = :value1", "TableName": "testTable"});
106
+ expect(mockSend).toHaveBeenCalledWith({"ExpressionAttributeNames": {"#key0": "entity", "#key1": "filter1"}, "ExpressionAttributeValues": {":value0": "entity", ":value1": "filter1"}, "IndexName": "GSI2", "KeyConditionExpression": "#key0 = :value0 and #key1 = :value1", "TableName": "testTable"});
107
107
  expect(result).toEqual(items);
108
108
  });
109
109
  it('should call QueryCommand with filterexpressions', async () => {
@@ -116,7 +116,7 @@ describe('DynamoLib', () => {
116
116
 
117
117
  await dynamoLib.query({tableName, searchParameters});
118
118
 
119
- expect(QueryCommand).toHaveBeenCalledWith({"ExpressionAttributeNames": {"#key0": "entity", "#key1": "filter1", "#key2": "filter2", "#key3": "filter3", "#key4": "filter4", "#key5": "filter5"}, "ExpressionAttributeValues": {":value0": "entity", ":value1": "filter1", ":value2": "filter", ":value3": "filter3", ":value4": "filter4", ":value5": "filter5"}, "FilterExpression": "#key1 = :value1 and contains (#key2, :value2) and begins_with(#key3, :value3) and attribute_exists (#key4) and attribute_not_exists (#key5)", "KeyConditionExpression": "#key0 = :value0", "TableName": "testTable"});
119
+ expect(QueryCommand).toHaveBeenCalledWith({"ExpressionAttributeNames": {"#key0": "entity", "#key1": "filter1", "#key2": "filter2", "#key3": "filter3", "#key4": "filter4", "#key5": "filter5"}, "ExpressionAttributeValues": {":value0": "entity", ":value1": "filter1", ":value2": "filter", ":value3": "filter3", ":value4": "filter4", ":value5": "filter5"}, "IndexName": "GSI2", "FilterExpression": "#key1 = :value1 and contains (#key2, :value2) and begins_with(#key3, :value3) and attribute_exists (#key4) and attribute_not_exists (#key5)", "KeyConditionExpression": "#key0 = :value0", "TableName": "testTable"});
120
120
  });
121
121
  it('should call QueryCommand with filterexpression for own registers', async () => {
122
122
  const permission = 'OWNS';
@@ -125,7 +125,7 @@ describe('DynamoLib', () => {
125
125
 
126
126
  await dynamoLib.query({tableName, searchParameters, permission, username});
127
127
 
128
- expect(QueryCommand).toHaveBeenCalledWith({"ExpressionAttributeNames": {"#key0": "entity", "#key1": "creationUser"}, "ExpressionAttributeValues": {":value0": "entity", ":value1": "testUser"}, "FilterExpression": "#key1 = :value1", "KeyConditionExpression": "#key0 = :value0", "TableName": "testTable"});
128
+ expect(QueryCommand).toHaveBeenCalledWith({"ExpressionAttributeNames": {"#key0": "entity", "#key1": "creationUser"}, "ExpressionAttributeValues": {":value0": "entity", ":value1": "testUser"}, "IndexName": "GSI2", "FilterExpression": "#key1 = :value1", "KeyConditionExpression": "#key0 = :value0", "TableName": "testTable"});
129
129
  });
130
130
  it('should call QueryCommand with keyCondition begins_with', async () => {
131
131
  searchParameters.parameters[0].operator = "begins_with";
@@ -133,7 +133,7 @@ describe('DynamoLib', () => {
133
133
 
134
134
  await dynamoLib.query({tableName, searchParameters});
135
135
 
136
- expect(QueryCommand).toHaveBeenCalledWith({"ExpressionAttributeNames": {"#key0": "entity"}, "ExpressionAttributeValues": {":value0": "entity"}, "KeyConditionExpression": "begins_with(#key0, :value0)", "TableName": "testTable"});
136
+ expect(QueryCommand).toHaveBeenCalledWith({"ExpressionAttributeNames": {"#key0": "entity"}, "ExpressionAttributeValues": {":value0": "entity"}, "IndexName": "GSI2", "KeyConditionExpression": "begins_with(#key0, :value0)", "TableName": "testTable"});
137
137
  });
138
138
  it('should call QueryCommand with keyCondition between', async () => {
139
139
  searchParameters.parameters[0].operator = "BETWEEN";
@@ -143,7 +143,17 @@ describe('DynamoLib', () => {
143
143
 
144
144
  await dynamoLib.query({tableName, searchParameters});
145
145
 
146
- expect(QueryCommand).toHaveBeenCalledWith({"ExpressionAttributeNames": {"#key0": "entity"}, "ExpressionAttributeValues": {":value0": "2025-01-01", ":value10": "2025-01-31"}, "KeyConditionExpression": "#key0 BETWEEN :value0 AND :value10", "TableName": "testTable"});
146
+ expect(QueryCommand).toHaveBeenCalledWith({"ExpressionAttributeNames": {"#key0": "entity"}, "ExpressionAttributeValues": {":value0": "2025-01-01", ":value10": "2025-01-31"}, "IndexName": "GSI2", "KeyConditionExpression": "#key0 BETWEEN :value0 AND :value10", "TableName": "testTable"});
147
+ });
148
+ it('should call QueryCommand with optional parameters', async () => {
149
+ searchParameters.exclusiveStartKey = {"PK": "PK", "SK": "SK"};
150
+ searchParameters.limit = 10;
151
+ searchParameters.scanIndexForward = true;
152
+ mockSend.mockResolvedValue({ Items: items });
153
+
154
+ await dynamoLib.query({tableName, searchParameters});
155
+
156
+ expect(QueryCommand).toHaveBeenCalledWith({"ExclusiveStartKey": {"PK": "PK", "SK": "SK"}, "ExpressionAttributeNames": {"#key0": "entity"}, "ExpressionAttributeValues": {":value0": "entity"}, "IndexName": "GSI2", "KeyConditionExpression": "#key0 = :value0", "Limit": 10, "ScanIndexForward": true, "TableName": "testTable"});
147
157
  });
148
158
  it('should return undefined if items is not found', async () => {
149
159
 
@@ -153,6 +163,11 @@ describe('DynamoLib', () => {
153
163
 
154
164
  expect(result).toEqual(undefined);
155
165
  });
166
+ it('should throw error', async () => {
167
+ mockSend.mockRejectedValueOnce(new Error("error"));
168
+
169
+ await expect(dynamoLib.query({tableName, searchParameters})).rejects.toThrow('error');
170
+ });
156
171
  });
157
172
 
158
173
  describe('saveBatch', () => {