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