@eresearchqut/ddb-repository 1.5.6 → 1.5.8
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/DynamoDbRepository.d.ts +50 -0
- package/dist/consumed-capacity-middleware.d.ts +10 -0
- package/dist/index.d.ts +2 -0
- package/package.json +6 -3
- package/.github/workflows/build.yml +0 -61
- package/.github/workflows/release.yml +0 -34
- package/.releaserc.json +0 -17
- package/CHANGELOG.md +0 -85
- package/eslint.config.mjs +0 -13
- package/jest.config.ts +0 -28
- package/src/DynamoDbRepository.ts +0 -379
- package/src/consumed-capacity-middleware.ts +0 -41
- package/src/index.ts +0 -4
- package/test/DynamoDbRepository.test.ts +0 -1216
- package/tsconfig.json +0 -12
|
@@ -1,379 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BatchGetItemCommand,
|
|
3
|
-
BatchGetItemCommandInput,
|
|
4
|
-
DeleteItemCommand,
|
|
5
|
-
DynamoDBClient,
|
|
6
|
-
GetItemCommand,
|
|
7
|
-
paginateQuery,
|
|
8
|
-
PutItemCommand,
|
|
9
|
-
QueryCommandInput,
|
|
10
|
-
ReturnConsumedCapacity,
|
|
11
|
-
UpdateItemCommand,
|
|
12
|
-
} from "@aws-sdk/client-dynamodb";
|
|
13
|
-
import {marshall, unmarshall} from "@aws-sdk/util-dynamodb";
|
|
14
|
-
import {replace, uniqWith, isEqual, pickBy} from "lodash";
|
|
15
|
-
|
|
16
|
-
export enum FilterOperator {
|
|
17
|
-
EQUALS = "=",
|
|
18
|
-
NOT_EQUALS = "<>",
|
|
19
|
-
GREATER_THAN_OR_EQUALS = ">=",
|
|
20
|
-
GREATER_THAN = ">",
|
|
21
|
-
LESS_THAN = "<",
|
|
22
|
-
LESS_THAN_OR_EQUALS = "<=",
|
|
23
|
-
IN = "IN",
|
|
24
|
-
BETWEEN = "BETWEEN",
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface FilterExpression {
|
|
28
|
-
attribute: string;
|
|
29
|
-
value:
|
|
30
|
-
| string
|
|
31
|
-
| number
|
|
32
|
-
| boolean
|
|
33
|
-
| Array<string | number>
|
|
34
|
-
| [string, string]
|
|
35
|
-
| [number, number];
|
|
36
|
-
operator: FilterOperator;
|
|
37
|
-
negate?: boolean;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface FilterableQuery {
|
|
41
|
-
filterExpressions: Array<FilterExpression>;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export interface ProjectedQuery {
|
|
45
|
-
projectedAttributes: string[];
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export interface IndexedQuery {
|
|
49
|
-
index: string;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface Query extends Partial<FilterableQuery>, Partial<ProjectedQuery>, Partial<IndexedQuery> {
|
|
53
|
-
[key: string]: unknown;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const expressionAttributeKey = (key: string) => replace(key, /-/g, "_");
|
|
57
|
-
|
|
58
|
-
const mapInKeys = (filterExpression: FilterExpression) =>
|
|
59
|
-
Array.isArray(filterExpression.value)
|
|
60
|
-
? filterExpression.value.map(
|
|
61
|
-
(_, index) => `:${filterExpression.attribute}${index}`,
|
|
62
|
-
)
|
|
63
|
-
: `:${filterExpression.attribute}`;
|
|
64
|
-
|
|
65
|
-
const mapFilterExpression = (filterExpression: FilterExpression) => {
|
|
66
|
-
switch (filterExpression.operator) {
|
|
67
|
-
case FilterOperator.IN:
|
|
68
|
-
return (
|
|
69
|
-
`#${expressionAttributeKey(filterExpression.attribute)} ${filterExpression.operator} ` +
|
|
70
|
-
`(${mapInKeys(filterExpression)})`
|
|
71
|
-
);
|
|
72
|
-
case FilterOperator.BETWEEN:
|
|
73
|
-
return (
|
|
74
|
-
`#${expressionAttributeKey(filterExpression.attribute)} ${filterExpression.operator} ` +
|
|
75
|
-
`:${expressionAttributeKey(filterExpression.attribute)}0 AND :${expressionAttributeKey(filterExpression.attribute)}1`
|
|
76
|
-
);
|
|
77
|
-
default:
|
|
78
|
-
return (
|
|
79
|
-
`#${expressionAttributeKey(filterExpression.attribute)} ${filterExpression.operator} ` +
|
|
80
|
-
`:${expressionAttributeKey(filterExpression.attribute)}`
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
const mapFilterExpressions = (
|
|
86
|
-
filterExpressions: Array<FilterExpression>,
|
|
87
|
-
) =>
|
|
88
|
-
filterExpressions
|
|
89
|
-
.map((filterExpression) =>
|
|
90
|
-
filterExpression.negate
|
|
91
|
-
? `NOT ${mapFilterExpression(filterExpression)}`
|
|
92
|
-
: mapFilterExpression(filterExpression),
|
|
93
|
-
)
|
|
94
|
-
.join(" AND ");
|
|
95
|
-
|
|
96
|
-
const mapFilterExpressionValues = (
|
|
97
|
-
filterExpression: FilterExpression,
|
|
98
|
-
): Record<string, string | number | boolean> =>
|
|
99
|
-
Array.isArray(filterExpression.value)
|
|
100
|
-
? filterExpression.value.reduce(
|
|
101
|
-
(reduction, value, index) => ({
|
|
102
|
-
...reduction,
|
|
103
|
-
[`:${expressionAttributeKey(filterExpression.attribute)}${index}`]: value,
|
|
104
|
-
}),
|
|
105
|
-
Object.assign({}),
|
|
106
|
-
)
|
|
107
|
-
: {
|
|
108
|
-
[`:${expressionAttributeKey(filterExpression.attribute)}`]:
|
|
109
|
-
filterExpression.value,
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
const paginate = <T>(array: Array<T>, pageSize: number) => {
|
|
113
|
-
return array.reduce((acc, val, i) => {
|
|
114
|
-
const idx = Math.floor(i / pageSize)
|
|
115
|
-
const page = acc[idx] || (acc[idx] = [])
|
|
116
|
-
page.push(val)
|
|
117
|
-
return acc
|
|
118
|
-
}, [] as Array<Array<T>>);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export interface DynamoDbRepositoryOptions {
|
|
122
|
-
client: DynamoDBClient;
|
|
123
|
-
tableName: string;
|
|
124
|
-
hashKey: string;
|
|
125
|
-
rangeKey?: string;
|
|
126
|
-
returnConsumedCapacity?: ReturnConsumedCapacity;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export class DynamoDbRepository<K, T> {
|
|
130
|
-
private readonly dynamoDBClient: DynamoDBClient;
|
|
131
|
-
private readonly tableName: string;
|
|
132
|
-
private readonly hashKey: string;
|
|
133
|
-
private readonly rangKey?: string;
|
|
134
|
-
private readonly returnConsumedCapacity: ReturnConsumedCapacity | undefined;
|
|
135
|
-
|
|
136
|
-
constructor(options: DynamoDbRepositoryOptions) {
|
|
137
|
-
this.dynamoDBClient = options.client;
|
|
138
|
-
this.tableName = options.tableName;
|
|
139
|
-
this.hashKey = options.hashKey;
|
|
140
|
-
this.rangKey = options.rangeKey;
|
|
141
|
-
this.returnConsumedCapacity = options.returnConsumedCapacity ?? ReturnConsumedCapacity.TOTAL;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
getItem = async (key: K): Promise<T | undefined> => {
|
|
145
|
-
return this.dynamoDBClient
|
|
146
|
-
.send(
|
|
147
|
-
new GetItemCommand({
|
|
148
|
-
TableName: this.tableName,
|
|
149
|
-
Key: marshall(key, {removeUndefinedValues: true}),
|
|
150
|
-
ReturnConsumedCapacity: this.returnConsumedCapacity,
|
|
151
|
-
}),
|
|
152
|
-
)
|
|
153
|
-
.then((result) =>
|
|
154
|
-
result.Item ? unmarshall(result.Item) as T : undefined,
|
|
155
|
-
)
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
putItem = async (key: K, record: T): Promise<T> => {
|
|
159
|
-
const Item = marshall({...record, ...key}, {removeUndefinedValues: true});
|
|
160
|
-
return this.dynamoDBClient
|
|
161
|
-
.send(
|
|
162
|
-
new PutItemCommand({
|
|
163
|
-
TableName: this.tableName,
|
|
164
|
-
ReturnConsumedCapacity: this.returnConsumedCapacity,
|
|
165
|
-
Item,
|
|
166
|
-
}),
|
|
167
|
-
)
|
|
168
|
-
.then(() => this.getItem(key) as Promise<T>);
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
deleteItem = async (key: K) => {
|
|
172
|
-
return this.dynamoDBClient.send(new DeleteItemCommand({
|
|
173
|
-
TableName: this.tableName,
|
|
174
|
-
Key: marshall(key),
|
|
175
|
-
})).then((result) => result.Attributes ?
|
|
176
|
-
unmarshall(result.Attributes) : undefined);
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
updateItem = async (
|
|
181
|
-
key: K,
|
|
182
|
-
updates: Partial<T>,
|
|
183
|
-
remove?: string[],
|
|
184
|
-
): Promise<T | undefined> => {
|
|
185
|
-
const hasUpdates = Object.keys(updates).length > 0;
|
|
186
|
-
const setAttributesExpression = hasUpdates ? `SET ${Object.entries(updates)
|
|
187
|
-
.filter(([, value]) => value !== undefined)
|
|
188
|
-
.map(
|
|
189
|
-
([key]) =>
|
|
190
|
-
`#${expressionAttributeKey(key)} = :${expressionAttributeKey(key)}`,
|
|
191
|
-
)
|
|
192
|
-
.join(", ")}` : '';
|
|
193
|
-
const removeAttributesExpression = remove
|
|
194
|
-
? ` REMOVE ${remove.map((key) => `#${expressionAttributeKey(key)}`).join(", ")}`
|
|
195
|
-
: "";
|
|
196
|
-
const removeAttributeNames = remove
|
|
197
|
-
? remove.map(expressionAttributeKey).reduce(
|
|
198
|
-
(acc, key) => ({
|
|
199
|
-
...acc,
|
|
200
|
-
[`#${expressionAttributeKey(key)}`]: key,
|
|
201
|
-
}),
|
|
202
|
-
{},
|
|
203
|
-
)
|
|
204
|
-
: {};
|
|
205
|
-
const updateItemCommandInput = {
|
|
206
|
-
TableName: this.tableName,
|
|
207
|
-
Key: marshall(key),
|
|
208
|
-
UpdateExpression: `${setAttributesExpression}${removeAttributesExpression}`,
|
|
209
|
-
ExpressionAttributeNames: Object.entries(updates)
|
|
210
|
-
.filter(([, value]) => value !== undefined)
|
|
211
|
-
.reduce(
|
|
212
|
-
(acc, [key]) => ({
|
|
213
|
-
...acc,
|
|
214
|
-
[`#${expressionAttributeKey(key)}`]: key,
|
|
215
|
-
}),
|
|
216
|
-
Object.assign(
|
|
217
|
-
removeAttributeNames,
|
|
218
|
-
),
|
|
219
|
-
) as Record<string, string>,
|
|
220
|
-
ExpressionAttributeValues: hasUpdates ? marshall(
|
|
221
|
-
Object.entries(updates).reduce(
|
|
222
|
-
(acc, [key, value]) => ({
|
|
223
|
-
...acc,
|
|
224
|
-
[`:${expressionAttributeKey(key)}`]: value,
|
|
225
|
-
}),
|
|
226
|
-
Object.assign(
|
|
227
|
-
{},
|
|
228
|
-
),
|
|
229
|
-
),
|
|
230
|
-
{removeUndefinedValues: true},
|
|
231
|
-
) : undefined,
|
|
232
|
-
ReturnConsumedCapacity: this.returnConsumedCapacity,
|
|
233
|
-
};
|
|
234
|
-
return this.dynamoDBClient
|
|
235
|
-
.send(new UpdateItemCommand(updateItemCommandInput))
|
|
236
|
-
.then(() => this.getItem(key));
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
getItems = async (
|
|
241
|
-
query: Query
|
|
242
|
-
): Promise<Array<T> | undefined> => {
|
|
243
|
-
const {index, filterExpressions, projectedAttributes, ...keys} = query;
|
|
244
|
-
const KeyConditionExpression = Object.keys(keys)
|
|
245
|
-
.map((key) => `#${expressionAttributeKey(key)} = :${expressionAttributeKey(key)}`).join(' AND ');
|
|
246
|
-
const keyExpressionAttributeNames = Object.keys(keys)
|
|
247
|
-
.reduce((acc, key) => ({...acc, [`#${expressionAttributeKey(key)}`]: key}), Object.assign({}));
|
|
248
|
-
const keyExpressionAttributeValues = Object.entries(keys)
|
|
249
|
-
.reduce((acc, [key, value]) => ({...acc, [`:${expressionAttributeKey(key)}`]: value}), Object.assign({}));
|
|
250
|
-
|
|
251
|
-
const ProjectionExpression = !index && projectedAttributes
|
|
252
|
-
? projectedAttributes.map((attribute) => `#${expressionAttributeKey(attribute)}`).join(',')
|
|
253
|
-
: undefined;
|
|
254
|
-
const projectionAttributeNames: Record<string, string> = !index && projectedAttributes ? projectedAttributes.reduce(
|
|
255
|
-
(
|
|
256
|
-
reduction: Record<string, string>,
|
|
257
|
-
attribute: string,
|
|
258
|
-
) => ({
|
|
259
|
-
...reduction,
|
|
260
|
-
[`#${expressionAttributeKey(attribute)}`]:
|
|
261
|
-
attribute,
|
|
262
|
-
}),
|
|
263
|
-
Object.assign({}),
|
|
264
|
-
) : {}
|
|
265
|
-
const hasFilterExpressions = Array.isArray(filterExpressions) && filterExpressions.length > 0;
|
|
266
|
-
const FilterExpression = hasFilterExpressions
|
|
267
|
-
? mapFilterExpressions(filterExpressions)
|
|
268
|
-
: undefined;
|
|
269
|
-
const filterAttributeNames: Record<string, string> = hasFilterExpressions
|
|
270
|
-
? filterExpressions.reduce(
|
|
271
|
-
(
|
|
272
|
-
reduction: Record<string, string>,
|
|
273
|
-
filterExpression: FilterExpression,
|
|
274
|
-
) => ({
|
|
275
|
-
...reduction,
|
|
276
|
-
[`#${expressionAttributeKey(filterExpression.attribute)}`]:
|
|
277
|
-
filterExpression.attribute,
|
|
278
|
-
}),
|
|
279
|
-
Object.assign({}),
|
|
280
|
-
)
|
|
281
|
-
: {};
|
|
282
|
-
const filterAttributeValues = filterExpressions
|
|
283
|
-
? filterExpressions.reduce(
|
|
284
|
-
(reduction, filterExpression) => ({
|
|
285
|
-
...reduction,
|
|
286
|
-
...mapFilterExpressionValues(filterExpression),
|
|
287
|
-
}),
|
|
288
|
-
Object.assign({}),
|
|
289
|
-
)
|
|
290
|
-
: {};
|
|
291
|
-
const queryCommandInput: QueryCommandInput = {
|
|
292
|
-
TableName: this.tableName,
|
|
293
|
-
ReturnConsumedCapacity: this.returnConsumedCapacity,
|
|
294
|
-
IndexName: index,
|
|
295
|
-
KeyConditionExpression,
|
|
296
|
-
FilterExpression,
|
|
297
|
-
ProjectionExpression,
|
|
298
|
-
ExpressionAttributeNames: {
|
|
299
|
-
...keyExpressionAttributeNames,
|
|
300
|
-
...filterAttributeNames,
|
|
301
|
-
...projectionAttributeNames
|
|
302
|
-
},
|
|
303
|
-
ExpressionAttributeValues: marshall(
|
|
304
|
-
{...keyExpressionAttributeValues, ...filterAttributeValues},
|
|
305
|
-
{removeUndefinedValues: true},
|
|
306
|
-
),
|
|
307
|
-
};
|
|
308
|
-
const paginator = paginateQuery(
|
|
309
|
-
{client: this.dynamoDBClient, pageSize: 100},
|
|
310
|
-
queryCommandInput,
|
|
311
|
-
);
|
|
312
|
-
|
|
313
|
-
if (index) {
|
|
314
|
-
const keys: Array<K> = [];
|
|
315
|
-
for await (const page of paginator) {
|
|
316
|
-
if (page.Items) {
|
|
317
|
-
keys.push(
|
|
318
|
-
...(page.Items.map((item) => unmarshall(item) as T)
|
|
319
|
-
.map((item: T) =>
|
|
320
|
-
pickBy(item as object, (_, key) => (key === this.hashKey || key === this.rangKey)) as K)),
|
|
321
|
-
)
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
const items = await this.batchGetItems(keys, query as ProjectedQuery);
|
|
325
|
-
return items as Array<T>;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
const items: Array<T> = [];
|
|
329
|
-
for await (const page of paginator) {
|
|
330
|
-
if (page.Items) {
|
|
331
|
-
items.push(
|
|
332
|
-
...(page.Items?.map((item) => unmarshall(item) as T) || []),
|
|
333
|
-
)
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
return items;
|
|
337
|
-
};
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
batchGetItems = async (
|
|
341
|
-
keys: K[], projectedQuery?: ProjectedQuery
|
|
342
|
-
): Promise<Array<T | undefined>> => {
|
|
343
|
-
const uniqueKeys = uniqWith(keys, isEqual);
|
|
344
|
-
const keyPages = paginate(uniqueKeys, 100);
|
|
345
|
-
const {projectedAttributes} = projectedQuery || {};
|
|
346
|
-
const ProjectionExpression = projectedAttributes
|
|
347
|
-
? projectedAttributes.map((attribute) => `#${expressionAttributeKey(attribute)}`).join(',')
|
|
348
|
-
: undefined;
|
|
349
|
-
const ExpressionAttributeNames = projectedAttributes ?
|
|
350
|
-
projectedAttributes.reduce(
|
|
351
|
-
(
|
|
352
|
-
reduction: Record<string, string>,
|
|
353
|
-
attribute: string,
|
|
354
|
-
) => ({
|
|
355
|
-
...reduction,
|
|
356
|
-
[`#${expressionAttributeKey(attribute)}`]:
|
|
357
|
-
attribute,
|
|
358
|
-
}),
|
|
359
|
-
Object.assign({}),
|
|
360
|
-
) : undefined;
|
|
361
|
-
return Promise.all((keyPages.map(async (keyPage) => {
|
|
362
|
-
const batchRequest: BatchGetItemCommandInput = {
|
|
363
|
-
RequestItems: {
|
|
364
|
-
[this.tableName]: {
|
|
365
|
-
Keys: keyPage.map((key) => (marshall(key))),
|
|
366
|
-
ProjectionExpression,
|
|
367
|
-
ExpressionAttributeNames,
|
|
368
|
-
}
|
|
369
|
-
},
|
|
370
|
-
ReturnConsumedCapacity: this.returnConsumedCapacity,
|
|
371
|
-
}
|
|
372
|
-
return this.dynamoDBClient.send(new BatchGetItemCommand(batchRequest)).then(result =>
|
|
373
|
-
result.Responses?.[this.tableName].map((item) => unmarshall(item) as T));
|
|
374
|
-
})))
|
|
375
|
-
.then((itemSets) => itemSets.flat());
|
|
376
|
-
|
|
377
|
-
};
|
|
378
|
-
}
|
|
379
|
-
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ConsumedCapacity,
|
|
3
|
-
ReturnConsumedCapacity
|
|
4
|
-
} from "@aws-sdk/client-dynamodb";
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
HandlerExecutionContext,
|
|
8
|
-
InitializeHandler,
|
|
9
|
-
InitializeHandlerArguments,
|
|
10
|
-
InitializeHandlerOutput,
|
|
11
|
-
MetadataBearer
|
|
12
|
-
} from "@smithy/types";
|
|
13
|
-
|
|
14
|
-
import {get} from "lodash";
|
|
15
|
-
|
|
16
|
-
export interface ConsumedCapacityDetail {
|
|
17
|
-
ReturnConsumedCapacity: ReturnConsumedCapacity | undefined
|
|
18
|
-
ConsumedCapacity: ConsumedCapacity | ConsumedCapacity[] | undefined
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface ConsumedCapacityMiddlewareConfig {
|
|
22
|
-
onConsumedCapacity: (consumedCapacity: ConsumedCapacityDetail) => Promise<unknown>;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
26
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
27
|
-
export const consumedCapacityMiddleware =
|
|
28
|
-
(consumedCapacityMiddlewareConfig: ConsumedCapacityMiddlewareConfig) =>
|
|
29
|
-
<Output extends MetadataBearer = MetadataBearer>(
|
|
30
|
-
next: InitializeHandler<any, Output>,
|
|
31
|
-
context: HandlerExecutionContext
|
|
32
|
-
): InitializeHandler<any, Output> =>
|
|
33
|
-
async (args: InitializeHandlerArguments<any>): Promise<InitializeHandlerOutput<Output>> => {
|
|
34
|
-
const {input} = args;
|
|
35
|
-
const {ReturnConsumedCapacity} = input;
|
|
36
|
-
const response = await next(args);
|
|
37
|
-
const {output} = response;
|
|
38
|
-
const consumedCapacity = get(output, "ConsumedCapacity") as ConsumedCapacity | ConsumedCapacity[] | undefined;
|
|
39
|
-
await consumedCapacityMiddlewareConfig.onConsumedCapacity({ReturnConsumedCapacity, ConsumedCapacity: consumedCapacity});
|
|
40
|
-
return response;
|
|
41
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export {Query, IndexedQuery, ProjectedQuery, FilterableQuery, FilterExpression, FilterOperator, DynamoDbRepository} from "./DynamoDbRepository";
|
|
2
|
-
export {consumedCapacityMiddleware, ConsumedCapacityDetail, ConsumedCapacityMiddlewareConfig} from "./consumed-capacity-middleware"
|
|
3
|
-
|
|
4
|
-
|