ag-common 0.0.802 → 0.0.804

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.
@@ -53,7 +53,7 @@ const wipeTable = (tableName) => __awaiter(void 0, void 0, void 0, function* ()
53
53
  var _a, e_1, _b, _c;
54
54
  try {
55
55
  const generator = (0, get_1.scanWithGenerator)(tableName, {
56
- BATCH_SIZE: 100, // Process in chunks of 100 items
56
+ limit: 100, // Process in chunks of 100 items
57
57
  maxRetries: null, // Always retry on 429 since we want to ensure complete deletion
58
58
  });
59
59
  try {
@@ -51,11 +51,5 @@ export declare const scan: <T>(tableName: string, options?: ScanOptions) => Prom
51
51
  * @returns Promise resolving to query results or error
52
52
  */
53
53
  export declare const batchQueryDynamo: <T>(params: DynamoBatchQueryParams) => Promise<DynamoDBResult<T[]>>;
54
- export declare function queryWithGenerator<T>(params: DynamoQueryParams & {
55
- /** how many to return in query generator. default 100 */
56
- BATCH_SIZE?: number;
57
- }): AsyncGenerator<T[], void, unknown>;
58
- export declare function scanWithGenerator<T>(tableName: string, options?: ScanOptions & {
59
- /** how many to return in scan generator. default 100 */
60
- BATCH_SIZE?: number;
61
- }): AsyncGenerator<T[], void, unknown>;
54
+ export declare function queryWithGenerator<T>(params: DynamoQueryParams): AsyncGenerator<T[], void, unknown>;
55
+ export declare function scanWithGenerator<T>(tableName: string, options?: ScanOptions): AsyncGenerator<T[], void, unknown>;
@@ -33,7 +33,7 @@ const isError = (result) => 'error' in result;
33
33
  * Helper function that builds the query parameters and executes the query
34
34
  */
35
35
  const executeQuery = (params, exclusiveStartKey) => __awaiter(void 0, void 0, void 0, function* () {
36
- var _a, _b, _c, _d, _e;
36
+ var _a, _b, _c, _d;
37
37
  let kce = `#${params.pkName.toLowerCase()} ${(_a = params.pkOperator) !== null && _a !== void 0 ? _a : '='} :${params.pkName.toLowerCase()}`;
38
38
  const ean = {
39
39
  [`#${params.pkName.toLowerCase()}`]: params.pkName,
@@ -61,7 +61,7 @@ const executeQuery = (params, exclusiveStartKey) => __awaiter(void 0, void 0, vo
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: exclusiveStartKey }, (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: params.limit === -1 ? undefined : 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), {
@@ -78,7 +78,7 @@ const executeScan = (tableName, options, exclusiveStartKey) => __awaiter(void 0,
78
78
  return acc;
79
79
  }, {});
80
80
  const expressionAttributeNames = Object.assign(Object.assign({}, projectionAttrs), (_b = options === null || options === void 0 ? void 0 : options.filter) === null || _b === void 0 ? void 0 : _b.attrNames);
81
- const scanParams = Object.assign(Object.assign(Object.assign(Object.assign({ TableName: tableName, IndexName: options === null || options === void 0 ? void 0 : options.indexName, Limit: options === null || options === void 0 ? void 0 : options.BATCH_SIZE }, ((options === null || options === void 0 ? void 0 : options.filter) && Object.assign({ FilterExpression: options.filter.filterExpression }, (options.filter.attrValues && {
81
+ const scanParams = Object.assign(Object.assign(Object.assign(Object.assign({ TableName: tableName, IndexName: options === null || options === void 0 ? void 0 : options.indexName, Limit: (options === null || options === void 0 ? void 0 : options.limit) === -1 ? undefined : options === null || options === void 0 ? void 0 : options.limit }, ((options === null || options === void 0 ? void 0 : options.filter) && Object.assign({ FilterExpression: options.filter.filterExpression }, (options.filter.attrValues && {
82
82
  ExpressionAttributeValues: options.filter.attrValues,
83
83
  })))), (Object.keys(expressionAttributeNames).length > 0 && {
84
84
  ExpressionAttributeNames: expressionAttributeNames,
@@ -128,24 +128,25 @@ const queryDynamo = (params) => __awaiter(void 0, void 0, void 0, function* () {
128
128
  try {
129
129
  const items = [];
130
130
  let lastEvaluatedKey = params.exclusiveStartKey;
131
+ const hasLimit = params.limit && params.limit > 0;
131
132
  do {
132
133
  const result = yield executeQuery(params, lastEvaluatedKey);
133
134
  if (result.Items) {
134
135
  items.push(...result.Items);
135
136
  }
136
137
  lastEvaluatedKey = result.LastEvaluatedKey;
137
- // If we have a limit and we've reached it, stop processing
138
- if (params.limit && items.length >= params.limit) {
138
+ // If limit is undefined, we stop after one query (default to single page).
139
+ // This satisfies: "other never endlessly query" (unless limit is -1).
140
+ if (params.limit === undefined || params.limit === null) {
141
+ break;
142
+ }
143
+ // If limit is > 0 and we've reached it, stop processing
144
+ if (hasLimit && items.length >= params.limit) {
139
145
  return {
140
146
  data: items.slice(0, params.limit),
141
147
  lastEvaluatedKey,
142
148
  };
143
149
  }
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
150
  } while (lastEvaluatedKey && Object.keys(lastEvaluatedKey).length > 0);
150
151
  return { data: items, lastEvaluatedKey };
151
152
  }
@@ -164,8 +165,20 @@ const scan = (tableName, options) => __awaiter(void 0, void 0, void 0, function*
164
165
  Items.push(...result.Items);
165
166
  }
166
167
  ExclusiveStartKey = result.LastEvaluatedKey;
168
+ // If limit is undefined, we stop after one query (default to single page).
169
+ if ((options === null || options === void 0 ? void 0 : options.limit) === undefined) {
170
+ break;
171
+ }
172
+ if ((options === null || options === void 0 ? void 0 : options.limit) &&
173
+ options.limit > 0 &&
174
+ Items.length >= options.limit) {
175
+ return {
176
+ data: Items.slice(0, options.limit),
177
+ lastEvaluatedKey: ExclusiveStartKey,
178
+ };
179
+ }
167
180
  } while (ExclusiveStartKey);
168
- return { data: Items };
181
+ return { data: Items, lastEvaluatedKey: ExclusiveStartKey };
169
182
  }
170
183
  catch (e) {
171
184
  return { error: e.toString() };
@@ -281,7 +294,7 @@ const executePartiQLQuery = (params) => __awaiter(void 0, void 0, void 0, functi
281
294
  const executeParams = {
282
295
  Statement: statement,
283
296
  Parameters: parameters,
284
- Limit: params.limit,
297
+ Limit: params.limit === -1 ? undefined : params.limit,
285
298
  NextToken: nextToken,
286
299
  };
287
300
  const result = yield (0, withRetry_1.withRetry)(() => _1.dynamoDb.send(new lib_dynamodb_1.ExecuteStatementCommand(executeParams)), 'batchQueryDynamo:' + JSON.stringify(params), {
@@ -309,7 +322,7 @@ const executePartiQLQuery = (params) => __awaiter(void 0, void 0, void 0, functi
309
322
  function queryWithGenerator(params) {
310
323
  return __asyncGenerator(this, arguments, function* queryWithGenerator_1() {
311
324
  var _a;
312
- const BATCH_SIZE = (_a = params.BATCH_SIZE) !== null && _a !== void 0 ? _a : 100;
325
+ const limit = (_a = params.limit) !== null && _a !== void 0 ? _a : 100;
313
326
  let items = [];
314
327
  let lastEvaluatedKey = params.exclusiveStartKey;
315
328
  try {
@@ -318,9 +331,13 @@ function queryWithGenerator(params) {
318
331
  if (result.Items) {
319
332
  items.push(...result.Items);
320
333
  // Process items in chunks of BATCH_SIZE
321
- while (items.length >= BATCH_SIZE) {
322
- const batch = items.slice(0, BATCH_SIZE);
323
- items = items.slice(BATCH_SIZE);
334
+ if (!limit) {
335
+ yield yield __await(items);
336
+ return yield __await(void 0);
337
+ }
338
+ while (items.length >= limit) {
339
+ const batch = items.slice(0, limit);
340
+ items = items.slice(limit);
324
341
  yield yield __await(batch);
325
342
  }
326
343
  }
@@ -339,7 +356,7 @@ function queryWithGenerator(params) {
339
356
  function scanWithGenerator(tableName, options) {
340
357
  return __asyncGenerator(this, arguments, function* scanWithGenerator_1() {
341
358
  var _a;
342
- const BATCH_SIZE = (_a = options === null || options === void 0 ? void 0 : options.BATCH_SIZE) !== null && _a !== void 0 ? _a : 100;
359
+ const limit = (_a = options === null || options === void 0 ? void 0 : options.limit) !== null && _a !== void 0 ? _a : 100;
343
360
  let items = [];
344
361
  let exclusiveStartKey;
345
362
  try {
@@ -347,10 +364,14 @@ function scanWithGenerator(tableName, options) {
347
364
  const result = yield __await(executeScan(tableName, options, exclusiveStartKey));
348
365
  if (result.Items) {
349
366
  items.push(...result.Items);
350
- // Process items in chunks of BATCH_SIZE
351
- while (items.length >= BATCH_SIZE) {
352
- const batch = items.slice(0, BATCH_SIZE);
353
- items = items.slice(BATCH_SIZE);
367
+ // Process items in chunks of limit
368
+ if (!limit) {
369
+ yield yield __await(items);
370
+ return yield __await(void 0);
371
+ }
372
+ while (items.length >= limit) {
373
+ const batch = items.slice(0, limit);
374
+ items = items.slice(limit);
354
375
  yield yield __await(batch);
355
376
  }
356
377
  }
@@ -25,7 +25,17 @@ export interface ScanOptions {
25
25
  filter?: DynamoFilter;
26
26
  requiredAttributeList?: string[];
27
27
  indexName?: string;
28
+ /**
29
+ * - number > 0: Limit the total number of items returned.
30
+ * - -1: Endlessly query until all items are retrieved.
31
+ * - null | undefined: Fetch only a single page (one network request).
32
+ */
33
+ limit?: number;
28
34
  maxRetries?: number | null;
35
+ /**
36
+ * The key to start/resume from.
37
+ */
38
+ exclusiveStartKey?: Key;
29
39
  }
30
40
  export interface DynamoQueryParams {
31
41
  tableName: string;
@@ -36,11 +46,19 @@ export interface DynamoQueryParams {
36
46
  skValue?: string | number | [string | number, string | number];
37
47
  skOperator?: '=' | '<' | '>' | '<=' | '>=' | 'BETWEEN' | 'BEGINS_WITH';
38
48
  indexName?: string;
49
+ /**
50
+ * - number > 0: Limit the total number of items returned.
51
+ * - -1: Endlessly query until all items are retrieved.
52
+ * - null | undefined: Fetch only a single page (one network request).
53
+ */
39
54
  limit?: number;
40
55
  filter?: DynamoFilter;
41
56
  sortAscending?: boolean;
42
57
  /** default 3, set to null to disable retries */
43
58
  maxRetries?: number | null;
59
+ /**
60
+ * The key to start/resume from.
61
+ */
44
62
  exclusiveStartKey?: Key;
45
63
  }
46
64
  export interface DynamoBatchQueryParams {
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.802",
2
+ "version": "0.0.804",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",