ag-common 0.0.725 → 0.0.727

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,9 +16,17 @@ 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
- limit?: number;
22
30
  }
23
31
  interface DynamoQueryParams {
24
32
  tableName: string;
@@ -48,6 +56,10 @@ export declare const batchDelete: (params: {
48
56
  pkName: string;
49
57
  }) => Promise<DynamoDBResult<void>>;
50
58
  export declare const scan: <T>(tableName: string, options?: ScanOptions) => Promise<DynamoDBResult<T[]>>;
59
+ export declare function scanWithGenerator<T>(tableName: string, options?: ScanOptions & {
60
+ /** how many to return in scan generator. default 100 */
61
+ BATCH_SIZE?: number;
62
+ }): AsyncGenerator<T[], void, unknown>;
51
63
  export declare const getItemsDynamo: <T>(params: {
52
64
  tableName: string;
53
65
  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");
@@ -118,7 +131,7 @@ const scan = (tableName, options) => __awaiter(void 0, void 0, void 0, function*
118
131
  }, {});
119
132
  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);
120
133
  do {
121
- const params = new lib_dynamodb_1.ScanCommand(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ TableName: tableName }, ((options === null || options === void 0 ? void 0 : options.filter) && Object.assign({ FilterExpression: options.filter.filterExpression }, (options.filter.attrValues && {
134
+ const params = new lib_dynamodb_1.ScanCommand(Object.assign(Object.assign(Object.assign(Object.assign({ TableName: tableName }, ((options === null || options === void 0 ? void 0 : options.filter) && Object.assign({ FilterExpression: options.filter.filterExpression }, (options.filter.attrValues && {
122
135
  ExpressionAttributeValues: options.filter.attrValues,
123
136
  })))), (Object.keys(expressionAttributeNames).length > 0 && {
124
137
  ExpressionAttributeNames: expressionAttributeNames,
@@ -126,28 +139,65 @@ const scan = (tableName, options) => __awaiter(void 0, void 0, void 0, function*
126
139
  ProjectionExpression: options.requiredAttributeList
127
140
  .map((_, index) => `#proj${index}`)
128
141
  .join(', '),
129
- })), { ExclusiveStartKey }), ((options === null || options === void 0 ? void 0 : options.limit) &&
130
- Items.length < options.limit && {
131
- Limit: options.limit - Items.length,
132
- })));
142
+ })), { ExclusiveStartKey }));
133
143
  const result = yield withRetry(() => exports.dynamoDb.send(params), 'scan');
134
144
  if (result.Items) {
135
145
  Items.push(...result.Items);
136
146
  }
137
147
  ExclusiveStartKey = result.LastEvaluatedKey;
138
- if ((options === null || options === void 0 ? void 0 : options.limit) && Items.length >= options.limit) {
139
- break;
140
- }
141
148
  } while (ExclusiveStartKey);
142
- return {
143
- data: (options === null || options === void 0 ? void 0 : options.limit) ? Items.slice(0, options.limit) : Items,
144
- };
149
+ return { data: Items };
145
150
  }
146
151
  catch (e) {
147
152
  return { error: e.toString() };
148
153
  }
149
154
  });
150
155
  exports.scan = scan;
156
+ function scanWithGenerator(tableName, options) {
157
+ return __asyncGenerator(this, arguments, function* scanWithGenerator_1() {
158
+ var _a, _b, _c;
159
+ const BATCH_SIZE = (_a = options === null || options === void 0 ? void 0 : options.BATCH_SIZE) !== null && _a !== void 0 ? _a : 100;
160
+ let items = [];
161
+ let exclusiveStartKey;
162
+ try {
163
+ const projectionAttrs = (_b = options === null || options === void 0 ? void 0 : options.requiredAttributeList) === null || _b === void 0 ? void 0 : _b.reduce((acc, attr, index) => {
164
+ acc[`#proj${index}`] = attr;
165
+ return acc;
166
+ }, {});
167
+ const expressionAttributeNames = Object.assign(Object.assign({}, projectionAttrs), (_c = options === null || options === void 0 ? void 0 : options.filter) === null || _c === void 0 ? void 0 : _c.attrNames);
168
+ do {
169
+ 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 && {
170
+ ExpressionAttributeValues: options.filter.attrValues,
171
+ })))), (Object.keys(expressionAttributeNames).length > 0 && {
172
+ ExpressionAttributeNames: expressionAttributeNames,
173
+ })), ((options === null || options === void 0 ? void 0 : options.requiredAttributeList) && {
174
+ ProjectionExpression: options.requiredAttributeList
175
+ .map((_, index) => `#proj${index}`)
176
+ .join(', '),
177
+ })), { ExclusiveStartKey: exclusiveStartKey }));
178
+ const result = yield __await(withRetry(() => exports.dynamoDb.send(params), 'scanWithGenerator'));
179
+ if (result.Items) {
180
+ items.push(...result.Items);
181
+ // Process items in chunks of BATCH_SIZE
182
+ while (items.length >= BATCH_SIZE) {
183
+ const batch = items.slice(0, BATCH_SIZE);
184
+ items = items.slice(BATCH_SIZE);
185
+ yield yield __await(batch);
186
+ }
187
+ }
188
+ exclusiveStartKey = result.LastEvaluatedKey;
189
+ } while (exclusiveStartKey);
190
+ // Yield any remaining items
191
+ if (items.length > 0) {
192
+ yield yield __await(items);
193
+ }
194
+ }
195
+ catch (e) {
196
+ throw new Error(`Scan generator error: ${e.toString()}`);
197
+ }
198
+ });
199
+ }
200
+ exports.scanWithGenerator = scanWithGenerator;
151
201
  const getItemsDynamo = (params) => __awaiter(void 0, void 0, void 0, function* () {
152
202
  var _c, _d;
153
203
  try {
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.725",
2
+ "version": "0.0.727",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",