ag-common 0.0.460 → 0.0.462
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.
- package/dist/api/helpers/dynamo.d.ts +31 -35
- package/dist/api/helpers/dynamo.js +176 -279
- package/dist/api/types/aws.d.ts +1 -15
- package/dist/api/types/index.d.ts +1 -2
- package/package.json +11 -4
|
@@ -1,58 +1,54 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
|
|
2
2
|
import { DYNAMOKEYS, IQueryDynamo, Key } from '../types';
|
|
3
|
-
export declare
|
|
4
|
-
export declare
|
|
5
|
-
export declare const putDynamo: <T
|
|
3
|
+
export declare const setDynamo: (region: string) => DynamoDBDocument;
|
|
4
|
+
export declare let dynamoDb: DynamoDBDocument;
|
|
5
|
+
export declare const putDynamo: <T extends Record<string, any>>(item: T, tableName: string, opt?: {
|
|
6
6
|
/** if provided, will assert this PK value doesnt already exist */
|
|
7
7
|
pkName?: string;
|
|
8
8
|
}) => Promise<{
|
|
9
|
-
error?: string
|
|
10
|
-
data?: T | undefined;
|
|
9
|
+
error?: string;
|
|
11
10
|
}>;
|
|
12
|
-
export declare const batchWrite: <T extends
|
|
13
|
-
error?: string
|
|
11
|
+
export declare const batchWrite: <T extends Record<string, any>>(tableName: string, itemsIn: T[]) => Promise<{
|
|
12
|
+
error?: string;
|
|
14
13
|
}>;
|
|
15
|
-
export declare const batchDelete: ({ tableName,
|
|
16
|
-
pkName: string;
|
|
17
|
-
breakOnError?: boolean | undefined;
|
|
14
|
+
export declare const batchDelete: ({ tableName, keys, pkName, }: {
|
|
18
15
|
tableName: string;
|
|
19
16
|
keys: string[];
|
|
20
|
-
rangeName?: string | undefined;
|
|
21
|
-
rangeKeys?: string[] | undefined;
|
|
22
|
-
}) => Promise<{
|
|
23
|
-
error: string;
|
|
24
|
-
} | {
|
|
25
|
-
error?: undefined;
|
|
26
|
-
}>;
|
|
27
|
-
export declare const scan: <T>(tableName: string, opt?: {
|
|
28
|
-
filter?: {
|
|
29
|
-
filterExpression: string;
|
|
30
|
-
attrNames: Record<string, string>;
|
|
31
|
-
attrValues: Record<string, string>;
|
|
32
|
-
};
|
|
33
|
-
/** ProjectionExpression. will csv values */
|
|
34
|
-
requiredAttributeList?: string[];
|
|
35
|
-
}) => Promise<T[]>;
|
|
36
|
-
export declare const getItemDynamo: <T>({ tableName, pkName, pkValue, }: {
|
|
37
17
|
pkName: string;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
18
|
+
}) => Promise<{
|
|
19
|
+
error?: string;
|
|
20
|
+
}>;
|
|
21
|
+
export declare const scan: <T>(tableName: string) => Promise<{
|
|
22
|
+
data?: T[] | undefined;
|
|
23
|
+
error?: string | undefined;
|
|
24
|
+
}>;
|
|
41
25
|
export declare const getItemsDynamo: <T>({ tableName, items, }: {
|
|
42
26
|
items: {
|
|
43
27
|
pkName: string;
|
|
44
28
|
pkValue: string;
|
|
45
29
|
}[];
|
|
46
30
|
tableName: string;
|
|
47
|
-
}) => Promise<
|
|
48
|
-
|
|
49
|
-
|
|
31
|
+
}) => Promise<{
|
|
32
|
+
data?: T[] | undefined;
|
|
33
|
+
error?: string | undefined;
|
|
34
|
+
}>;
|
|
35
|
+
export declare const getItemDynamo: <T>({ tableName, pkName, pkValue, }: {
|
|
36
|
+
pkName: string;
|
|
37
|
+
pkValue: string;
|
|
38
|
+
tableName: string;
|
|
39
|
+
}) => Promise<{
|
|
40
|
+
data?: T | undefined;
|
|
41
|
+
error?: string | undefined;
|
|
42
|
+
}>;
|
|
43
|
+
export declare const queryDynamo: <T>({ tableName, pkName, pkValue, pkOperator, skName, skValue, skOperator, indexName, count, startKeyPk, filterName, filterValue, filterOperator, sortAscending, }: IQueryDynamo) => Promise<{
|
|
44
|
+
data?: T[] | undefined;
|
|
50
45
|
startKey?: Key | undefined;
|
|
46
|
+
error?: string | undefined;
|
|
51
47
|
}>;
|
|
52
48
|
export declare const getDynamoTtlDays: (days: number) => number;
|
|
53
49
|
export declare const getDynamoTtlMinutes: (mins: number) => number;
|
|
54
50
|
export declare const wipeTable: (tableName: string) => Promise<{
|
|
55
|
-
|
|
51
|
+
error?: string;
|
|
56
52
|
}>;
|
|
57
53
|
export declare const getDynamoUpdates: (items: DYNAMOKEYS) => {
|
|
58
54
|
UpdateExpression: string;
|
|
@@ -1,27 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
2
|
+
/* eslint-disable guard-for-in */
|
|
3
|
+
/* eslint-disable no-restricted-syntax */
|
|
4
|
+
/* eslint-disable no-await-in-loop */
|
|
5
|
+
/* eslint-disable prefer-const */
|
|
25
6
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
7
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
8
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -32,223 +13,180 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
32
13
|
});
|
|
33
14
|
};
|
|
34
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
-
exports.getDynamoUpdates = exports.wipeTable = exports.getDynamoTtlMinutes = exports.getDynamoTtlDays = exports.queryDynamo = exports.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
/* eslint-disable prefer-const */
|
|
40
|
-
const dynamodb_1 = __importStar(require("aws-sdk/clients/dynamodb"));
|
|
16
|
+
exports.getDynamoUpdates = exports.wipeTable = exports.getDynamoTtlMinutes = exports.getDynamoTtlDays = exports.queryDynamo = exports.getItemDynamo = exports.getItemsDynamo = exports.scan = exports.batchDelete = exports.batchWrite = exports.putDynamo = exports.dynamoDb = exports.setDynamo = void 0;
|
|
17
|
+
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
18
|
+
const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb");
|
|
19
|
+
// ES6 import
|
|
41
20
|
const array_1 = require("../../common/helpers/array");
|
|
42
21
|
const async_1 = require("../../common/helpers/async");
|
|
43
22
|
const log_1 = require("../../common/helpers/log");
|
|
44
23
|
const sleep_1 = require("../../common/helpers/sleep");
|
|
45
|
-
// eslint-disable-next-line import/no-mutable-exports
|
|
46
|
-
exports.dynamoDb = new dynamodb_1.DocumentClient();
|
|
47
24
|
const setDynamo = (region) => {
|
|
48
|
-
|
|
25
|
+
let raw = new client_dynamodb_1.DynamoDBClient({ region });
|
|
26
|
+
const ddbDocClient = lib_dynamodb_1.DynamoDBDocument.from(raw);
|
|
27
|
+
return ddbDocClient;
|
|
49
28
|
};
|
|
50
29
|
exports.setDynamo = setDynamo;
|
|
30
|
+
exports.dynamoDb = (0, exports.setDynamo)('ap-southeast-2');
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
51
32
|
const putDynamo = (item, tableName, opt) => __awaiter(void 0, void 0, void 0, function* () {
|
|
52
|
-
|
|
53
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
54
|
-
Item: item }, ((opt === null || opt === void 0 ? void 0 : opt.pkName) && {
|
|
33
|
+
let params = new lib_dynamodb_1.PutCommand(Object.assign({ TableName: tableName, Item: item }, ((opt === null || opt === void 0 ? void 0 : opt.pkName) && {
|
|
55
34
|
ConditionExpression: `attribute_not_exists(${opt.pkName})`,
|
|
56
|
-
}));
|
|
57
|
-
(0, log_1.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
35
|
+
})));
|
|
36
|
+
(0, log_1.debug)(`running dynamo put=${JSON.stringify(params, null, 2)}`);
|
|
37
|
+
try {
|
|
38
|
+
yield exports.dynamoDb.send(params);
|
|
39
|
+
return {};
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
(0, log_1.warn)('putDynamo error', e);
|
|
43
|
+
return { error: e.toString() };
|
|
62
44
|
}
|
|
63
|
-
// put never returns into, so just use what we have already
|
|
64
|
-
return { data: item };
|
|
65
45
|
});
|
|
66
46
|
exports.putDynamo = putDynamo;
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
let msg = es;
|
|
87
|
-
(0, log_1.warn)('dynamo write error', msg);
|
|
88
|
-
if (es.indexOf('429') !== -1 ||
|
|
89
|
-
es.indexOf('ProvisionedThroughputExceeded') !== -1) {
|
|
90
|
-
count += 1;
|
|
91
|
-
msg = `batch write throttled. retry ${count}/${max}`;
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
throw e;
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
48
|
+
const batchWrite = (tableName, itemsIn) => __awaiter(void 0, void 0, void 0, function* () {
|
|
49
|
+
//batch up to 20, so we can retry.
|
|
50
|
+
let chunked = (0, array_1.chunk)(itemsIn, 20);
|
|
51
|
+
try {
|
|
52
|
+
yield (0, async_1.asyncForEach)(chunked, (items) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
let retryCount = 0;
|
|
54
|
+
let retryMax = 3;
|
|
55
|
+
let params = new lib_dynamodb_1.BatchWriteCommand({
|
|
56
|
+
RequestItems: {
|
|
57
|
+
[`${tableName}`]: items.map((Item) => ({
|
|
58
|
+
PutRequest: { Item: Item },
|
|
59
|
+
})),
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
(0, log_1.debug)(`running dynamo batchWrite=${JSON.stringify(params, null, 2)}`);
|
|
63
|
+
try {
|
|
64
|
+
yield exports.dynamoDb.send(params);
|
|
65
|
+
return {};
|
|
95
66
|
}
|
|
96
|
-
|
|
97
|
-
|
|
67
|
+
catch (e) {
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
69
|
+
let es = e.toString();
|
|
70
|
+
let msg = es;
|
|
71
|
+
(0, log_1.warn)('dynamo write error', msg);
|
|
72
|
+
if (es.indexOf('429') !== -1 ||
|
|
73
|
+
es.indexOf('ProvisionedThroughputExceeded') !== -1) {
|
|
74
|
+
retryCount += 1;
|
|
75
|
+
msg = `batch write throttled. retry ${retryCount}/${retryMax}`;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
throw e;
|
|
79
|
+
}
|
|
80
|
+
if (retryCount >= retryMax) {
|
|
81
|
+
throw e;
|
|
82
|
+
}
|
|
83
|
+
(0, log_1.warn)(`dynamo retry ${retryCount}/${retryMax}`);
|
|
84
|
+
yield (0, sleep_1.sleep)(2000);
|
|
98
85
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
104
|
-
const batchWrite = (tableName, itemsIn, breakOnError = false) => __awaiter(void 0, void 0, void 0, function* () {
|
|
105
|
-
var _a, _b;
|
|
106
|
-
let items = JSON.parse(JSON.stringify(itemsIn));
|
|
107
|
-
(0, log_1.debug)(`push to dynamo:${tableName} - count=${itemsIn.length}`);
|
|
108
|
-
const error = [];
|
|
109
|
-
while (items.length > 0) {
|
|
110
|
-
const { part, rest } = (0, array_1.take)(items, 25);
|
|
111
|
-
// eslint-disable-next-line no-param-reassign
|
|
112
|
-
items = rest;
|
|
113
|
-
let req = {
|
|
114
|
-
[`${tableName}`]: part.map((item) => ({
|
|
115
|
-
PutRequest: { Item: item },
|
|
116
|
-
})),
|
|
117
|
-
};
|
|
118
|
-
let res = yield batchWriteRaw(req);
|
|
119
|
-
const newError = (_b = (_a = res.$response) === null || _a === void 0 ? void 0 : _a.error) !== null && _b !== void 0 ? _b : null;
|
|
120
|
-
if (newError) {
|
|
121
|
-
error.push(newError);
|
|
122
|
-
if (breakOnError) {
|
|
123
|
-
items = [];
|
|
124
|
-
}
|
|
125
|
-
}
|
|
86
|
+
}));
|
|
87
|
+
return {};
|
|
126
88
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return { error: me };
|
|
89
|
+
catch (e) {
|
|
90
|
+
(0, log_1.warn)('batchWrite error', e);
|
|
91
|
+
return { error: e.toString() };
|
|
131
92
|
}
|
|
132
|
-
return {};
|
|
133
93
|
});
|
|
134
94
|
exports.batchWrite = batchWrite;
|
|
135
|
-
const batchDelete = ({ tableName,
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
params
|
|
95
|
+
const batchDelete = ({ tableName, keys, pkName, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
96
|
+
//batch up to 20, so we can retry.
|
|
97
|
+
let chunked = (0, array_1.chunk)(keys, 20);
|
|
98
|
+
try {
|
|
99
|
+
yield (0, async_1.asyncForEach)(chunked, (items) => __awaiter(void 0, void 0, void 0, function* () {
|
|
100
|
+
let retryCount = 0;
|
|
101
|
+
let retryMax = 3;
|
|
102
|
+
let params = new lib_dynamodb_1.BatchWriteCommand({
|
|
103
|
+
RequestItems: {
|
|
104
|
+
[`${tableName}`]: items.map((key) => ({
|
|
105
|
+
DeleteRequest: { Key: { [`${pkName}`]: key } },
|
|
106
|
+
})),
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
(0, log_1.debug)(`running dynamo batch delete=${JSON.stringify(params, null, 2)}`);
|
|
110
|
+
try {
|
|
111
|
+
yield exports.dynamoDb.send(params);
|
|
112
|
+
return {};
|
|
152
113
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
114
|
+
catch (e) {
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
116
|
+
let es = e.toString();
|
|
117
|
+
let msg = es;
|
|
118
|
+
(0, log_1.warn)('dynamo write error', msg);
|
|
119
|
+
if (es.indexOf('429') !== -1 ||
|
|
120
|
+
es.indexOf('ProvisionedThroughputExceeded') !== -1) {
|
|
121
|
+
retryCount += 1;
|
|
122
|
+
msg = `batch delete write throttled. retry ${retryCount}/${retryMax}`;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
throw e;
|
|
126
|
+
}
|
|
127
|
+
if (retryCount >= retryMax) {
|
|
128
|
+
throw e;
|
|
129
|
+
}
|
|
130
|
+
(0, log_1.warn)(`dynamo retry ${retryCount}/${retryMax}`);
|
|
131
|
+
yield (0, sleep_1.sleep)(2000);
|
|
160
132
|
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
(0, log_1.
|
|
166
|
-
return { error:
|
|
133
|
+
}));
|
|
134
|
+
return {};
|
|
135
|
+
}
|
|
136
|
+
catch (e) {
|
|
137
|
+
(0, log_1.warn)('batchDelete error', e);
|
|
138
|
+
return { error: e.toString() };
|
|
167
139
|
}
|
|
168
|
-
return {};
|
|
169
140
|
});
|
|
170
141
|
exports.batchDelete = batchDelete;
|
|
171
|
-
const scan = (tableName
|
|
172
|
-
|
|
173
|
-
let
|
|
174
|
-
do {
|
|
175
|
-
let params = {
|
|
176
|
-
TableName: tableName,
|
|
177
|
-
ExclusiveStartKey,
|
|
178
|
-
};
|
|
179
|
-
if (opt === null || opt === void 0 ? void 0 : opt.filter) {
|
|
180
|
-
params.FilterExpression = opt.filter.filterExpression;
|
|
181
|
-
params.ExpressionAttributeNames = opt.filter.attrNames;
|
|
182
|
-
params.ExpressionAttributeValues = opt.filter.attrValues;
|
|
183
|
-
}
|
|
184
|
-
if (opt === null || opt === void 0 ? void 0 : opt.requiredAttributeList) {
|
|
185
|
-
params.ProjectionExpression = opt.requiredAttributeList.join(', ');
|
|
186
|
-
}
|
|
187
|
-
const { Items: newitems, LastEvaluatedKey, $response,
|
|
188
|
-
// eslint-disable-next-line no-await-in-loop
|
|
189
|
-
} = yield exports.dynamoDb.scan(params).promise();
|
|
190
|
-
ExclusiveStartKey = LastEvaluatedKey;
|
|
191
|
-
if ($response.error && $response.error.statusCode) {
|
|
192
|
-
throw new Error($response.error.message);
|
|
193
|
-
}
|
|
194
|
-
if (newitems) {
|
|
195
|
-
Items.push(...newitems.map((r) => r));
|
|
196
|
-
}
|
|
197
|
-
} while (ExclusiveStartKey);
|
|
198
|
-
(0, log_1.info)(`dynamo scan against ${tableName} ok, count=${Items === null || Items === void 0 ? void 0 : Items.length}`);
|
|
199
|
-
return Items;
|
|
200
|
-
});
|
|
201
|
-
exports.scan = scan;
|
|
202
|
-
const getItemDynamo = ({ tableName, pkName, pkValue, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
203
|
-
const params = {
|
|
204
|
-
Key: { [pkName]: pkValue },
|
|
142
|
+
const scan = (tableName) => __awaiter(void 0, void 0, void 0, function* () {
|
|
143
|
+
var _a;
|
|
144
|
+
let params = new lib_dynamodb_1.ScanCommand({
|
|
205
145
|
TableName: tableName,
|
|
206
|
-
};
|
|
146
|
+
});
|
|
147
|
+
(0, log_1.debug)(`running dynamo scan=${JSON.stringify(params, null, 2)}`);
|
|
207
148
|
try {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
return ret;
|
|
149
|
+
let ret = yield exports.dynamoDb.send(params);
|
|
150
|
+
let items = ((_a = ret.Items) !== null && _a !== void 0 ? _a : []).map((r) => r);
|
|
151
|
+
return { data: items };
|
|
212
152
|
}
|
|
213
153
|
catch (e) {
|
|
214
|
-
(0, log_1.
|
|
215
|
-
|
|
154
|
+
(0, log_1.warn)('scan error', e);
|
|
155
|
+
return { error: e.toString() };
|
|
216
156
|
}
|
|
217
157
|
});
|
|
218
|
-
exports.
|
|
158
|
+
exports.scan = scan;
|
|
219
159
|
const getItemsDynamo = ({ tableName, items, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
220
|
-
var
|
|
221
|
-
const params = {
|
|
160
|
+
var _b, _c;
|
|
161
|
+
const params = new lib_dynamodb_1.BatchGetCommand({
|
|
222
162
|
RequestItems: {
|
|
223
163
|
[tableName]: {
|
|
224
164
|
Keys: items.map(({ pkName, pkValue }) => ({
|
|
225
|
-
[pkName]:
|
|
165
|
+
[pkName]: pkValue,
|
|
226
166
|
})),
|
|
227
167
|
},
|
|
228
168
|
},
|
|
229
|
-
};
|
|
230
|
-
const dbRaw = new dynamodb_1.default({ apiVersion: '2012-10-08' });
|
|
169
|
+
});
|
|
231
170
|
try {
|
|
232
|
-
|
|
233
|
-
(
|
|
234
|
-
|
|
235
|
-
[];
|
|
236
|
-
return ret;
|
|
171
|
+
let res = yield exports.dynamoDb.send(params);
|
|
172
|
+
let data = (_c = (_b = res.Responses) === null || _b === void 0 ? void 0 : _b[tableName].map((r) => r)) !== null && _c !== void 0 ? _c : [];
|
|
173
|
+
return { data };
|
|
237
174
|
}
|
|
238
175
|
catch (e) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
243
|
-
e.toString();
|
|
244
|
-
(0, log_1.error)(msg);
|
|
245
|
-
throw e;
|
|
176
|
+
(0, log_1.warn)('getItemsDynamo error:', e);
|
|
177
|
+
return { error: e.toString() };
|
|
178
|
+
//
|
|
246
179
|
}
|
|
247
180
|
});
|
|
248
181
|
exports.getItemsDynamo = getItemsDynamo;
|
|
249
|
-
const
|
|
250
|
-
var
|
|
251
|
-
let
|
|
182
|
+
const getItemDynamo = ({ tableName, pkName, pkValue, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
183
|
+
var _d;
|
|
184
|
+
let r = yield (0, exports.getItemsDynamo)({ tableName, items: [{ pkName, pkValue }] });
|
|
185
|
+
return { data: (_d = r.data) === null || _d === void 0 ? void 0 : _d[0], error: r.error };
|
|
186
|
+
});
|
|
187
|
+
exports.getItemDynamo = getItemDynamo;
|
|
188
|
+
const queryDynamo = ({ tableName, pkName, pkValue, pkOperator = '=', skName, skValue, skOperator = '=', indexName, count = 1000, startKeyPk, filterName, filterValue, filterOperator = '=', sortAscending = true, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
189
|
+
var _e;
|
|
252
190
|
let kce = `#${pkName.toLowerCase()} ${pkOperator} :${pkName.toLowerCase()}`;
|
|
253
191
|
const ean = { [`#${pkName.toLowerCase()}`]: pkName };
|
|
254
192
|
const eav = {
|
|
@@ -292,54 +230,36 @@ const queryDynamo = ({ tableName, pkName, pkValue, pkOperator = '=', skName, skV
|
|
|
292
230
|
}
|
|
293
231
|
}
|
|
294
232
|
const Items = [];
|
|
233
|
+
let startKey = startKeyPk
|
|
234
|
+
? { [`${pkName}`]: { S: startKeyPk } }
|
|
235
|
+
: undefined;
|
|
295
236
|
do {
|
|
296
|
-
const params = {
|
|
297
|
-
TableName: tableName,
|
|
298
|
-
KeyConditionExpression: kce,
|
|
299
|
-
ExpressionAttributeNames: ean,
|
|
300
|
-
ExpressionAttributeValues: eav,
|
|
237
|
+
const params = new lib_dynamodb_1.QueryCommand(Object.assign(Object.assign(Object.assign(Object.assign({ TableName: tableName, KeyConditionExpression: kce, ExpressionAttributeNames: ean, ExpressionAttributeValues: eav }, (startKey && {
|
|
301
238
|
ExclusiveStartKey: startKey,
|
|
302
|
-
|
|
303
|
-
ScanIndexForward: sortAscending,
|
|
304
|
-
};
|
|
305
|
-
if (count > 0) {
|
|
306
|
-
params.Limit = count;
|
|
307
|
-
}
|
|
308
|
-
else {
|
|
309
|
-
params.Limit = 1000;
|
|
310
|
-
}
|
|
311
|
-
if (indexName) {
|
|
312
|
-
params.IndexName = indexName;
|
|
313
|
-
}
|
|
314
|
-
let newitems;
|
|
239
|
+
})), (FilterExpression && { FilterExpression })), { ScanIndexForward: sortAscending, Limit: count > 0 ? count : 1000 }), (indexName && { IndexName: indexName })));
|
|
315
240
|
let lek;
|
|
316
|
-
let
|
|
241
|
+
let newItems;
|
|
317
242
|
try {
|
|
318
243
|
({
|
|
319
|
-
Items:
|
|
244
|
+
Items: newItems,
|
|
320
245
|
LastEvaluatedKey: lek,
|
|
321
|
-
$response,
|
|
322
246
|
// eslint-disable-next-line no-await-in-loop
|
|
323
|
-
} = yield exports.dynamoDb.
|
|
247
|
+
} = yield exports.dynamoDb.send(params));
|
|
248
|
+
if (newItems) {
|
|
249
|
+
Items.push(...newItems.map((i) => i));
|
|
250
|
+
}
|
|
324
251
|
}
|
|
325
252
|
catch (e) {
|
|
326
|
-
(0, log_1.
|
|
327
|
-
|
|
253
|
+
(0, log_1.warn)('error. query params=', JSON.stringify(params), e);
|
|
254
|
+
return { error: e.toString() };
|
|
328
255
|
}
|
|
329
256
|
startKey = lek;
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
}
|
|
334
|
-
(0, log_1.debug)(`dynamo query against ${params === null || params === void 0 ? void 0 : params.TableName} ok, count=${newitems === null || newitems === void 0 ? void 0 : newitems.length} ${JSON.stringify(params)}`, ` next startkey=${startKey}`);
|
|
335
|
-
if (newitems) {
|
|
336
|
-
Items.push(...newitems.map((r) => r));
|
|
337
|
-
}
|
|
338
|
-
if (count > 0 && ((_g = newitems === null || newitems === void 0 ? void 0 : newitems.length) !== null && _g !== void 0 ? _g : 0) >= count) {
|
|
339
|
-
return { Items, startKey };
|
|
257
|
+
(0, log_1.debug)(`dynamo query against ${params === null || params === void 0 ? void 0 : params.input.TableName} ok, count=${newItems === null || newItems === void 0 ? void 0 : newItems.length} ${JSON.stringify(params)}`, ` next startkey=${startKey}`);
|
|
258
|
+
if (count > 0 && ((_e = newItems === null || newItems === void 0 ? void 0 : newItems.length) !== null && _e !== void 0 ? _e : 0) >= count) {
|
|
259
|
+
return { data: Items, startKey };
|
|
340
260
|
}
|
|
341
261
|
} while (startKey && Object.keys(startKey).length > 0);
|
|
342
|
-
return { Items, startKey: undefined };
|
|
262
|
+
return { data: Items, startKey: undefined };
|
|
343
263
|
});
|
|
344
264
|
exports.queryDynamo = queryDynamo;
|
|
345
265
|
const getDynamoTtlDays = (days) => Math.ceil(new Date().getTime() / 1000) + days * 86400;
|
|
@@ -347,53 +267,30 @@ exports.getDynamoTtlDays = getDynamoTtlDays;
|
|
|
347
267
|
const getDynamoTtlMinutes = (mins) => Math.ceil(new Date().getTime() / 1000) + mins * 60;
|
|
348
268
|
exports.getDynamoTtlMinutes = getDynamoTtlMinutes;
|
|
349
269
|
const wipeTable = (tableName) => __awaiter(void 0, void 0, void 0, function* () {
|
|
350
|
-
var
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
// @ts-ignore
|
|
374
|
-
const datagr = (0, array_1.chunk)(all, 25);
|
|
375
|
-
let res = yield Promise.all(datagr.map((group) => dbRaw
|
|
376
|
-
.batchWriteItem({
|
|
377
|
-
RequestItems: {
|
|
378
|
-
[`${tableName}`]: group.map((s) => ({
|
|
379
|
-
DeleteRequest: {
|
|
380
|
-
Key: {
|
|
381
|
-
[`${keyHash}`]: s[keyHash],
|
|
382
|
-
},
|
|
383
|
-
},
|
|
384
|
-
})),
|
|
385
|
-
},
|
|
386
|
-
})
|
|
387
|
-
.promise()));
|
|
388
|
-
let errors = res
|
|
389
|
-
.map((r) => { var _a, _b; return ((_b = (((_a = r === null || r === void 0 ? void 0 : r.$response) === null || _a === void 0 ? void 0 : _a.error) || {})) === null || _b === void 0 ? void 0 : _b.message) || undefined; })
|
|
390
|
-
.filter(array_1.notEmpty);
|
|
391
|
-
if (errors.length > 0) {
|
|
392
|
-
(0, log_1.error)('errors=', JSON.stringify(errors));
|
|
393
|
-
return { errors };
|
|
270
|
+
var _f, _g;
|
|
271
|
+
try {
|
|
272
|
+
let infoV = yield exports.dynamoDb.send(new client_dynamodb_1.DescribeTableCommand({ TableName: tableName }));
|
|
273
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
274
|
+
// @ts-ignore
|
|
275
|
+
let keyHash = infoV.Table.KeySchema.find((k) => k.KeyType === 'HASH').AttributeName;
|
|
276
|
+
if (!keyHash) {
|
|
277
|
+
throw new Error('couldnt find keyHash');
|
|
278
|
+
}
|
|
279
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
280
|
+
let all = ((_g = (_f = (yield (0, exports.scan)(tableName))) === null || _f === void 0 ? void 0 : _f.data) === null || _g === void 0 ? void 0 : _g.map((d) => d)) || [];
|
|
281
|
+
(0, log_1.warn)(`will delete ${all === null || all === void 0 ? void 0 : all.length} items from ${tableName}`);
|
|
282
|
+
yield (0, exports.batchDelete)({
|
|
283
|
+
tableName,
|
|
284
|
+
keys: all.map((s) => s[keyHash]),
|
|
285
|
+
pkName: 'PK',
|
|
286
|
+
});
|
|
287
|
+
(0, log_1.warn)(`cleared table ${tableName}`);
|
|
288
|
+
return {};
|
|
289
|
+
}
|
|
290
|
+
catch (e) {
|
|
291
|
+
(0, log_1.warn)('wipeTable error:', e);
|
|
292
|
+
return { error: e.toString() };
|
|
394
293
|
}
|
|
395
|
-
(0, log_1.warn)(`cleared table ${tableName}`);
|
|
396
|
-
return {};
|
|
397
294
|
});
|
|
398
295
|
exports.wipeTable = wipeTable;
|
|
399
296
|
const getDynamoUpdates = (items) => {
|
package/dist/api/types/aws.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AttributeValue } from '@aws-sdk/client-dynamodb/dist-types/models/models_0';
|
|
1
2
|
export interface APIGatewayProxyResult {
|
|
2
3
|
statusCode: number;
|
|
3
4
|
headers?: {
|
|
@@ -30,20 +31,6 @@ export interface APIGatewayEvent {
|
|
|
30
31
|
httpMethod: string;
|
|
31
32
|
};
|
|
32
33
|
}
|
|
33
|
-
interface AttributeValue {
|
|
34
|
-
B?: string | undefined;
|
|
35
|
-
BS?: string[] | undefined;
|
|
36
|
-
BOOL?: boolean | undefined;
|
|
37
|
-
L?: AttributeValue[] | undefined;
|
|
38
|
-
M?: {
|
|
39
|
-
[id: string]: AttributeValue;
|
|
40
|
-
} | undefined;
|
|
41
|
-
N?: string | undefined;
|
|
42
|
-
NS?: string[] | undefined;
|
|
43
|
-
NULL?: boolean | undefined;
|
|
44
|
-
S?: string | undefined;
|
|
45
|
-
SS?: string[] | undefined;
|
|
46
|
-
}
|
|
47
34
|
export interface AppSyncResolverEvent<TArguments, TSource = Record<string, any> | null> {
|
|
48
35
|
arguments: TArguments;
|
|
49
36
|
identity?: any;
|
|
@@ -100,4 +87,3 @@ export interface DynamoDBStreamEvent {
|
|
|
100
87
|
export type Key = {
|
|
101
88
|
[key: string]: AttributeValue;
|
|
102
89
|
};
|
|
103
|
-
export {};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { aws_dynamodb as dynamodb, aws_iam as iam, aws_lambda as lambda } from 'aws-cdk-lib';
|
|
2
|
-
import { Key } from './aws';
|
|
3
2
|
export interface DYNAMOKEYS {
|
|
4
3
|
type: string;
|
|
5
4
|
L1: string;
|
|
@@ -60,7 +59,7 @@ export interface IQueryDynamo {
|
|
|
60
59
|
indexName?: string;
|
|
61
60
|
/** default 1000 */
|
|
62
61
|
count?: number;
|
|
63
|
-
|
|
62
|
+
startKeyPk?: string;
|
|
64
63
|
filterName?: string;
|
|
65
64
|
filterValue?: string | number | boolean;
|
|
66
65
|
/** default, = */
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.0.
|
|
2
|
+
"version": "0.0.462",
|
|
3
3
|
"name": "ag-common",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
@@ -12,11 +12,15 @@
|
|
|
12
12
|
"build": "rimraf dist && tsc -p tsconfig.build.json",
|
|
13
13
|
"lint": "npx eslint --ext .ts,.tsx .",
|
|
14
14
|
"start": "cross-env BROWSER=none cross-env storybook dev -p 6006",
|
|
15
|
-
"build-storybook": "storybook build -o docs --quiet"
|
|
15
|
+
"build-storybook": "storybook build -o docs --quiet",
|
|
16
|
+
"test-run": "ts-node -- src/api/helpers/dynamo.test.ts"
|
|
16
17
|
},
|
|
17
18
|
"dependencies": {
|
|
19
|
+
"@aws-sdk/client-dynamodb": "^3.379.1",
|
|
20
|
+
"@aws-sdk/lib-dynamodb": "^3.379.1",
|
|
21
|
+
"@aws-sdk/util-dynamodb": "^3.379.1",
|
|
18
22
|
"aws-cdk-lib": ">=2",
|
|
19
|
-
"aws-sdk": ">=2",
|
|
23
|
+
"aws-sdk": ">=2.1368.0",
|
|
20
24
|
"axios": ">=1",
|
|
21
25
|
"constructs": ">=10",
|
|
22
26
|
"jsonwebtoken": ">=9",
|
|
@@ -42,14 +46,17 @@
|
|
|
42
46
|
"@storybook/react": "7.1.1",
|
|
43
47
|
"@storybook/react-webpack5": "7.1.1",
|
|
44
48
|
"@storybook/theming": "7.1.1",
|
|
49
|
+
"@types/jest": "^29.5.3",
|
|
45
50
|
"@types/jsonwebtoken": "9.0.2",
|
|
46
51
|
"@types/node": "20.4.5",
|
|
47
52
|
"@types/react": "18.2.17",
|
|
48
53
|
"@types/react-dom": "18.2.7",
|
|
49
54
|
"cross-env": "7.0.3",
|
|
50
55
|
"eslint-config-e7npm": "0.0.13",
|
|
56
|
+
"jest": "^29.6.2",
|
|
51
57
|
"rimraf": "5.0.1",
|
|
52
|
-
"storybook": "7.1.1"
|
|
58
|
+
"storybook": "7.1.1",
|
|
59
|
+
"ts-jest": "^29.1.1"
|
|
53
60
|
},
|
|
54
61
|
"files": [
|
|
55
62
|
"dist/**/*",
|