ag-common 0.0.800 → 0.0.801

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.
@@ -32,7 +32,7 @@ const isError = (result) => 'error' in result;
32
32
  /**
33
33
  * Helper function that builds the query parameters and executes the query
34
34
  */
35
- const executeQuery = (params, startKey) => __awaiter(void 0, void 0, void 0, function* () {
35
+ const executeQuery = (params, exclusiveStartKey) => __awaiter(void 0, void 0, void 0, function* () {
36
36
  var _a, _b, _c, _d, _e;
37
37
  let kce = `#${params.pkName.toLowerCase()} ${(_a = params.pkOperator) !== null && _a !== void 0 ? _a : '='} :${params.pkName.toLowerCase()}`;
38
38
  const ean = {
@@ -61,7 +61,7 @@ const executeQuery = (params, startKey) => __awaiter(void 0, void 0, void 0, fun
61
61
  eav[`:${skName.toLowerCase()}`] = skValue;
62
62
  }
63
63
  }
64
- const queryParams = Object.assign({ TableName: params.tableName, KeyConditionExpression: kce, ExpressionAttributeNames: Object.assign(Object.assign({}, ean), (_b = params.filter) === null || _b === void 0 ? void 0 : _b.attrNames), ExpressionAttributeValues: Object.assign(Object.assign({}, eav), (_c = params.filter) === null || _c === void 0 ? void 0 : _c.attrValues), ScanIndexForward: (_d = params.sortAscending) !== null && _d !== void 0 ? _d : true, Limit: (_e = params.BATCH_SIZE) !== null && _e !== void 0 ? _e : params.limit, IndexName: params.indexName, ExclusiveStartKey: startKey }, (params.filter && Object.assign({ FilterExpression: params.filter.filterExpression }, (params.filter.attrValues && {
64
+ const queryParams = Object.assign({ TableName: params.tableName, KeyConditionExpression: kce, ExpressionAttributeNames: Object.assign(Object.assign({}, ean), (_b = params.filter) === null || _b === void 0 ? void 0 : _b.attrNames), ExpressionAttributeValues: Object.assign(Object.assign({}, eav), (_c = params.filter) === null || _c === void 0 ? void 0 : _c.attrValues), ScanIndexForward: (_d = params.sortAscending) !== null && _d !== void 0 ? _d : true, Limit: (_e = params.BATCH_SIZE) !== null && _e !== void 0 ? _e : params.limit, IndexName: params.indexName, ExclusiveStartKey: exclusiveStartKey }, (params.filter && Object.assign({ FilterExpression: params.filter.filterExpression }, (params.filter.attrValues && {
65
65
  ExpressionAttributeValues: Object.assign(Object.assign({}, eav), params.filter.attrValues),
66
66
  }))));
67
67
  return (0, withRetry_1.withRetry)(() => _1.dynamoDb.send(new lib_dynamodb_1.QueryCommand(queryParams)), 'queryDynamo:' + JSON.stringify(params), {
@@ -127,21 +127,27 @@ exports.getItemDynamo = getItemDynamo;
127
127
  const queryDynamo = (params) => __awaiter(void 0, void 0, void 0, function* () {
128
128
  try {
129
129
  const items = [];
130
- let startKey;
130
+ let lastEvaluatedKey = params.exclusiveStartKey;
131
131
  do {
132
- const result = yield executeQuery(params, startKey);
132
+ const result = yield executeQuery(params, lastEvaluatedKey);
133
133
  if (result.Items) {
134
134
  items.push(...result.Items);
135
135
  }
136
- startKey = result.LastEvaluatedKey;
136
+ lastEvaluatedKey = result.LastEvaluatedKey;
137
137
  // If we have a limit and we've reached it, stop processing
138
138
  if (params.limit && items.length >= params.limit) {
139
139
  return {
140
140
  data: items.slice(0, params.limit),
141
+ lastEvaluatedKey,
141
142
  };
142
143
  }
143
- } while (startKey && Object.keys(startKey).length > 0);
144
- return { data: items };
144
+ // If a start key was provided but no limit, we don't want to endlessly page
145
+ // so we stop after the first batch
146
+ if (params.exclusiveStartKey && !params.limit) {
147
+ break;
148
+ }
149
+ } while (lastEvaluatedKey && Object.keys(lastEvaluatedKey).length > 0);
150
+ return { data: items, lastEvaluatedKey };
145
151
  }
146
152
  catch (e) {
147
153
  return { error: e.toString() };
@@ -305,10 +311,10 @@ function queryWithGenerator(params) {
305
311
  var _a;
306
312
  const BATCH_SIZE = (_a = params.BATCH_SIZE) !== null && _a !== void 0 ? _a : 100;
307
313
  let items = [];
308
- let startKey;
314
+ let lastEvaluatedKey = params.exclusiveStartKey;
309
315
  try {
310
316
  do {
311
- const result = yield __await(executeQuery(params, startKey));
317
+ const result = yield __await(executeQuery(params, lastEvaluatedKey));
312
318
  if (result.Items) {
313
319
  items.push(...result.Items);
314
320
  // Process items in chunks of BATCH_SIZE
@@ -318,8 +324,8 @@ function queryWithGenerator(params) {
318
324
  yield yield __await(batch);
319
325
  }
320
326
  }
321
- startKey = result.LastEvaluatedKey;
322
- } while (startKey && Object.keys(startKey).length > 0);
327
+ lastEvaluatedKey = result.LastEvaluatedKey;
328
+ } while (lastEvaluatedKey && Object.keys(lastEvaluatedKey).length > 0);
323
329
  // Yield any remaining items
324
330
  if (items.length > 0) {
325
331
  yield yield __await(items);
@@ -1,8 +1,10 @@
1
+ import type { Key } from '../../types';
1
2
  export type DynamoDBError = {
2
3
  error: string;
3
4
  };
4
5
  export type DynamoDBSuccess<T> = {
5
6
  data: T;
7
+ lastEvaluatedKey?: Key;
6
8
  };
7
9
  export type DynamoDBResult<T> = DynamoDBSuccess<T> | DynamoDBError;
8
10
  export interface DynamoFilter {
@@ -39,6 +41,7 @@ export interface DynamoQueryParams {
39
41
  sortAscending?: boolean;
40
42
  /** default 3, set to null to disable retries */
41
43
  maxRetries?: number | null;
44
+ exclusiveStartKey?: Key;
42
45
  }
43
46
  export interface DynamoBatchQueryParams {
44
47
  tableName: string;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.800",
2
+ "version": "0.0.801",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",