@eresearchqut/ddb-repository 1.5.8 → 1.13.4

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/index.cjs ADDED
@@ -0,0 +1,478 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let _aws_sdk_client_dynamodb = require("@aws-sdk/client-dynamodb");
3
+ let _aws_sdk_util_dynamodb = require("@aws-sdk/util-dynamodb");
4
+ let lodash = require("lodash");
5
+
6
+ //#region src/DynamoDbRepository.ts
7
+ let FilterOperator = /* @__PURE__ */ function(FilterOperator) {
8
+ FilterOperator["EQUALS"] = "=";
9
+ FilterOperator["NOT_EQUALS"] = "<>";
10
+ FilterOperator["GREATER_THAN_OR_EQUALS"] = ">=";
11
+ FilterOperator["GREATER_THAN"] = ">";
12
+ FilterOperator["LESS_THAN"] = "<";
13
+ FilterOperator["LESS_THAN_OR_EQUALS"] = "<=";
14
+ FilterOperator["IN"] = "IN";
15
+ FilterOperator["BETWEEN"] = "BETWEEN";
16
+ FilterOperator["BEGINS_WITH"] = "BEGINS_WITH";
17
+ FilterOperator["CONTAINS"] = "CONTAINS";
18
+ return FilterOperator;
19
+ }({});
20
+ const marshallKey = (key) => (0, _aws_sdk_util_dynamodb.marshall)(key, { removeUndefinedValues: true });
21
+ const expressionAttributeKey = (key) => (0, lodash.replace)(key, /-/g, "_");
22
+ const mapInKeys = (filterExpression) => Array.isArray(filterExpression.value) ? filterExpression.value.map((_, index) => `:${expressionAttributeKey(filterExpression.attribute)}${index}`) : `:${expressionAttributeKey(filterExpression.attribute)}`;
23
+ const mapFilterExpression = (filterExpression) => {
24
+ switch (filterExpression.operator) {
25
+ case FilterOperator.IN: return `#${expressionAttributeKey(filterExpression.attribute)} ${filterExpression.operator} (${mapInKeys(filterExpression)})`;
26
+ case FilterOperator.BETWEEN: return `#${expressionAttributeKey(filterExpression.attribute)} ${filterExpression.operator} :${expressionAttributeKey(filterExpression.attribute)}0 AND :${expressionAttributeKey(filterExpression.attribute)}1`;
27
+ case FilterOperator.BEGINS_WITH:
28
+ case FilterOperator.CONTAINS: return `${filterExpression.operator.toLowerCase()}(#${expressionAttributeKey(filterExpression.attribute)}, :${expressionAttributeKey(filterExpression.attribute)})`;
29
+ default: return `#${expressionAttributeKey(filterExpression.attribute)} ${filterExpression.operator} :${expressionAttributeKey(filterExpression.attribute)}`;
30
+ }
31
+ };
32
+ const mapFilterExpressions = (filterExpressions) => filterExpressions.map((filterExpression) => filterExpression.negate ? `NOT ${mapFilterExpression(filterExpression)}` : mapFilterExpression(filterExpression)).join(" AND ");
33
+ const mapFilterExpressionValues = (filterExpression) => Array.isArray(filterExpression.value) ? filterExpression.value.reduce((reduction, value, index) => ({
34
+ ...reduction,
35
+ [`:${expressionAttributeKey(filterExpression.attribute)}${index}`]: value
36
+ }), Object.assign({})) : { [`:${expressionAttributeKey(filterExpression.attribute)}`]: filterExpression.value };
37
+ const paginate = (array, pageSize) => {
38
+ return array.reduce((acc, val, i) => {
39
+ const idx = Math.floor(i / pageSize);
40
+ (acc[idx] || (acc[idx] = [])).push(val);
41
+ return acc;
42
+ }, []);
43
+ };
44
+ var DynamoDbRepository = class {
45
+ constructor(options) {
46
+ this.getItem = async (key) => {
47
+ return this.dynamoDBClient.send(new _aws_sdk_client_dynamodb.GetItemCommand({
48
+ TableName: this.tableName,
49
+ Key: marshallKey(key),
50
+ ReturnConsumedCapacity: this.returnConsumedCapacity
51
+ })).then((result) => result.Item ? (0, _aws_sdk_util_dynamodb.unmarshall)(result.Item) : void 0);
52
+ };
53
+ this.putItem = async (key, record) => {
54
+ const Item = (0, _aws_sdk_util_dynamodb.marshall)({
55
+ ...record,
56
+ ...key
57
+ }, { removeUndefinedValues: true });
58
+ return this.dynamoDBClient.send(new _aws_sdk_client_dynamodb.PutItemCommand({
59
+ TableName: this.tableName,
60
+ ReturnConsumedCapacity: this.returnConsumedCapacity,
61
+ Item
62
+ })).then(() => (0, _aws_sdk_util_dynamodb.unmarshall)(Item));
63
+ };
64
+ this.deleteItem = async (key) => {
65
+ return this.dynamoDBClient.send(new _aws_sdk_client_dynamodb.DeleteItemCommand({
66
+ TableName: this.tableName,
67
+ Key: marshallKey(key),
68
+ ReturnValues: _aws_sdk_client_dynamodb.ReturnValue.ALL_OLD,
69
+ ReturnConsumedCapacity: this.returnConsumedCapacity
70
+ })).then((result) => result.Attributes ? (0, _aws_sdk_util_dynamodb.unmarshall)(result.Attributes) : void 0);
71
+ };
72
+ this.updateItem = async (key, updates, remove) => {
73
+ const filteredUpdateEntries = Object.entries(updates).filter(([, value]) => value !== void 0);
74
+ const hasUpdates = filteredUpdateEntries.length > 0;
75
+ if (!hasUpdates && !remove?.length) return this.getItem(key);
76
+ const setAttributesExpression = hasUpdates ? `SET ${filteredUpdateEntries.map(([key]) => `#${expressionAttributeKey(key)} = :${expressionAttributeKey(key)}`).join(", ")}` : "";
77
+ const removeAttributesExpression = remove?.length ? ` REMOVE ${remove.map((key) => `#${expressionAttributeKey(key)}`).join(", ")}` : "";
78
+ const removeAttributeNames = remove?.length ? remove.reduce((acc, key) => ({
79
+ ...acc,
80
+ [`#${expressionAttributeKey(key)}`]: key
81
+ }), {}) : {};
82
+ const updateItemCommandInput = {
83
+ TableName: this.tableName,
84
+ Key: marshallKey(key),
85
+ UpdateExpression: `${setAttributesExpression}${removeAttributesExpression}`,
86
+ ExpressionAttributeNames: filteredUpdateEntries.reduce((acc, [key]) => ({
87
+ ...acc,
88
+ [`#${expressionAttributeKey(key)}`]: key
89
+ }), Object.assign(removeAttributeNames)),
90
+ ExpressionAttributeValues: hasUpdates ? (0, _aws_sdk_util_dynamodb.marshall)(filteredUpdateEntries.reduce((acc, [key, value]) => ({
91
+ ...acc,
92
+ [`:${expressionAttributeKey(key)}`]: value
93
+ }), {}), { removeUndefinedValues: true }) : void 0,
94
+ ReturnConsumedCapacity: this.returnConsumedCapacity
95
+ };
96
+ return this.dynamoDBClient.send(new _aws_sdk_client_dynamodb.UpdateItemCommand({
97
+ ...updateItemCommandInput,
98
+ ReturnValues: "ALL_NEW"
99
+ })).then((result) => result.Attributes ? (0, _aws_sdk_util_dynamodb.unmarshall)(result.Attributes) : void 0);
100
+ };
101
+ this.getItems = async (query) => {
102
+ const { index, filterExpressions, projectedAttributes, limit, sortOrder, ...keys } = query;
103
+ const KeyConditionExpression = Object.keys(keys).map((key) => `#${expressionAttributeKey(key)} = :${expressionAttributeKey(key)}`).join(" AND ");
104
+ const keyExpressionAttributeNames = Object.keys(keys).reduce((acc, key) => ({
105
+ ...acc,
106
+ [`#${expressionAttributeKey(key)}`]: key
107
+ }), Object.assign({}));
108
+ const keyExpressionAttributeValues = Object.entries(keys).reduce((acc, [key, value]) => ({
109
+ ...acc,
110
+ [`:${expressionAttributeKey(key)}`]: value
111
+ }), Object.assign({}));
112
+ const gsiKeyAttributes = index ? [this.hashKey, ...this.rangKey ? [this.rangKey] : []] : [];
113
+ const ProjectionExpression = index ? gsiKeyAttributes.map((attr) => `#${expressionAttributeKey(attr)}`).join(",") : projectedAttributes ? projectedAttributes.map((attribute) => `#${expressionAttributeKey(attribute)}`).join(",") : void 0;
114
+ const projectionAttributeNames = index ? gsiKeyAttributes.reduce((acc, attr) => ({
115
+ ...acc,
116
+ [`#${expressionAttributeKey(attr)}`]: attr
117
+ }), {}) : projectedAttributes ? projectedAttributes.reduce((reduction, attribute) => ({
118
+ ...reduction,
119
+ [`#${expressionAttributeKey(attribute)}`]: attribute
120
+ }), Object.assign({})) : {};
121
+ const hasFilterExpressions = Array.isArray(filterExpressions) && filterExpressions.length > 0;
122
+ const FilterExpression = hasFilterExpressions ? mapFilterExpressions(filterExpressions) : void 0;
123
+ const filterAttributeNames = hasFilterExpressions ? filterExpressions.reduce((reduction, filterExpression) => ({
124
+ ...reduction,
125
+ [`#${expressionAttributeKey(filterExpression.attribute)}`]: filterExpression.attribute
126
+ }), Object.assign({})) : {};
127
+ const filterAttributeValues = filterExpressions ? filterExpressions.reduce((reduction, filterExpression) => ({
128
+ ...reduction,
129
+ ...mapFilterExpressionValues(filterExpression)
130
+ }), Object.assign({})) : {};
131
+ const Limit = limit;
132
+ const ScanIndexForward = sortOrder === "DESC" ? false : void 0;
133
+ const queryCommandInput = {
134
+ TableName: this.tableName,
135
+ ReturnConsumedCapacity: this.returnConsumedCapacity,
136
+ IndexName: index,
137
+ KeyConditionExpression,
138
+ FilterExpression,
139
+ ProjectionExpression,
140
+ ExpressionAttributeNames: {
141
+ ...keyExpressionAttributeNames,
142
+ ...filterAttributeNames,
143
+ ...projectionAttributeNames
144
+ },
145
+ ExpressionAttributeValues: (0, _aws_sdk_util_dynamodb.marshall)({
146
+ ...keyExpressionAttributeValues,
147
+ ...filterAttributeValues
148
+ }, { removeUndefinedValues: true }),
149
+ Limit,
150
+ ScanIndexForward
151
+ };
152
+ const paginator = (0, _aws_sdk_client_dynamodb.paginateQuery)({
153
+ client: this.dynamoDBClient,
154
+ pageSize: 100
155
+ }, queryCommandInput);
156
+ if (index) {
157
+ const collectedKeys = [];
158
+ for await (const page of paginator) {
159
+ if (page.Items) collectedKeys.push(...page.Items.map((item) => (0, _aws_sdk_util_dynamodb.unmarshall)(item)).map((item) => (0, lodash.pickBy)(item, (_, key) => key === this.hashKey || key === this.rangKey)));
160
+ if (limit && collectedKeys.length >= limit) break;
161
+ }
162
+ const keysBatch = limit ? collectedKeys.slice(0, limit) : collectedKeys;
163
+ const keyAttrs = [this.hashKey, ...this.rangKey ? [this.rangKey] : []];
164
+ const batchProjectedQuery = projectedAttributes ? {
165
+ ...query,
166
+ projectedAttributes: [...new Set([...projectedAttributes, ...keyAttrs])]
167
+ } : query;
168
+ const items = await this.batchGetItems(keysBatch, batchProjectedQuery);
169
+ const orderedItems = keysBatch.flatMap((key) => {
170
+ const k = key;
171
+ const match = items.find((item) => {
172
+ if (!item) return false;
173
+ const t = item;
174
+ return t[this.hashKey] === k[this.hashKey] && (!this.rangKey || t[this.rangKey] === k[this.rangKey]);
175
+ });
176
+ return match ? [match] : [];
177
+ });
178
+ if (projectedAttributes) {
179
+ const projSet = new Set(projectedAttributes);
180
+ return orderedItems.map((item) => (0, lodash.pickBy)(item, (_, key) => projSet.has(key)));
181
+ }
182
+ return orderedItems;
183
+ }
184
+ const items = [];
185
+ for await (const page of paginator) {
186
+ if (page.Items) items.push(...page.Items?.map((item) => (0, _aws_sdk_util_dynamodb.unmarshall)(item)) || []);
187
+ if (limit && items.length >= limit) break;
188
+ }
189
+ return limit ? items.slice(0, limit) : items;
190
+ };
191
+ this.getItemsPage = async (query) => {
192
+ const { index, filterExpressions, projectedAttributes, limit, sortOrder, cursor, ...keys } = query;
193
+ const KeyConditionExpression = Object.keys(keys).map((key) => `#${expressionAttributeKey(key)} = :${expressionAttributeKey(key)}`).join(" AND ");
194
+ const keyExpressionAttributeNames = Object.keys(keys).reduce((acc, key) => ({
195
+ ...acc,
196
+ [`#${expressionAttributeKey(key)}`]: key
197
+ }), Object.assign({}));
198
+ const keyExpressionAttributeValues = Object.entries(keys).reduce((acc, [key, value]) => ({
199
+ ...acc,
200
+ [`:${expressionAttributeKey(key)}`]: value
201
+ }), Object.assign({}));
202
+ const ProjectionExpression = !index && projectedAttributes ? projectedAttributes.map((attribute) => `#${expressionAttributeKey(attribute)}`).join(",") : void 0;
203
+ const projectionAttributeNames = !index && projectedAttributes ? projectedAttributes.reduce((reduction, attribute) => ({
204
+ ...reduction,
205
+ [`#${expressionAttributeKey(attribute)}`]: attribute
206
+ }), Object.assign({})) : {};
207
+ const hasFilterExpressions = Array.isArray(filterExpressions) && filterExpressions.length > 0;
208
+ const FilterExpression = hasFilterExpressions ? mapFilterExpressions(filterExpressions) : void 0;
209
+ const filterAttributeNames = hasFilterExpressions ? filterExpressions.reduce((reduction, filterExpression) => ({
210
+ ...reduction,
211
+ [`#${expressionAttributeKey(filterExpression.attribute)}`]: filterExpression.attribute
212
+ }), Object.assign({})) : {};
213
+ const filterAttributeValues = filterExpressions ? filterExpressions.reduce((reduction, filterExpression) => ({
214
+ ...reduction,
215
+ ...mapFilterExpressionValues(filterExpression)
216
+ }), Object.assign({})) : {};
217
+ const ExclusiveStartKey = cursor ? JSON.parse(Buffer.from(cursor, "base64").toString("utf-8")) : void 0;
218
+ const queryCommandInput = {
219
+ TableName: this.tableName,
220
+ ReturnConsumedCapacity: this.returnConsumedCapacity,
221
+ IndexName: index,
222
+ KeyConditionExpression,
223
+ FilterExpression,
224
+ ProjectionExpression,
225
+ ExpressionAttributeNames: {
226
+ ...keyExpressionAttributeNames,
227
+ ...filterAttributeNames,
228
+ ...projectionAttributeNames
229
+ },
230
+ ExpressionAttributeValues: (0, _aws_sdk_util_dynamodb.marshall)({
231
+ ...keyExpressionAttributeValues,
232
+ ...filterAttributeValues
233
+ }, { removeUndefinedValues: true }),
234
+ Limit: limit,
235
+ ScanIndexForward: sortOrder === "DESC" ? false : void 0,
236
+ ExclusiveStartKey: ExclusiveStartKey ? (0, _aws_sdk_util_dynamodb.marshall)(ExclusiveStartKey, { removeUndefinedValues: true }) : void 0
237
+ };
238
+ const result = await this.dynamoDBClient.send(new _aws_sdk_client_dynamodb.QueryCommand(queryCommandInput));
239
+ const nextCursor = result.LastEvaluatedKey ? Buffer.from(JSON.stringify((0, _aws_sdk_util_dynamodb.unmarshall)(result.LastEvaluatedKey))).toString("base64") : void 0;
240
+ if (index) {
241
+ const collectedKeys = (result.Items ?? []).map((item) => (0, _aws_sdk_util_dynamodb.unmarshall)(item)).map((item) => (0, lodash.pickBy)(item, (_, key) => key === this.hashKey || key === this.rangKey));
242
+ return {
243
+ items: (await this.batchGetItems(collectedKeys, query)).filter((item) => item !== void 0),
244
+ cursor: nextCursor
245
+ };
246
+ }
247
+ return {
248
+ items: (result.Items ?? []).map((item) => (0, _aws_sdk_util_dynamodb.unmarshall)(item)),
249
+ cursor: nextCursor
250
+ };
251
+ };
252
+ this.batchGetItems = async (keys, projectedQuery) => {
253
+ const keyPages = paginate((0, lodash.uniqWith)(keys, lodash.isEqual), 100);
254
+ const { projectedAttributes } = projectedQuery || {};
255
+ const ProjectionExpression = projectedAttributes ? projectedAttributes.map((attribute) => `#${expressionAttributeKey(attribute)}`).join(",") : void 0;
256
+ const ExpressionAttributeNames = projectedAttributes ? projectedAttributes.reduce((reduction, attribute) => ({
257
+ ...reduction,
258
+ [`#${expressionAttributeKey(attribute)}`]: attribute
259
+ }), Object.assign({})) : void 0;
260
+ return Promise.all(keyPages.map(async (keyPage) => {
261
+ const batchRequest = {
262
+ RequestItems: { [this.tableName]: {
263
+ Keys: keyPage.map((key) => marshallKey(key)),
264
+ ProjectionExpression,
265
+ ExpressionAttributeNames
266
+ } },
267
+ ReturnConsumedCapacity: this.returnConsumedCapacity
268
+ };
269
+ const fetchPage = async (request) => {
270
+ const result = await this.dynamoDBClient.send(new _aws_sdk_client_dynamodb.BatchGetItemCommand(request));
271
+ const retrieved = result.Responses?.[this.tableName]?.map((item) => (0, _aws_sdk_util_dynamodb.unmarshall)(item)) ?? [];
272
+ const unprocessed = result.UnprocessedKeys;
273
+ if (!unprocessed || Object.keys(unprocessed).length === 0) return retrieved;
274
+ return [...retrieved, ...await fetchPage({
275
+ RequestItems: unprocessed,
276
+ ReturnConsumedCapacity: this.returnConsumedCapacity
277
+ })];
278
+ };
279
+ return fetchPage(batchRequest);
280
+ })).then((itemSets) => itemSets.flat());
281
+ };
282
+ this.batchWriteItems = async (puts, deletes) => {
283
+ const requests = [...puts.map(({ key, item }) => ({ PutRequest: { Item: (0, _aws_sdk_util_dynamodb.marshall)({
284
+ ...item,
285
+ ...key
286
+ }, { removeUndefinedValues: true }) } })), ...deletes.map((key) => ({ DeleteRequest: { Key: marshallKey(key) } }))];
287
+ const sendBatch = async (requestItems) => {
288
+ const unprocessed = (await this.dynamoDBClient.send(new _aws_sdk_client_dynamodb.BatchWriteItemCommand({
289
+ RequestItems: requestItems,
290
+ ReturnConsumedCapacity: this.returnConsumedCapacity
291
+ }))).UnprocessedItems;
292
+ if (unprocessed && Object.keys(unprocessed).length > 0) await sendBatch(unprocessed);
293
+ };
294
+ await Promise.all(paginate(requests, 25).map((batch) => sendBatch({ [this.tableName]: batch })));
295
+ };
296
+ this.dynamoDBClient = options.client;
297
+ this.tableName = options.tableName;
298
+ this.hashKey = options.hashKey;
299
+ this.rangKey = options.rangeKey;
300
+ this.returnConsumedCapacity = options.returnConsumedCapacity ?? _aws_sdk_client_dynamodb.ReturnConsumedCapacity.TOTAL;
301
+ }
302
+ };
303
+
304
+ //#endregion
305
+ //#region src/consumed-capacity-middleware.ts
306
+ const consumedCapacityMiddleware = (consumedCapacityMiddlewareConfig) => (next) => async (args) => {
307
+ const { input } = args;
308
+ const returnConsumedCapacity = (0, lodash.get)(input, "ReturnConsumedCapacity");
309
+ const response = await next(args);
310
+ const { output } = response;
311
+ const consumedCapacity = (0, lodash.get)(output, "ConsumedCapacity");
312
+ await consumedCapacityMiddlewareConfig.onConsumedCapacity({
313
+ ReturnConsumedCapacity: returnConsumedCapacity,
314
+ ConsumedCapacity: consumedCapacity
315
+ });
316
+ return response;
317
+ };
318
+
319
+ //#endregion
320
+ //#region src/JsonPointerRepository.ts
321
+ const isJsonPointerValue = (value) => value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean";
322
+ const escapeToken = (token) => token.replace(/~/g, "~0").replace(/\//g, "~1");
323
+ const unescapeToken = (token) => token.replace(/~1/g, "/").replace(/~0/g, "~");
324
+ const validatePointer = (pointer) => {
325
+ if (!pointer.startsWith("/")) throw new TypeError(`Invalid JSON Pointer "${pointer}": must start with "/".`);
326
+ };
327
+ const flattenDocument = (doc, prefix = "") => {
328
+ if (doc === null) return { [prefix]: null };
329
+ if (typeof doc !== "object") {
330
+ if (isJsonPointerValue(doc)) return { [prefix]: doc };
331
+ const pointer = prefix === "" ? "/" : prefix;
332
+ throw new TypeError(`Unsupported value at JSON pointer "${pointer}": expected string, number, boolean, or null but received ${typeof doc}.`);
333
+ }
334
+ const result = {};
335
+ if (Array.isArray(doc)) for (let i = 0; i < doc.length; i++) Object.assign(result, flattenDocument(doc[i], `${prefix}/${i}`));
336
+ else for (const [key, value] of Object.entries(doc)) Object.assign(result, flattenDocument(value, `${prefix}/${escapeToken(key)}`));
337
+ return result;
338
+ };
339
+ const setAtPointer = (root, pointer, value) => {
340
+ const tokens = pointer.split("/").slice(1).map(unescapeToken);
341
+ if (tokens.length === 0) return;
342
+ let current = root;
343
+ for (let i = 0; i < tokens.length - 1; i++) {
344
+ const token = tokens[i];
345
+ const nextToken = tokens[i + 1];
346
+ if (current[token] == null) current[token] = /^\d+$/.test(nextToken) ? [] : {};
347
+ else if (typeof current[token] !== "object") throw new TypeError(`Conflicting pointers: cannot traverse "${pointer}" because "/${tokens.slice(0, i + 1).join("/")}" already holds a primitive value.`);
348
+ current = current[token];
349
+ }
350
+ const lastToken = tokens[tokens.length - 1];
351
+ current[lastToken] = value;
352
+ };
353
+ const reconstructDocument = (items) => {
354
+ const root = {};
355
+ const sorted = [...items].sort((a, b) => a.pointer.localeCompare(b.pointer));
356
+ for (const item of sorted) setAtPointer(root, item.pointer, item.value);
357
+ return root;
358
+ };
359
+ /**
360
+ * Stores JSON documents as individual per-pointer DynamoDB items addressed by JSON Pointer (RFC 6901).
361
+ *
362
+ * Only JSON objects are supported as the root document (`T extends Record<string, unknown>`).
363
+ * Root arrays and primitives are not supported. Documents must contain at least one leaf value;
364
+ * empty objects and empty arrays are not supported.
365
+ */
366
+ var JsonPointerRepository = class {
367
+ constructor(options) {
368
+ this.putDocument = async (id, document) => {
369
+ const flat = flattenDocument(document);
370
+ const newPointers = Object.keys(flat);
371
+ if (newPointers.length === 0) throw new TypeError("Cannot store a document with no leaf values. Empty objects and arrays are not supported.");
372
+ const newPointerSet = new Set(newPointers);
373
+ const stalePointers = (await this.repository.getItems({ [this.idKey]: id }) ?? []).map((item) => item[this.pointerKey]).filter((p) => !newPointerSet.has(p));
374
+ const puts = Object.entries(flat).map(([pointer, value]) => {
375
+ const key = {
376
+ [this.idKey]: id,
377
+ [this.pointerKey]: pointer
378
+ };
379
+ return {
380
+ key,
381
+ item: {
382
+ ...key,
383
+ [this.valueKey]: value
384
+ }
385
+ };
386
+ });
387
+ const deleteKeys = stalePointers.map((pointer) => ({
388
+ [this.idKey]: id,
389
+ [this.pointerKey]: pointer
390
+ }));
391
+ await this.repository.batchWriteItems(puts, deleteKeys);
392
+ };
393
+ this.getDocument = async (id) => {
394
+ const items = await this.repository.getItems({ [this.idKey]: id });
395
+ if (!items || items.length === 0) return void 0;
396
+ return reconstructDocument(items.map((item) => ({
397
+ pointer: item[this.pointerKey],
398
+ value: item[this.valueKey]
399
+ })));
400
+ };
401
+ this.getAttribute = async (id, pointer) => {
402
+ validatePointer(pointer);
403
+ const item = await this.repository.getItem({
404
+ [this.idKey]: id,
405
+ [this.pointerKey]: pointer
406
+ });
407
+ return item ? item[this.valueKey] : void 0;
408
+ };
409
+ this.putAttribute = async (id, pointer, value) => {
410
+ validatePointer(pointer);
411
+ const key = {
412
+ [this.idKey]: id,
413
+ [this.pointerKey]: pointer
414
+ };
415
+ await this.repository.putItem(key, {
416
+ ...key,
417
+ [this.valueKey]: value
418
+ });
419
+ };
420
+ this.deleteAttribute = async (id, pointer) => {
421
+ validatePointer(pointer);
422
+ await this.repository.deleteItem({
423
+ [this.idKey]: id,
424
+ [this.pointerKey]: pointer
425
+ });
426
+ };
427
+ this.deleteDocument = async (id) => {
428
+ const items = await this.repository.getItems({ [this.idKey]: id }) ?? [];
429
+ if (items.length === 0) return;
430
+ const deleteKeys = items.map((item) => ({
431
+ [this.idKey]: id,
432
+ [this.pointerKey]: item[this.pointerKey]
433
+ }));
434
+ await this.repository.batchWriteItems([], deleteKeys);
435
+ };
436
+ this.patchDocument = async (id, updates) => {
437
+ const entries = Object.entries(updates);
438
+ if (entries.length === 0) return;
439
+ entries.forEach(([pointer]) => validatePointer(pointer));
440
+ const puts = entries.filter((entry) => entry[1] !== void 0).map(([pointer, value]) => {
441
+ const key = {
442
+ [this.idKey]: id,
443
+ [this.pointerKey]: pointer
444
+ };
445
+ return {
446
+ key,
447
+ item: {
448
+ ...key,
449
+ [this.valueKey]: value
450
+ }
451
+ };
452
+ });
453
+ const deleteKeys = entries.filter(([, value]) => value === void 0).map(([pointer]) => ({
454
+ [this.idKey]: id,
455
+ [this.pointerKey]: pointer
456
+ }));
457
+ await this.repository.batchWriteItems(puts, deleteKeys);
458
+ };
459
+ const { client, tableName, idKey = "id", pointerKey = "pointer", valueKey = "value", returnConsumedCapacity } = options;
460
+ this.idKey = idKey;
461
+ this.pointerKey = pointerKey;
462
+ this.valueKey = valueKey;
463
+ this.repository = new DynamoDbRepository({
464
+ client,
465
+ tableName,
466
+ hashKey: idKey,
467
+ rangeKey: pointerKey,
468
+ returnConsumedCapacity
469
+ });
470
+ }
471
+ };
472
+
473
+ //#endregion
474
+ exports.DynamoDbRepository = DynamoDbRepository;
475
+ exports.FilterOperator = FilterOperator;
476
+ exports.JsonPointerRepository = JsonPointerRepository;
477
+ exports.consumedCapacityMiddleware = consumedCapacityMiddleware;
478
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["GetItemCommand","PutItemCommand","DeleteItemCommand","ReturnValue","UpdateItemCommand","QueryCommand","isEqual","BatchGetItemCommand","BatchWriteItemCommand","ReturnConsumedCapacity"],"sources":["../src/DynamoDbRepository.ts","../src/consumed-capacity-middleware.ts","../src/JsonPointerRepository.ts"],"sourcesContent":["import {\n BatchGetItemCommand,\n BatchGetItemCommandInput,\n BatchWriteItemCommand,\n DeleteItemCommand,\n DynamoDBClient,\n GetItemCommand,\n paginateQuery,\n PutItemCommand,\n QueryCommand,\n QueryCommandInput,\n ReturnConsumedCapacity,\n ReturnValue,\n UpdateItemCommand,\n WriteRequest,\n} from \"@aws-sdk/client-dynamodb\";\nimport {marshall, unmarshall, NativeAttributeValue} from \"@aws-sdk/util-dynamodb\";\nimport {replace, uniqWith, isEqual, pickBy} from \"lodash\";\n\nexport enum FilterOperator {\n EQUALS = \"=\",\n NOT_EQUALS = \"<>\",\n GREATER_THAN_OR_EQUALS = \">=\",\n GREATER_THAN = \">\",\n LESS_THAN = \"<\",\n LESS_THAN_OR_EQUALS = \"<=\",\n IN = \"IN\",\n BETWEEN = \"BETWEEN\",\n BEGINS_WITH = \"BEGINS_WITH\",\n CONTAINS = \"CONTAINS\",\n}\n\nexport interface FilterExpression {\n attribute: string;\n value:\n | string\n | number\n | boolean\n | Array<string | number>\n | [string, string]\n | [number, number];\n operator: FilterOperator;\n negate?: boolean;\n}\n\nexport interface FilterableQuery {\n filterExpressions: Array<FilterExpression>;\n}\n\nexport interface ProjectedQuery {\n projectedAttributes: string[];\n}\n\nexport interface IndexedQuery {\n index: string;\n}\n\nexport interface Query extends Partial<FilterableQuery>, Partial<ProjectedQuery>, Partial<IndexedQuery> {\n [key: string]: unknown;\n filterExpressions?: Array<FilterExpression>;\n projectedAttributes?: string[];\n index?: string;\n sortOrder?: \"ASC\" | \"DESC\";\n limit?: number\n}\n\nconst marshallKey = (key: unknown) =>\n marshall(key as Record<string, NativeAttributeValue>, {removeUndefinedValues: true});\n\nconst expressionAttributeKey = (key: string) => replace(key, /-/g, \"_\");\n\nconst mapInKeys = (filterExpression: FilterExpression) =>\n Array.isArray(filterExpression.value)\n ? filterExpression.value.map(\n (_, index) => `:${expressionAttributeKey(filterExpression.attribute)}${index}`,\n )\n : `:${expressionAttributeKey(filterExpression.attribute)}`;\n\nconst mapFilterExpression = (filterExpression: FilterExpression) => {\n switch (filterExpression.operator) {\n case FilterOperator.IN:\n return (\n `#${expressionAttributeKey(filterExpression.attribute)} ${filterExpression.operator} ` +\n `(${mapInKeys(filterExpression)})`\n );\n case FilterOperator.BETWEEN:\n return (\n `#${expressionAttributeKey(filterExpression.attribute)} ${filterExpression.operator} ` +\n `:${expressionAttributeKey(filterExpression.attribute)}0 AND :${expressionAttributeKey(filterExpression.attribute)}1`\n );\n case FilterOperator.BEGINS_WITH:\n case FilterOperator.CONTAINS:\n return (\n `${filterExpression.operator.toLowerCase()}(#${expressionAttributeKey(filterExpression.attribute)}, ` +\n `:${expressionAttributeKey(filterExpression.attribute)})`\n );\n default:\n return (\n `#${expressionAttributeKey(filterExpression.attribute)} ${filterExpression.operator} ` +\n `:${expressionAttributeKey(filterExpression.attribute)}`\n );\n }\n};\n\nconst mapFilterExpressions = (\n filterExpressions: Array<FilterExpression>,\n) =>\n filterExpressions\n .map((filterExpression) =>\n filterExpression.negate\n ? `NOT ${mapFilterExpression(filterExpression)}`\n : mapFilterExpression(filterExpression),\n )\n .join(\" AND \");\n\nconst mapFilterExpressionValues = (\n filterExpression: FilterExpression,\n): Record<string, string | number | boolean> =>\n Array.isArray(filterExpression.value)\n ? filterExpression.value.reduce(\n (reduction, value, index) => ({\n ...reduction,\n [`:${expressionAttributeKey(filterExpression.attribute)}${index}`]: value,\n }),\n Object.assign({}),\n )\n : {\n [`:${expressionAttributeKey(filterExpression.attribute)}`]:\n filterExpression.value,\n };\n\nconst paginate = <T>(array: Array<T>, pageSize: number) => {\n return array.reduce((acc, val, i) => {\n const idx = Math.floor(i / pageSize)\n const page = acc[idx] || (acc[idx] = [])\n page.push(val)\n return acc\n }, [] as Array<Array<T>>);\n}\n\nexport interface DynamoDbRepositoryOptions {\n client: DynamoDBClient;\n tableName: string;\n hashKey: string;\n rangeKey?: string;\n returnConsumedCapacity?: ReturnConsumedCapacity;\n}\n\nexport interface PageResult<T> {\n items: Array<T>;\n cursor?: string;\n}\n\nexport class DynamoDbRepository<K, T> {\n private readonly dynamoDBClient: DynamoDBClient;\n private readonly tableName: string;\n private readonly hashKey: string;\n private readonly rangKey?: string;\n private readonly returnConsumedCapacity: ReturnConsumedCapacity | undefined;\n\n constructor(options: DynamoDbRepositoryOptions) {\n this.dynamoDBClient = options.client;\n this.tableName = options.tableName;\n this.hashKey = options.hashKey;\n this.rangKey = options.rangeKey;\n this.returnConsumedCapacity = options.returnConsumedCapacity ?? ReturnConsumedCapacity.TOTAL;\n }\n\n getItem = async (key: K): Promise<T | undefined> => {\n return this.dynamoDBClient\n .send(\n new GetItemCommand({\n TableName: this.tableName,\n Key: marshallKey(key),\n ReturnConsumedCapacity: this.returnConsumedCapacity,\n }),\n )\n .then((result) =>\n result.Item ? unmarshall(result.Item) as T : undefined,\n )\n };\n\n putItem = async (key: K, record: T): Promise<T> => {\n const Item = marshall({...record, ...key} as Record<string, NativeAttributeValue>, {removeUndefinedValues: true});\n return this.dynamoDBClient\n .send(\n new PutItemCommand({\n TableName: this.tableName,\n ReturnConsumedCapacity: this.returnConsumedCapacity,\n Item,\n }),\n )\n .then(() => unmarshall(Item) as T);\n };\n\n deleteItem = async (key: K): Promise<T | undefined> => {\n return this.dynamoDBClient.send(new DeleteItemCommand({\n TableName: this.tableName,\n Key: marshallKey(key),\n ReturnValues: ReturnValue.ALL_OLD,\n ReturnConsumedCapacity: this.returnConsumedCapacity,\n })).then((result) => result.Attributes ?\n unmarshall(result.Attributes) as T : undefined);\n };\n\n\n updateItem = async (\n key: K,\n updates: Partial<T>,\n remove?: string[],\n ): Promise<T | undefined> => {\n const filteredUpdateEntries = Object.entries(updates).filter(([, value]) => value !== undefined);\n const hasUpdates = filteredUpdateEntries.length > 0;\n if (!hasUpdates && !remove?.length) {\n return this.getItem(key);\n }\n const setAttributesExpression = hasUpdates ? `SET ${filteredUpdateEntries\n .map(\n ([key]) =>\n `#${expressionAttributeKey(key)} = :${expressionAttributeKey(key)}`,\n )\n .join(\", \")}` : '';\n const removeAttributesExpression = remove?.length\n ? ` REMOVE ${remove.map((key) => `#${expressionAttributeKey(key)}`).join(\", \")}`\n : \"\";\n const removeAttributeNames = remove?.length\n ? remove.reduce(\n (acc, key) => ({\n ...acc,\n [`#${expressionAttributeKey(key)}`]: key,\n }),\n {} as Record<string, string>,\n )\n : {};\n const updateItemCommandInput = {\n TableName: this.tableName,\n Key: marshallKey(key),\n UpdateExpression: `${setAttributesExpression}${removeAttributesExpression}`,\n ExpressionAttributeNames: filteredUpdateEntries\n .reduce(\n (acc, [key]) => ({\n ...acc,\n [`#${expressionAttributeKey(key)}`]: key,\n }),\n Object.assign(\n removeAttributeNames,\n ),\n ) as Record<string, string>,\n ExpressionAttributeValues: hasUpdates ? marshall(\n filteredUpdateEntries.reduce(\n (acc, [key, value]) => ({\n ...acc,\n [`:${expressionAttributeKey(key)}`]: value,\n }),\n {} as Record<string, NativeAttributeValue>,\n ),\n {removeUndefinedValues: true},\n ) : undefined,\n ReturnConsumedCapacity: this.returnConsumedCapacity,\n };\n return this.dynamoDBClient\n .send(new UpdateItemCommand({...updateItemCommandInput, ReturnValues: 'ALL_NEW'}))\n .then((result) => result.Attributes ? unmarshall(result.Attributes) as T : undefined);\n };\n\n\n getItems = async (\n query: Query\n ): Promise<Array<T> | undefined> => {\n const {index, filterExpressions, projectedAttributes, limit, sortOrder,...keys} = query;\n const KeyConditionExpression = Object.keys(keys)\n .map((key) => `#${expressionAttributeKey(key)} = :${expressionAttributeKey(key)}`).join(' AND ');\n const keyExpressionAttributeNames = Object.keys(keys)\n .reduce((acc, key) => ({...acc, [`#${expressionAttributeKey(key)}`]: key}), Object.assign({}));\n const keyExpressionAttributeValues = Object.entries(keys)\n .reduce((acc, [key, value]) => ({...acc, [`:${expressionAttributeKey(key)}`]: value}), Object.assign({}));\n\n const gsiKeyAttributes = index\n ? [this.hashKey, ...(this.rangKey ? [this.rangKey] : [])]\n : [];\n\n const ProjectionExpression = index\n ? gsiKeyAttributes.map((attr) => `#${expressionAttributeKey(attr)}`).join(',')\n : (projectedAttributes\n ? projectedAttributes.map((attribute) => `#${expressionAttributeKey(attribute)}`).join(',')\n : undefined);\n\n const projectionAttributeNames: Record<string, string> = index\n ? gsiKeyAttributes.reduce(\n (acc: Record<string, string>, attr: string) => ({\n ...acc,\n [`#${expressionAttributeKey(attr)}`]: attr,\n }),\n {},\n )\n : (projectedAttributes ? projectedAttributes.reduce(\n (\n reduction: Record<string, string>,\n attribute: string,\n ) => ({\n ...reduction,\n [`#${expressionAttributeKey(attribute)}`]:\n attribute,\n }),\n Object.assign({}),\n ) : {})\n const hasFilterExpressions = Array.isArray(filterExpressions) && filterExpressions.length > 0;\n const FilterExpression = hasFilterExpressions\n ? mapFilterExpressions(filterExpressions!)\n : undefined;\n const filterAttributeNames: Record<string, string> = hasFilterExpressions\n ? filterExpressions!.reduce(\n (\n reduction: Record<string, string>,\n filterExpression: FilterExpression,\n ) => ({\n ...reduction,\n [`#${expressionAttributeKey(filterExpression.attribute)}`]:\n filterExpression.attribute,\n }),\n Object.assign({}),\n )\n : {};\n const filterAttributeValues = filterExpressions\n ? filterExpressions.reduce(\n (reduction, filterExpression) => ({\n ...reduction,\n ...mapFilterExpressionValues(filterExpression),\n }),\n Object.assign({}),\n )\n : {};\n\n const Limit = limit;\n const ScanIndexForward = sortOrder === \"DESC\" ? false : undefined;\n const queryCommandInput: QueryCommandInput = {\n TableName: this.tableName,\n ReturnConsumedCapacity: this.returnConsumedCapacity,\n IndexName: index,\n KeyConditionExpression,\n FilterExpression,\n ProjectionExpression,\n ExpressionAttributeNames: {\n ...keyExpressionAttributeNames,\n ...filterAttributeNames,\n ...projectionAttributeNames\n },\n ExpressionAttributeValues: marshall(\n {...keyExpressionAttributeValues, ...filterAttributeValues} as Record<string, NativeAttributeValue>,\n {removeUndefinedValues: true},\n ),\n Limit,\n ScanIndexForward\n };\n const paginator = paginateQuery(\n {client: this.dynamoDBClient, pageSize: 100},\n queryCommandInput,\n );\n\n if (index) {\n const collectedKeys: Array<K> = [];\n for await (const page of paginator) {\n if (page.Items) {\n collectedKeys.push(\n ...(page.Items.map((item) => unmarshall(item) as T)\n .map((item: T) =>\n pickBy(item as object, (_, key) => (key === this.hashKey || key === this.rangKey)) as K)),\n )\n }\n if (limit && collectedKeys.length >= limit) break;\n }\n const keysBatch = limit ? collectedKeys.slice(0, limit) : collectedKeys;\n const keyAttrs = [this.hashKey, ...(this.rangKey ? [this.rangKey] : [])];\n const batchProjectedQuery = projectedAttributes\n ? { ...query, projectedAttributes: [...new Set([...projectedAttributes, ...keyAttrs])] } as ProjectedQuery\n : query as ProjectedQuery;\n const items = await this.batchGetItems(keysBatch, batchProjectedQuery);\n const orderedItems = keysBatch.flatMap((key) => {\n const k = key as Record<string, unknown>;\n const match = (items as Array<T | undefined>).find((item) => {\n if (!item) return false;\n const t = item as Record<string, unknown>;\n return t[this.hashKey] === k[this.hashKey] &&\n (!this.rangKey || t[this.rangKey] === k[this.rangKey]);\n });\n return match ? [match] : [];\n });\n if (projectedAttributes) {\n const projSet = new Set(projectedAttributes);\n return orderedItems.map(item =>\n pickBy(item as object, (_, key) => projSet.has(key)) as T\n ) as Array<T>;\n }\n return orderedItems as Array<T>;\n }\n\n const items: Array<T> = [];\n for await (const page of paginator) {\n if (page.Items) {\n items.push(\n ...(page.Items?.map((item) => unmarshall(item) as T) || []),\n )\n }\n if (limit && items.length >= limit) break;\n }\n return limit ? items.slice(0, limit) : items;\n };\n\n\n getItemsPage = async (\n query: Query & { cursor?: string }\n ): Promise<PageResult<T>> => {\n const {index, filterExpressions, projectedAttributes, limit, sortOrder, cursor, ...keys} = query;\n const KeyConditionExpression = Object.keys(keys)\n .map((key) => `#${expressionAttributeKey(key)} = :${expressionAttributeKey(key)}`).join(' AND ');\n const keyExpressionAttributeNames = Object.keys(keys)\n .reduce((acc, key) => ({...acc, [`#${expressionAttributeKey(key)}`]: key}), Object.assign({}));\n const keyExpressionAttributeValues = Object.entries(keys)\n .reduce((acc, [key, value]) => ({...acc, [`:${expressionAttributeKey(key)}`]: value}), Object.assign({}));\n\n const ProjectionExpression = !index && projectedAttributes\n ? projectedAttributes.map((attribute) => `#${expressionAttributeKey(attribute)}`).join(',')\n : undefined;\n const projectionAttributeNames: Record<string, string> = !index && projectedAttributes ? projectedAttributes.reduce(\n (reduction: Record<string, string>, attribute: string) => ({\n ...reduction,\n [`#${expressionAttributeKey(attribute)}`]: attribute,\n }),\n Object.assign({}),\n ) : {};\n const hasFilterExpressions = Array.isArray(filterExpressions) && filterExpressions.length > 0;\n const FilterExpression = hasFilterExpressions\n ? mapFilterExpressions(filterExpressions!)\n : undefined;\n const filterAttributeNames: Record<string, string> = hasFilterExpressions\n ? filterExpressions!.reduce(\n (reduction: Record<string, string>, filterExpression: FilterExpression) => ({\n ...reduction,\n [`#${expressionAttributeKey(filterExpression.attribute)}`]: filterExpression.attribute,\n }),\n Object.assign({}),\n )\n : {};\n const filterAttributeValues = filterExpressions\n ? filterExpressions.reduce(\n (reduction, filterExpression) => ({\n ...reduction,\n ...mapFilterExpressionValues(filterExpression),\n }),\n Object.assign({}),\n )\n : {};\n\n const ExclusiveStartKey = cursor\n ? JSON.parse(Buffer.from(cursor, 'base64').toString('utf-8')) as Record<string, unknown>\n : undefined;\n\n const queryCommandInput: QueryCommandInput = {\n TableName: this.tableName,\n ReturnConsumedCapacity: this.returnConsumedCapacity,\n IndexName: index,\n KeyConditionExpression,\n FilterExpression,\n ProjectionExpression,\n ExpressionAttributeNames: {\n ...keyExpressionAttributeNames,\n ...filterAttributeNames,\n ...projectionAttributeNames\n },\n ExpressionAttributeValues: marshall(\n {...keyExpressionAttributeValues, ...filterAttributeValues} as Record<string, NativeAttributeValue>,\n {removeUndefinedValues: true},\n ),\n Limit: limit,\n ScanIndexForward: sortOrder === \"DESC\" ? false : undefined,\n ExclusiveStartKey: ExclusiveStartKey\n ? marshall(ExclusiveStartKey as Record<string, NativeAttributeValue>, {removeUndefinedValues: true})\n : undefined,\n };\n\n const result = await this.dynamoDBClient.send(new QueryCommand(queryCommandInput));\n const nextCursor = result.LastEvaluatedKey\n ? Buffer.from(JSON.stringify(unmarshall(result.LastEvaluatedKey))).toString('base64')\n : undefined;\n\n if (index) {\n const collectedKeys = (result.Items ?? [])\n .map((item) => unmarshall(item) as T)\n .map((item: T) =>\n pickBy(item as object, (_, key) => (key === this.hashKey || key === this.rangKey)) as K\n );\n const items = await this.batchGetItems(collectedKeys, query as ProjectedQuery);\n return {items: items.filter((item): item is T => item !== undefined), cursor: nextCursor};\n }\n\n const items = (result.Items ?? []).map((item) => unmarshall(item) as T);\n return {items, cursor: nextCursor};\n };\n\n\n batchGetItems = async (\n keys: K[], projectedQuery?: ProjectedQuery\n ): Promise<Array<T | undefined>> => {\n const uniqueKeys = uniqWith(keys, isEqual);\n const keyPages = paginate(uniqueKeys, 100);\n const {projectedAttributes} = projectedQuery || {};\n const ProjectionExpression = projectedAttributes\n ? projectedAttributes.map((attribute) => `#${expressionAttributeKey(attribute)}`).join(',')\n : undefined;\n const ExpressionAttributeNames = projectedAttributes ?\n projectedAttributes.reduce(\n (\n reduction: Record<string, string>,\n attribute: string,\n ) => ({\n ...reduction,\n [`#${expressionAttributeKey(attribute)}`]:\n attribute,\n }),\n Object.assign({}),\n ) : undefined;\n return Promise.all((keyPages.map(async (keyPage) => {\n const batchRequest: BatchGetItemCommandInput = {\n RequestItems: {\n [this.tableName]: {\n Keys: keyPage.map((key) => marshallKey(key)),\n ProjectionExpression,\n ExpressionAttributeNames,\n }\n },\n ReturnConsumedCapacity: this.returnConsumedCapacity,\n }\n\n const fetchPage = async (request: BatchGetItemCommandInput): Promise<T[]> => {\n const result = await this.dynamoDBClient.send(new BatchGetItemCommand(request));\n const retrieved = result.Responses?.[this.tableName]?.map((item) => unmarshall(item) as T) ?? [];\n const unprocessed = result.UnprocessedKeys;\n if (!unprocessed || Object.keys(unprocessed).length === 0) return retrieved;\n return [...retrieved, ...await fetchPage({\n RequestItems: unprocessed,\n ReturnConsumedCapacity: this.returnConsumedCapacity,\n })];\n };\n\n return fetchPage(batchRequest);\n })))\n .then((itemSets) => itemSets.flat());\n };\n\n batchWriteItems = async (\n puts: { key: K; item: T }[],\n deletes: K[],\n ): Promise<void> => {\n const requests: WriteRequest[] = [\n ...puts.map(({ key, item }) => ({\n PutRequest: {\n Item: marshall(\n { ...item, ...key } as Record<string, NativeAttributeValue>,\n { removeUndefinedValues: true },\n ),\n },\n })),\n ...deletes.map((key) => ({\n DeleteRequest: { Key: marshallKey(key) },\n })),\n ];\n\n const sendBatch = async (requestItems: Record<string, WriteRequest[]>): Promise<void> => {\n const result = await this.dynamoDBClient.send(\n new BatchWriteItemCommand({\n RequestItems: requestItems,\n ReturnConsumedCapacity: this.returnConsumedCapacity,\n }),\n );\n const unprocessed = result.UnprocessedItems;\n if (unprocessed && Object.keys(unprocessed).length > 0) {\n await sendBatch(unprocessed as Record<string, WriteRequest[]>);\n }\n };\n\n await Promise.all(\n paginate(requests, 25).map(batch => sendBatch({ [this.tableName]: batch })),\n );\n };\n}\n\n","import {\n ConsumedCapacity,\n ReturnConsumedCapacity,\n ServiceInputTypes,\n ServiceOutputTypes,\n} from \"@aws-sdk/client-dynamodb\";\n\nimport {\n InitializeHandler,\n InitializeHandlerArguments,\n InitializeHandlerOutput,\n} from \"@smithy/types\";\n\nimport {get} from \"lodash\";\n\nexport interface ConsumedCapacityDetail {\n ReturnConsumedCapacity: ReturnConsumedCapacity | undefined\n ConsumedCapacity: ConsumedCapacity | ConsumedCapacity[] | undefined\n}\n\nexport interface ConsumedCapacityMiddlewareConfig {\n onConsumedCapacity: (consumedCapacity: ConsumedCapacityDetail) => Promise<unknown>;\n}\n\nexport const consumedCapacityMiddleware =\n (consumedCapacityMiddlewareConfig: ConsumedCapacityMiddlewareConfig) =>\n (next: InitializeHandler<ServiceInputTypes, ServiceOutputTypes>) =>\n async (args: InitializeHandlerArguments<ServiceInputTypes>): Promise<InitializeHandlerOutput<ServiceOutputTypes>> => {\n const {input} = args;\n const returnConsumedCapacity = get(input, \"ReturnConsumedCapacity\") as ReturnConsumedCapacity | undefined;\n const response = await next(args);\n const {output} = response;\n const consumedCapacity = get(output, \"ConsumedCapacity\") as ConsumedCapacity | ConsumedCapacity[] | undefined;\n await consumedCapacityMiddlewareConfig.onConsumedCapacity({ReturnConsumedCapacity: returnConsumedCapacity, ConsumedCapacity: consumedCapacity});\n return response;\n };","import { DynamoDBClient, ReturnConsumedCapacity } from \"@aws-sdk/client-dynamodb\";\nimport { DynamoDbRepository } from \"./DynamoDbRepository\";\n\nexport type JsonPointerValue = string | number | boolean | null;\n\nexport interface JsonPointerRepositoryOptions {\n client: DynamoDBClient;\n tableName: string;\n idKey?: string;\n pointerKey?: string;\n valueKey?: string;\n returnConsumedCapacity?: ReturnConsumedCapacity;\n}\n\ntype PointerKey = Record<string, string>;\ntype PointerItem = Record<string, unknown>;\n\nconst isJsonPointerValue = (value: unknown): value is JsonPointerValue =>\n value === null ||\n typeof value === \"string\" ||\n typeof value === \"number\" ||\n typeof value === \"boolean\";\n\nconst escapeToken = (token: string): string =>\n token.replace(/~/g, \"~0\").replace(/\\//g, \"~1\");\n\nconst unescapeToken = (token: string): string =>\n token.replace(/~1/g, \"/\").replace(/~0/g, \"~\");\n\nconst validatePointer = (pointer: string): void => {\n if (!pointer.startsWith(\"/\")) {\n throw new TypeError(`Invalid JSON Pointer \"${pointer}\": must start with \"/\".`);\n }\n};\n\nconst flattenDocument = (doc: unknown, prefix = \"\"): Record<string, JsonPointerValue> => {\n if (doc === null) return { [prefix]: null };\n if (typeof doc !== \"object\") {\n if (isJsonPointerValue(doc)) {\n return { [prefix]: doc };\n }\n const pointer = prefix === \"\" ? \"/\" : prefix;\n throw new TypeError(\n `Unsupported value at JSON pointer \"${pointer}\": expected string, number, boolean, or null but received ${typeof doc}.`,\n );\n }\n\n const result: Record<string, JsonPointerValue> = {};\n\n if (Array.isArray(doc)) {\n for (let i = 0; i < doc.length; i++) {\n Object.assign(result, flattenDocument(doc[i], `${prefix}/${i}`));\n }\n } else {\n for (const [key, value] of Object.entries(doc)) {\n Object.assign(result, flattenDocument(value, `${prefix}/${escapeToken(key)}`));\n }\n }\n\n return result;\n};\n\nconst setAtPointer = (\n root: Record<string, unknown>,\n pointer: string,\n value: JsonPointerValue,\n): void => {\n const tokens = pointer.split(\"/\").slice(1).map(unescapeToken);\n if (tokens.length === 0) return;\n\n let current: Record<string, unknown> = root;\n for (let i = 0; i < tokens.length - 1; i++) {\n const token = tokens[i];\n const nextToken = tokens[i + 1];\n if (current[token] == null) {\n current[token] = /^\\d+$/.test(nextToken) ? [] : {};\n } else if (typeof current[token] !== \"object\") {\n throw new TypeError(\n `Conflicting pointers: cannot traverse \"${pointer}\" because \"/${tokens.slice(0, i + 1).join(\"/\")}\" already holds a primitive value.`,\n );\n }\n current = current[token] as Record<string, unknown>;\n }\n\n const lastToken = tokens[tokens.length - 1];\n current[lastToken] = value;\n};\n\nconst reconstructDocument = <T>(\n items: Array<{ pointer: string; value: JsonPointerValue }>,\n): T => {\n const root: Record<string, unknown> = {};\n const sorted = [...items].sort((a, b) => a.pointer.localeCompare(b.pointer));\n for (const item of sorted) {\n setAtPointer(root, item.pointer, item.value);\n }\n return root as T;\n};\n\n/**\n * Stores JSON documents as individual per-pointer DynamoDB items addressed by JSON Pointer (RFC 6901).\n *\n * Only JSON objects are supported as the root document (`T extends Record<string, unknown>`).\n * Root arrays and primitives are not supported. Documents must contain at least one leaf value;\n * empty objects and empty arrays are not supported.\n */\nexport class JsonPointerRepository<T extends Record<string, unknown>> {\n private readonly repository: DynamoDbRepository<PointerKey, PointerItem>;\n private readonly idKey: string;\n private readonly pointerKey: string;\n private readonly valueKey: string;\n\n constructor(options: JsonPointerRepositoryOptions) {\n const {\n client,\n tableName,\n idKey = \"id\",\n pointerKey = \"pointer\",\n valueKey = \"value\",\n returnConsumedCapacity,\n } = options;\n\n this.idKey = idKey;\n this.pointerKey = pointerKey;\n this.valueKey = valueKey;\n\n this.repository = new DynamoDbRepository<PointerKey, PointerItem>({\n client,\n tableName,\n hashKey: idKey,\n rangeKey: pointerKey,\n returnConsumedCapacity,\n });\n }\n\n putDocument = async (id: string, document: T): Promise<void> => {\n const flat = flattenDocument(document);\n const newPointers = Object.keys(flat);\n\n if (newPointers.length === 0) {\n throw new TypeError(\n \"Cannot store a document with no leaf values. Empty objects and arrays are not supported.\",\n );\n }\n\n const newPointerSet = new Set(newPointers);\n const existing = await this.repository.getItems({ [this.idKey]: id }) ?? [];\n const existingPointers = existing.map(item => item[this.pointerKey] as string);\n const stalePointers = existingPointers.filter(p => !newPointerSet.has(p));\n\n const puts = Object.entries(flat).map(([pointer, value]) => {\n const key = { [this.idKey]: id, [this.pointerKey]: pointer } as PointerKey;\n return { key, item: { ...key, [this.valueKey]: value } as PointerItem };\n });\n const deleteKeys = stalePointers.map(\n pointer => ({ [this.idKey]: id, [this.pointerKey]: pointer } as PointerKey),\n );\n await this.repository.batchWriteItems(puts, deleteKeys);\n };\n\n getDocument = async (id: string): Promise<T | undefined> => {\n const items = await this.repository.getItems({ [this.idKey]: id });\n if (!items || items.length === 0) return undefined;\n\n return reconstructDocument<T>(\n items.map(item => ({\n pointer: item[this.pointerKey] as string,\n value: item[this.valueKey] as JsonPointerValue,\n })),\n );\n };\n\n getAttribute = async <V = JsonPointerValue>(\n id: string,\n pointer: string,\n ): Promise<V | undefined> => {\n validatePointer(pointer);\n const item = await this.repository.getItem({\n [this.idKey]: id,\n [this.pointerKey]: pointer,\n } as PointerKey);\n return item ? (item[this.valueKey] as V) : undefined;\n };\n\n putAttribute = async (\n id: string,\n pointer: string,\n value: JsonPointerValue,\n ): Promise<void> => {\n validatePointer(pointer);\n const key = { [this.idKey]: id, [this.pointerKey]: pointer } as PointerKey;\n await this.repository.putItem(key, { ...key, [this.valueKey]: value } as PointerItem);\n };\n\n deleteAttribute = async (id: string, pointer: string): Promise<void> => {\n validatePointer(pointer);\n await this.repository.deleteItem({\n [this.idKey]: id,\n [this.pointerKey]: pointer,\n } as PointerKey);\n };\n\n deleteDocument = async (id: string): Promise<void> => {\n const items = await this.repository.getItems({ [this.idKey]: id }) ?? [];\n if (items.length === 0) return;\n const deleteKeys = items.map(\n item => ({ [this.idKey]: id, [this.pointerKey]: item[this.pointerKey] as string } as PointerKey),\n );\n await this.repository.batchWriteItems([], deleteKeys);\n };\n\n patchDocument = async (\n id: string,\n updates: Record<string, JsonPointerValue | undefined>,\n ): Promise<void> => {\n const entries = Object.entries(updates);\n if (entries.length === 0) return;\n entries.forEach(([pointer]) => validatePointer(pointer));\n const puts = entries\n .filter((entry): entry is [string, JsonPointerValue] => entry[1] !== undefined)\n .map(([pointer, value]) => {\n const key = { [this.idKey]: id, [this.pointerKey]: pointer } as PointerKey;\n return { key, item: { ...key, [this.valueKey]: value } as PointerItem };\n });\n const deleteKeys = entries\n .filter(([, value]) => value === undefined)\n .map(([pointer]) => ({ [this.idKey]: id, [this.pointerKey]: pointer } as PointerKey));\n await this.repository.batchWriteItems(puts, deleteKeys);\n };\n}\n"],"mappings":";;;;;;AAmBA,IAAY,0DAAL;AACH;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AAqCJ,MAAM,eAAe,6CACR,KAA6C,EAAC,uBAAuB,MAAK,CAAC;AAExF,MAAM,0BAA0B,4BAAwB,KAAK,MAAM,IAAI;AAEvE,MAAM,aAAa,qBACf,MAAM,QAAQ,iBAAiB,MAAM,GAC/B,iBAAiB,MAAM,KACpB,GAAG,UAAU,IAAI,uBAAuB,iBAAiB,UAAU,GAAG,QAC1E,GACC,IAAI,uBAAuB,iBAAiB,UAAU;AAEhE,MAAM,uBAAuB,qBAAuC;AAChE,SAAQ,iBAAiB,UAAzB;EACI,KAAK,eAAe,GAChB,QACI,IAAI,uBAAuB,iBAAiB,UAAU,CAAC,GAAG,iBAAiB,SAAS,IAChF,UAAU,iBAAiB,CAAC;EAExC,KAAK,eAAe,QAChB,QACI,IAAI,uBAAuB,iBAAiB,UAAU,CAAC,GAAG,iBAAiB,SAAS,IAChF,uBAAuB,iBAAiB,UAAU,CAAC,SAAS,uBAAuB,iBAAiB,UAAU,CAAC;EAE3H,KAAK,eAAe;EACpB,KAAK,eAAe,SAChB,QACI,GAAG,iBAAiB,SAAS,aAAa,CAAC,IAAI,uBAAuB,iBAAiB,UAAU,CAAC,KAC9F,uBAAuB,iBAAiB,UAAU,CAAC;EAE/D,QACI,QACI,IAAI,uBAAuB,iBAAiB,UAAU,CAAC,GAAG,iBAAiB,SAAS,IAChF,uBAAuB,iBAAiB,UAAU;;;AA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