ag-common 0.0.732 → 0.0.733
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.
|
@@ -28,6 +28,7 @@ interface ScanOptions {
|
|
|
28
28
|
filter?: DynamoFilter;
|
|
29
29
|
requiredAttributeList?: string[];
|
|
30
30
|
indexName?: string;
|
|
31
|
+
alwaysRetry?: boolean;
|
|
31
32
|
}
|
|
32
33
|
interface DynamoQueryParams {
|
|
33
34
|
tableName: string;
|
|
@@ -40,9 +41,7 @@ interface DynamoQueryParams {
|
|
|
40
41
|
indexName?: string;
|
|
41
42
|
limit?: number;
|
|
42
43
|
startKey?: Key;
|
|
43
|
-
|
|
44
|
-
filterValue?: unknown;
|
|
45
|
-
filterOperator?: string;
|
|
44
|
+
filter?: DynamoFilter;
|
|
46
45
|
sortAscending?: boolean;
|
|
47
46
|
}
|
|
48
47
|
export declare let dynamoDb: DynamoDBDocument;
|
|
@@ -76,6 +76,7 @@ exports.putDynamo = putDynamo;
|
|
|
76
76
|
const batchWrite = (tableName, items, opt) => __awaiter(void 0, void 0, void 0, function* () {
|
|
77
77
|
try {
|
|
78
78
|
const { batchSize = 20 } = opt !== null && opt !== void 0 ? opt : {};
|
|
79
|
+
var processed = 0;
|
|
79
80
|
const chunked = (0, array_1.chunk)(items, batchSize);
|
|
80
81
|
yield (0, async_1.asyncForEach)(chunked, (chunk) => __awaiter(void 0, void 0, void 0, function* () {
|
|
81
82
|
const params = new lib_dynamodb_1.BatchWriteCommand({
|
|
@@ -83,9 +84,10 @@ const batchWrite = (tableName, items, opt) => __awaiter(void 0, void 0, void 0,
|
|
|
83
84
|
[tableName]: chunk.map((Item) => ({ PutRequest: { Item } })),
|
|
84
85
|
},
|
|
85
86
|
});
|
|
86
|
-
yield (0, withRetry_1.withRetry)(() => exports.dynamoDb.send(params),
|
|
87
|
+
yield (0, withRetry_1.withRetry)(() => exports.dynamoDb.send(params), `batchwrite ${processed}/${items.length}. size=${batchSize}`, {
|
|
87
88
|
maxRetries: (opt === null || opt === void 0 ? void 0 : opt.alwaysRetry) ? null : undefined,
|
|
88
89
|
});
|
|
90
|
+
processed += chunk.length;
|
|
89
91
|
}));
|
|
90
92
|
return { data: undefined };
|
|
91
93
|
}
|
|
@@ -105,6 +107,7 @@ const batchDelete = (params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
105
107
|
try {
|
|
106
108
|
const { batchSize = 20, alwaysRetry = false } = (_a = params.opt) !== null && _a !== void 0 ? _a : {};
|
|
107
109
|
const chunked = (0, array_1.chunk)(params.keys, batchSize);
|
|
110
|
+
var processed = 0;
|
|
108
111
|
yield (0, async_1.asyncForEach)(chunked, (chunk) => __awaiter(void 0, void 0, void 0, function* () {
|
|
109
112
|
const command = new lib_dynamodb_1.BatchWriteCommand({
|
|
110
113
|
RequestItems: {
|
|
@@ -113,9 +116,10 @@ const batchDelete = (params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
113
116
|
})),
|
|
114
117
|
},
|
|
115
118
|
});
|
|
116
|
-
yield (0, withRetry_1.withRetry)(() => exports.dynamoDb.send(command),
|
|
119
|
+
yield (0, withRetry_1.withRetry)(() => exports.dynamoDb.send(command), `batchdelete ${processed}/${params.keys.length}. size=${batchSize}`, {
|
|
117
120
|
maxRetries: alwaysRetry ? null : undefined,
|
|
118
121
|
});
|
|
122
|
+
processed += chunk.length;
|
|
119
123
|
}));
|
|
120
124
|
return { data: undefined };
|
|
121
125
|
}
|
|
@@ -151,7 +155,9 @@ const scan = (tableName, options) => __awaiter(void 0, void 0, void 0, function*
|
|
|
151
155
|
.map((_, index) => `#proj${index}`)
|
|
152
156
|
.join(', '),
|
|
153
157
|
})), { ExclusiveStartKey }));
|
|
154
|
-
const result = yield (0, withRetry_1.withRetry)(() => exports.dynamoDb.send(params),
|
|
158
|
+
const result = yield (0, withRetry_1.withRetry)(() => exports.dynamoDb.send(params), `scan. already seen=${Items.length}`, {
|
|
159
|
+
maxRetries: (options === null || options === void 0 ? void 0 : options.alwaysRetry) ? null : undefined,
|
|
160
|
+
});
|
|
155
161
|
if (result.Items) {
|
|
156
162
|
Items.push(...result.Items);
|
|
157
163
|
}
|
|
@@ -194,7 +200,9 @@ function scanWithGenerator(tableName, options) {
|
|
|
194
200
|
.map((_, index) => `#proj${index}`)
|
|
195
201
|
.join(', '),
|
|
196
202
|
})), { ExclusiveStartKey: exclusiveStartKey }));
|
|
197
|
-
const result = yield __await((0, withRetry_1.withRetry)(() => exports.dynamoDb.send(params),
|
|
203
|
+
const result = yield __await((0, withRetry_1.withRetry)(() => exports.dynamoDb.send(params), `scanWithGenerator. already seen=${items.length}`, {
|
|
204
|
+
maxRetries: (options === null || options === void 0 ? void 0 : options.alwaysRetry) ? null : undefined,
|
|
205
|
+
}));
|
|
198
206
|
if (result.Items) {
|
|
199
207
|
items.push(...result.Items);
|
|
200
208
|
// Process items in chunks of BATCH_SIZE
|
|
@@ -250,7 +258,7 @@ const getItemDynamo = (params) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
250
258
|
});
|
|
251
259
|
exports.getItemDynamo = getItemDynamo;
|
|
252
260
|
const queryDynamo = (params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
253
|
-
var _a, _b, _c;
|
|
261
|
+
var _a, _b, _c, _d;
|
|
254
262
|
try {
|
|
255
263
|
let kce = `#${params.pkName.toLowerCase()} ${(_a = params.pkOperator) !== null && _a !== void 0 ? _a : '='} :${params.pkName.toLowerCase()}`;
|
|
256
264
|
const ean = {
|
|
@@ -279,29 +287,12 @@ const queryDynamo = (params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
279
287
|
eav[`:${skName.toLowerCase()}`] = skValue;
|
|
280
288
|
}
|
|
281
289
|
}
|
|
282
|
-
let FilterExpression;
|
|
283
|
-
if (params.filterName && params.filterValue !== undefined) {
|
|
284
|
-
ean[`#${params.filterName.toLowerCase()}`] = params.filterName;
|
|
285
|
-
eav[`:${params.filterName.toLowerCase()}`] = params.filterValue;
|
|
286
|
-
FilterExpression =
|
|
287
|
-
params.filterOperator === 'contains'
|
|
288
|
-
? `contains(#${params.filterName.toLowerCase()}, :${params.filterName.toLowerCase()})`
|
|
289
|
-
: `#${params.filterName.toLowerCase()} ${(_b = params.filterOperator) !== null && _b !== void 0 ? _b : '='} :${params.filterName.toLowerCase()}`;
|
|
290
|
-
}
|
|
291
290
|
const items = [];
|
|
292
291
|
let { startKey } = params;
|
|
293
292
|
do {
|
|
294
|
-
const queryParams = new lib_dynamodb_1.QueryCommand({
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
ExpressionAttributeNames: ean,
|
|
298
|
-
ExpressionAttributeValues: eav,
|
|
299
|
-
ScanIndexForward: (_c = params.sortAscending) !== null && _c !== void 0 ? _c : true,
|
|
300
|
-
Limit: params.limit,
|
|
301
|
-
IndexName: params.indexName,
|
|
302
|
-
ExclusiveStartKey: startKey,
|
|
303
|
-
FilterExpression,
|
|
304
|
-
});
|
|
293
|
+
const queryParams = new lib_dynamodb_1.QueryCommand(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, IndexName: params.indexName, ExclusiveStartKey: startKey }, (params.filter && Object.assign({ FilterExpression: params.filter.filterExpression }, (params.filter.attrValues && {
|
|
294
|
+
ExpressionAttributeValues: Object.assign(Object.assign({}, eav), params.filter.attrValues),
|
|
295
|
+
})))));
|
|
305
296
|
const result = yield (0, withRetry_1.withRetry)(() => exports.dynamoDb.send(queryParams), 'queryDynamo');
|
|
306
297
|
if (result.Items) {
|
|
307
298
|
items.push(...result.Items);
|
|
@@ -14,7 +14,7 @@ const log_1 = require("../../common/helpers/log");
|
|
|
14
14
|
const sleep_1 = require("../../common/helpers/sleep");
|
|
15
15
|
const withRetry = (operation, operationName, opt) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
16
|
let retryCount = 0;
|
|
17
|
-
|
|
17
|
+
const baseDelay = 2000;
|
|
18
18
|
let { maxRetries = 3 } = opt !== null && opt !== void 0 ? opt : {};
|
|
19
19
|
// eslint-disable-next-line
|
|
20
20
|
while (true) {
|
|
@@ -32,7 +32,7 @@ const withRetry = (operation, operationName, opt) => __awaiter(void 0, void 0, v
|
|
|
32
32
|
(0, log_1.warn)(`${operationName}: Max retries exceeded`);
|
|
33
33
|
throw error;
|
|
34
34
|
}
|
|
35
|
-
const delay = baseDelay *
|
|
35
|
+
const delay = baseDelay + retryCount * 1000;
|
|
36
36
|
(0, log_1.warn)(`${operationName}: Throttled. Retry ${retryCount}. Sleeping for ${delay}ms`);
|
|
37
37
|
yield (0, sleep_1.sleep)(delay);
|
|
38
38
|
continue;
|
package/package.json
CHANGED