ag-common 0.0.725 → 0.0.726

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.
@@ -16,6 +16,15 @@ interface DynamoFilter {
16
16
  attrValues?: Record<string, unknown>;
17
17
  }
18
18
  interface ScanOptions {
19
+ /** eg
20
+ * filter: {
21
+ filterExpression: '#feedIcon = :empty',
22
+ attrNames: { '#feedIcon': 'feedIcon' },
23
+ attrValues: {
24
+ ':empty': '',
25
+ },
26
+ },
27
+ */
19
28
  filter?: DynamoFilter;
20
29
  requiredAttributeList?: string[];
21
30
  limit?: number;
@@ -48,6 +57,7 @@ export declare const batchDelete: (params: {
48
57
  pkName: string;
49
58
  }) => Promise<DynamoDBResult<void>>;
50
59
  export declare const scan: <T>(tableName: string, options?: ScanOptions) => Promise<DynamoDBResult<T[]>>;
60
+ export declare function scanWithGenerator<T>(tableName: string, options?: ScanOptions): AsyncGenerator<T[], void, unknown>;
51
61
  export declare const getItemsDynamo: <T>(params: {
52
62
  tableName: string;
53
63
  items: {
@@ -8,8 +8,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
12
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
13
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
14
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
15
+ return i = {}, verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
16
+ function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
17
+ function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
18
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
19
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
20
+ function fulfill(value) { resume("next", value); }
21
+ function reject(value) { resume("throw", value); }
22
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
23
+ };
11
24
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.getDynamoUpdates = exports.wipeTable = exports.getDynamoTtlMinutes = exports.getDynamoTtlDays = exports.queryDynamo = exports.getItemDynamo = exports.getItemsDynamo = exports.scan = exports.batchDelete = exports.batchWrite = exports.putDynamo = exports.setDynamo = exports.dynamoDb = void 0;
25
+ exports.getDynamoUpdates = exports.wipeTable = exports.getDynamoTtlMinutes = exports.getDynamoTtlDays = exports.queryDynamo = exports.getItemDynamo = exports.getItemsDynamo = exports.scanWithGenerator = exports.scan = exports.batchDelete = exports.batchWrite = exports.putDynamo = exports.setDynamo = exports.dynamoDb = void 0;
13
26
  const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
14
27
  const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb");
15
28
  const array_1 = require("../../common/helpers/array");
@@ -148,6 +161,68 @@ const scan = (tableName, options) => __awaiter(void 0, void 0, void 0, function*
148
161
  }
149
162
  });
150
163
  exports.scan = scan;
164
+ function scanWithGenerator(tableName, options) {
165
+ return __asyncGenerator(this, arguments, function* scanWithGenerator_1() {
166
+ var _a, _b;
167
+ const BATCH_SIZE = 25;
168
+ let items = [];
169
+ let exclusiveStartKey;
170
+ let totalItems = 0;
171
+ try {
172
+ const projectionAttrs = (_a = options === null || options === void 0 ? void 0 : options.requiredAttributeList) === null || _a === void 0 ? void 0 : _a.reduce((acc, attr, index) => {
173
+ acc[`#proj${index}`] = attr;
174
+ return acc;
175
+ }, {});
176
+ 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);
177
+ do {
178
+ const params = new lib_dynamodb_1.ScanCommand(Object.assign(Object.assign(Object.assign(Object.assign({ TableName: tableName, Limit: BATCH_SIZE }, ((options === null || options === void 0 ? void 0 : options.filter) && Object.assign({ FilterExpression: options.filter.filterExpression }, (options.filter.attrValues && {
179
+ ExpressionAttributeValues: options.filter.attrValues,
180
+ })))), (Object.keys(expressionAttributeNames).length > 0 && {
181
+ ExpressionAttributeNames: expressionAttributeNames,
182
+ })), ((options === null || options === void 0 ? void 0 : options.requiredAttributeList) && {
183
+ ProjectionExpression: options.requiredAttributeList
184
+ .map((_, index) => `#proj${index}`)
185
+ .join(', '),
186
+ })), { ExclusiveStartKey: exclusiveStartKey }));
187
+ const result = yield __await(withRetry(() => exports.dynamoDb.send(params), 'scanWithGenerator'));
188
+ if (result.Items) {
189
+ items.push(...result.Items);
190
+ // Process items in chunks of BATCH_SIZE
191
+ while (items.length >= BATCH_SIZE) {
192
+ const batch = items.slice(0, BATCH_SIZE);
193
+ items = items.slice(BATCH_SIZE);
194
+ totalItems += batch.length;
195
+ // If we've reached the limit, yield the final batch and return
196
+ if ((options === null || options === void 0 ? void 0 : options.limit) && totalItems >= options.limit) {
197
+ const remainingCount = options.limit - (totalItems - batch.length);
198
+ yield yield __await(batch.slice(0, remainingCount));
199
+ return yield __await(void 0);
200
+ }
201
+ yield yield __await(batch);
202
+ }
203
+ }
204
+ exclusiveStartKey = result.LastEvaluatedKey;
205
+ } while (exclusiveStartKey &&
206
+ (!(options === null || options === void 0 ? void 0 : options.limit) || totalItems < options.limit));
207
+ // Yield any remaining items
208
+ if (items.length > 0) {
209
+ if (options === null || options === void 0 ? void 0 : options.limit) {
210
+ const remainingCount = options.limit - totalItems;
211
+ if (remainingCount > 0) {
212
+ yield yield __await(items.slice(0, remainingCount));
213
+ }
214
+ }
215
+ else {
216
+ yield yield __await(items);
217
+ }
218
+ }
219
+ }
220
+ catch (e) {
221
+ throw new Error(`Scan generator error: ${e.toString()}`);
222
+ }
223
+ });
224
+ }
225
+ exports.scanWithGenerator = scanWithGenerator;
151
226
  const getItemsDynamo = (params) => __awaiter(void 0, void 0, void 0, function* () {
152
227
  var _c, _d;
153
228
  try {
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.725",
2
+ "version": "0.0.726",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",