@eresearchqut/ddb-repository 1.0.2
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/.github/workflows/build.yml +61 -0
- package/.github/workflows/release.yml +42 -0
- package/.releaserc.json +17 -0
- package/CHANGELOG.md +23 -0
- package/README.md +202 -0
- package/dist/DynamoDbRepository.js +239 -0
- package/dist/index.js +17 -0
- package/eslint.config.mjs +13 -0
- package/jest.config.ts +28 -0
- package/package.json +45 -0
- package/src/DynamoDbRepository.ts +363 -0
- package/src/index.ts +3 -0
- package/test/repository.test.ts +1123 -0
- package/tsconfig.json +12 -0
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@eresearchqut/ddb-repository",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"test": "jest",
|
|
13
|
+
"test:watch": "jest --watch",
|
|
14
|
+
"test:coverage": "jest --coverage",
|
|
15
|
+
"lint": "eslint . --ext .ts",
|
|
16
|
+
"lint:fix": "eslint . --ext .ts --fix"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@aws-sdk/client-dynamodb": "^3.700.0",
|
|
20
|
+
"@aws-sdk/util-dynamodb": "^3.700.0",
|
|
21
|
+
"@eslint/js": "^9.39.1",
|
|
22
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
23
|
+
"@semantic-release/git": "^10.0.1",
|
|
24
|
+
"@semantic-release/github": "^12.0.2",
|
|
25
|
+
"@semantic-release/npm": "^13.1.2",
|
|
26
|
+
"@testcontainers/localstack": "^10.15.0",
|
|
27
|
+
"@types/jest": "^29.5.0",
|
|
28
|
+
"@types/lodash": "^4.17.20",
|
|
29
|
+
"@types/node": "^24.10.1",
|
|
30
|
+
"eslint": "^9.39.1",
|
|
31
|
+
"eslint-plugin-jest": "^29.1.0",
|
|
32
|
+
"jest": "^29.7.0",
|
|
33
|
+
"semantic-release": "^25.0.2",
|
|
34
|
+
"testcontainers": "^10.15.0",
|
|
35
|
+
"ts-jest": "^29.2.0",
|
|
36
|
+
"ts-node": "^10.9.2",
|
|
37
|
+
"typescript": "^5.5.3",
|
|
38
|
+
"typescript-eslint": "^8.47.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@aws-sdk/client-dynamodb": ">= 3.49.0",
|
|
42
|
+
"@aws-sdk/util-dynamodb": ">= 3.49.0",
|
|
43
|
+
"lodash": "^4.17.20"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BatchGetItemCommand,
|
|
3
|
+
BatchGetItemCommandInput,
|
|
4
|
+
DeleteItemCommand,
|
|
5
|
+
DynamoDBClient,
|
|
6
|
+
GetItemCommand,
|
|
7
|
+
paginateQuery,
|
|
8
|
+
PutItemCommand,
|
|
9
|
+
QueryCommandInput,
|
|
10
|
+
UpdateItemCommand,
|
|
11
|
+
} from "@aws-sdk/client-dynamodb";
|
|
12
|
+
import {marshall, unmarshall} from "@aws-sdk/util-dynamodb";
|
|
13
|
+
import {replace, uniqWith, isEqual, pickBy} from "lodash";
|
|
14
|
+
|
|
15
|
+
const expressionAttributeKey = (key: string) => replace(key, /-/g, "_");
|
|
16
|
+
|
|
17
|
+
export enum FilterOperator {
|
|
18
|
+
EQUALS = "=",
|
|
19
|
+
NOT_EQUALS = "<>",
|
|
20
|
+
GREATER_THAN_OR_EQUALS = ">=",
|
|
21
|
+
GREATER_THAN = ">",
|
|
22
|
+
LESS_THAN = "<",
|
|
23
|
+
LESS_THAN_OR_EQUALS = "<=",
|
|
24
|
+
IN = "IN",
|
|
25
|
+
BETWEEN = "BETWEEN",
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface FilterExpression {
|
|
29
|
+
attribute: string;
|
|
30
|
+
value:
|
|
31
|
+
| string
|
|
32
|
+
| number
|
|
33
|
+
| boolean
|
|
34
|
+
| Array<string | number>
|
|
35
|
+
| [string, string]
|
|
36
|
+
| [number, number];
|
|
37
|
+
operator: FilterOperator;
|
|
38
|
+
negate?: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface FilterableQuery {
|
|
42
|
+
filterExpressions: Array<FilterExpression>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface ProjectedQuery {
|
|
46
|
+
projectedAttributes: string[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface IndexedQuery {
|
|
50
|
+
index: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface Query extends Partial<FilterableQuery>, Partial<ProjectedQuery>, Partial<IndexedQuery> {
|
|
54
|
+
[key: string]: unknown;
|
|
55
|
+
}
|
|
56
|
+
|
|
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
|
+
export 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
|
+
export 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 class DynamoDbRepository<K, T> {
|
|
122
|
+
|
|
123
|
+
constructor(
|
|
124
|
+
private readonly dynamoDBClient: DynamoDBClient,
|
|
125
|
+
private readonly tableName: string,
|
|
126
|
+
private readonly hashKey: string,
|
|
127
|
+
private readonly rangKey?: string,
|
|
128
|
+
) {
|
|
129
|
+
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
getItem = async (key: K): Promise<T | undefined> => {
|
|
133
|
+
return this.dynamoDBClient
|
|
134
|
+
.send(
|
|
135
|
+
new GetItemCommand({
|
|
136
|
+
TableName: this.tableName,
|
|
137
|
+
Key: marshall(key, {removeUndefinedValues: true}),
|
|
138
|
+
}),
|
|
139
|
+
)
|
|
140
|
+
.then((result) =>
|
|
141
|
+
result.Item ? unmarshall(result.Item) as T : undefined,
|
|
142
|
+
)
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
putItem = async (key: K, record: T): Promise<T> => {
|
|
146
|
+
const Item = marshall({...record, ...key}, {removeUndefinedValues: true});
|
|
147
|
+
return this.dynamoDBClient
|
|
148
|
+
.send(
|
|
149
|
+
new PutItemCommand({
|
|
150
|
+
TableName: this.tableName,
|
|
151
|
+
Item,
|
|
152
|
+
}),
|
|
153
|
+
)
|
|
154
|
+
.then(() => this.getItem(key) as Promise<T>);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
deleteItem = async (key: K) => {
|
|
158
|
+
return this.dynamoDBClient.send(new DeleteItemCommand({
|
|
159
|
+
TableName: this.tableName,
|
|
160
|
+
Key: marshall(key),
|
|
161
|
+
})).then((result) => result.Attributes ?
|
|
162
|
+
unmarshall(result.Attributes) : undefined);
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
updateItem = async (
|
|
167
|
+
key: K,
|
|
168
|
+
updates: Partial<T>,
|
|
169
|
+
remove?: string[],
|
|
170
|
+
): Promise<T | undefined> => {
|
|
171
|
+
const hasUpdates = Object.keys(updates).length > 0;
|
|
172
|
+
const setAttributesExpression = hasUpdates ? `SET ${Object.entries(updates)
|
|
173
|
+
.filter(([, value]) => value !== undefined)
|
|
174
|
+
.map(
|
|
175
|
+
([key]) =>
|
|
176
|
+
`#${expressionAttributeKey(key)} = :${expressionAttributeKey(key)}`,
|
|
177
|
+
)
|
|
178
|
+
.join(", ")}` : '';
|
|
179
|
+
const removeAttributesExpression = remove
|
|
180
|
+
? ` REMOVE ${remove.map((key) => `#${expressionAttributeKey(key)}`).join(", ")}`
|
|
181
|
+
: "";
|
|
182
|
+
const removeAttributeNames = remove
|
|
183
|
+
? remove.map(expressionAttributeKey).reduce(
|
|
184
|
+
(acc, key) => ({
|
|
185
|
+
...acc,
|
|
186
|
+
[`#${expressionAttributeKey(key)}`]: key,
|
|
187
|
+
}),
|
|
188
|
+
{},
|
|
189
|
+
)
|
|
190
|
+
: {};
|
|
191
|
+
const updateItemCommandInput = {
|
|
192
|
+
TableName: this.tableName,
|
|
193
|
+
Key: marshall(key),
|
|
194
|
+
UpdateExpression: `${setAttributesExpression}${removeAttributesExpression}`,
|
|
195
|
+
ExpressionAttributeNames: Object.entries(updates)
|
|
196
|
+
.filter(([, value]) => value !== undefined)
|
|
197
|
+
.reduce(
|
|
198
|
+
(acc, [key]) => ({
|
|
199
|
+
...acc,
|
|
200
|
+
[`#${expressionAttributeKey(key)}`]: key,
|
|
201
|
+
}),
|
|
202
|
+
Object.assign(
|
|
203
|
+
removeAttributeNames,
|
|
204
|
+
),
|
|
205
|
+
) as Record<string, string>,
|
|
206
|
+
ExpressionAttributeValues: hasUpdates ? marshall(
|
|
207
|
+
Object.entries(updates).reduce(
|
|
208
|
+
(acc, [key, value]) => ({
|
|
209
|
+
...acc,
|
|
210
|
+
[`:${expressionAttributeKey(key)}`]: value,
|
|
211
|
+
}),
|
|
212
|
+
Object.assign(
|
|
213
|
+
{},
|
|
214
|
+
),
|
|
215
|
+
),
|
|
216
|
+
{removeUndefinedValues: true},
|
|
217
|
+
) : undefined,
|
|
218
|
+
};
|
|
219
|
+
return this.dynamoDBClient
|
|
220
|
+
.send(new UpdateItemCommand(updateItemCommandInput))
|
|
221
|
+
.then(() => this.getItem(key));
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
getItems = async (
|
|
226
|
+
query: Query
|
|
227
|
+
): Promise<Array<T> | undefined> => {
|
|
228
|
+
const {index, filterExpressions, projectedAttributes, ...keys} = query;
|
|
229
|
+
const KeyConditionExpression = Object.keys(keys)
|
|
230
|
+
.map((key) => `#${expressionAttributeKey(key)} = :${expressionAttributeKey(key)}`).join(' AND ');
|
|
231
|
+
const keyExpressionAttributeNames = Object.keys(keys)
|
|
232
|
+
.reduce((acc, key) => ({...acc, [`#${expressionAttributeKey(key)}`]: key}), Object.assign({}));
|
|
233
|
+
const keyExpressionAttributeValues = Object.entries(keys)
|
|
234
|
+
.reduce((acc, [key, value]) => ({...acc, [`:${expressionAttributeKey(key)}`]: value}), Object.assign({}));
|
|
235
|
+
|
|
236
|
+
const ProjectionExpression = !index && projectedAttributes
|
|
237
|
+
? projectedAttributes.map((attribute) => `#${expressionAttributeKey(attribute)}`).join(',')
|
|
238
|
+
: undefined;
|
|
239
|
+
const projectionAttributeNames: Record<string, string> = !index && projectedAttributes ? projectedAttributes.reduce(
|
|
240
|
+
(
|
|
241
|
+
reduction: Record<string, string>,
|
|
242
|
+
attribute: string,
|
|
243
|
+
) => ({
|
|
244
|
+
...reduction,
|
|
245
|
+
[`#${expressionAttributeKey(attribute)}`]:
|
|
246
|
+
attribute,
|
|
247
|
+
}),
|
|
248
|
+
Object.assign({}),
|
|
249
|
+
) : {}
|
|
250
|
+
const hasFilterExpressions = Array.isArray(filterExpressions) && filterExpressions.length > 0;
|
|
251
|
+
const FilterExpression = hasFilterExpressions
|
|
252
|
+
? mapFilterExpressions(filterExpressions)
|
|
253
|
+
: undefined;
|
|
254
|
+
const filterAttributeNames: Record<string, string> = hasFilterExpressions
|
|
255
|
+
? filterExpressions.reduce(
|
|
256
|
+
(
|
|
257
|
+
reduction: Record<string, string>,
|
|
258
|
+
filterExpression: FilterExpression,
|
|
259
|
+
) => ({
|
|
260
|
+
...reduction,
|
|
261
|
+
[`#${expressionAttributeKey(filterExpression.attribute)}`]:
|
|
262
|
+
filterExpression.attribute,
|
|
263
|
+
}),
|
|
264
|
+
Object.assign({}),
|
|
265
|
+
)
|
|
266
|
+
: {};
|
|
267
|
+
const filterAttributeValues = filterExpressions
|
|
268
|
+
? filterExpressions.reduce(
|
|
269
|
+
(reduction, filterExpression) => ({
|
|
270
|
+
...reduction,
|
|
271
|
+
...mapFilterExpressionValues(filterExpression),
|
|
272
|
+
}),
|
|
273
|
+
Object.assign({}),
|
|
274
|
+
)
|
|
275
|
+
: {};
|
|
276
|
+
const queryCommandInput: QueryCommandInput = {
|
|
277
|
+
TableName: this.tableName,
|
|
278
|
+
IndexName: index,
|
|
279
|
+
KeyConditionExpression,
|
|
280
|
+
FilterExpression,
|
|
281
|
+
ProjectionExpression,
|
|
282
|
+
ExpressionAttributeNames: {
|
|
283
|
+
...keyExpressionAttributeNames,
|
|
284
|
+
...filterAttributeNames,
|
|
285
|
+
...projectionAttributeNames
|
|
286
|
+
},
|
|
287
|
+
ExpressionAttributeValues: marshall(
|
|
288
|
+
{...keyExpressionAttributeValues, ...filterAttributeValues},
|
|
289
|
+
{removeUndefinedValues: true},
|
|
290
|
+
),
|
|
291
|
+
};
|
|
292
|
+
const paginator = paginateQuery(
|
|
293
|
+
{client: this.dynamoDBClient, pageSize: 100},
|
|
294
|
+
queryCommandInput,
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
if (index) {
|
|
298
|
+
const keys: Array<K> = [];
|
|
299
|
+
for await (const page of paginator) {
|
|
300
|
+
if (page.Items) {
|
|
301
|
+
keys.push(
|
|
302
|
+
...(page.Items.map((item) => unmarshall(item) as T)
|
|
303
|
+
.map((item: T) =>
|
|
304
|
+
pickBy(item as object, (_, key) => (key === this.hashKey || key === this.rangKey)) as K)),
|
|
305
|
+
)
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
const items = await this.batchGetItems(keys, query as ProjectedQuery);
|
|
309
|
+
return items as Array<T>;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const items: Array<T> = [];
|
|
313
|
+
for await (const page of paginator) {
|
|
314
|
+
if (page.Items) {
|
|
315
|
+
items.push(
|
|
316
|
+
...(page.Items?.map((item) => unmarshall(item) as T) || []),
|
|
317
|
+
)
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return items;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
batchGetItems = async (
|
|
325
|
+
keys: K[], projectedQuery?: ProjectedQuery
|
|
326
|
+
): Promise<Array<T | undefined>> => {
|
|
327
|
+
const uniqueKeys = uniqWith(keys, isEqual);
|
|
328
|
+
const keyPages = paginate(uniqueKeys, 100);
|
|
329
|
+
const {projectedAttributes} = projectedQuery || {};
|
|
330
|
+
const ProjectionExpression = projectedAttributes
|
|
331
|
+
? projectedAttributes.map((attribute) => `#${expressionAttributeKey(attribute)}`).join(',')
|
|
332
|
+
: undefined;
|
|
333
|
+
const ExpressionAttributeNames = projectedAttributes ?
|
|
334
|
+
projectedAttributes.reduce(
|
|
335
|
+
(
|
|
336
|
+
reduction: Record<string, string>,
|
|
337
|
+
attribute: string,
|
|
338
|
+
) => ({
|
|
339
|
+
...reduction,
|
|
340
|
+
[`#${expressionAttributeKey(attribute)}`]:
|
|
341
|
+
attribute,
|
|
342
|
+
}),
|
|
343
|
+
Object.assign({}),
|
|
344
|
+
) : undefined;
|
|
345
|
+
return Promise.all((keyPages.map(async (keyPage) => {
|
|
346
|
+
const batchRequest: BatchGetItemCommandInput = {
|
|
347
|
+
RequestItems: {
|
|
348
|
+
[this.tableName]: {
|
|
349
|
+
Keys: keyPage.map((key) => (marshall(key))),
|
|
350
|
+
ProjectionExpression,
|
|
351
|
+
ExpressionAttributeNames
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return this.dynamoDBClient.send(new BatchGetItemCommand(batchRequest)).then(result =>
|
|
356
|
+
result.Responses?.[this.tableName].map((item) => unmarshall(item) as T));
|
|
357
|
+
})))
|
|
358
|
+
.then((itemSets) => itemSets.flat());
|
|
359
|
+
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
}
|
|
363
|
+
|
package/src/index.ts
ADDED