@eresearchqut/ddb-repository 1.4.0 → 1.5.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.5.0](https://github.com/eresearchqut/ddb-repository/compare/v1.4.1...v1.5.0) (2025-11-21)
2
+
3
+
4
+ ### Features
5
+
6
+ * Initialise the repository using options ([e3a2f46](https://github.com/eresearchqut/ddb-repository/commit/e3a2f46d3a8fb0ca467409369eef505d0451b6b0))
7
+
8
+ ## [1.4.1](https://github.com/eresearchqut/ddb-repository/compare/v1.4.0...v1.4.1) (2025-11-21)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * Fix scope of internal methods ([c652753](https://github.com/eresearchqut/ddb-repository/commit/c652753156662fa08b818e0df068fbb2702881c9))
14
+
1
15
  # [1.4.0](https://github.com/eresearchqut/ddb-repository/compare/v1.3.0...v1.4.0) (2025-11-20)
2
16
 
3
17
 
@@ -27,11 +27,10 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
27
27
  function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
28
28
  };
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.DynamoDbRepository = exports.consumedCapacityMiddleware = exports.mapFilterExpressions = exports.mapFilterExpression = exports.FilterOperator = void 0;
30
+ exports.DynamoDbRepository = exports.FilterOperator = void 0;
31
31
  const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
32
32
  const util_dynamodb_1 = require("@aws-sdk/util-dynamodb");
33
33
  const lodash_1 = require("lodash");
34
- const expressionAttributeKey = (key) => (0, lodash_1.replace)(key, /-/g, "_");
35
34
  var FilterOperator;
36
35
  (function (FilterOperator) {
37
36
  FilterOperator["EQUALS"] = "=";
@@ -43,6 +42,7 @@ var FilterOperator;
43
42
  FilterOperator["IN"] = "IN";
44
43
  FilterOperator["BETWEEN"] = "BETWEEN";
45
44
  })(FilterOperator || (exports.FilterOperator = FilterOperator = {}));
45
+ const expressionAttributeKey = (key) => (0, lodash_1.replace)(key, /-/g, "_");
46
46
  const mapInKeys = (filterExpression) => Array.isArray(filterExpression.value)
47
47
  ? filterExpression.value.map((_, index) => `:${filterExpression.attribute}${index}`)
48
48
  : `:${filterExpression.attribute}`;
@@ -59,13 +59,11 @@ const mapFilterExpression = (filterExpression) => {
59
59
  `:${expressionAttributeKey(filterExpression.attribute)}`);
60
60
  }
61
61
  };
62
- exports.mapFilterExpression = mapFilterExpression;
63
62
  const mapFilterExpressions = (filterExpressions) => filterExpressions
64
63
  .map((filterExpression) => filterExpression.negate
65
- ? `NOT ${(0, exports.mapFilterExpression)(filterExpression)}`
66
- : (0, exports.mapFilterExpression)(filterExpression))
64
+ ? `NOT ${mapFilterExpression(filterExpression)}`
65
+ : mapFilterExpression(filterExpression))
67
66
  .join(" AND ");
68
- exports.mapFilterExpressions = mapFilterExpressions;
69
67
  const mapFilterExpressionValues = (filterExpression) => Array.isArray(filterExpression.value)
70
68
  ? filterExpression.value.reduce((reduction, value, index) => (Object.assign(Object.assign({}, reduction), { [`:${expressionAttributeKey(filterExpression.attribute)}${index}`]: value })), Object.assign({}))
71
69
  : {
@@ -79,28 +77,9 @@ const paginate = (array, pageSize) => {
79
77
  return acc;
80
78
  }, []);
81
79
  };
82
- const consumedCapacityMiddleware = (consumedCapacityMiddlewareConfig) => (next, context) => (args) => __awaiter(void 0, void 0, void 0, function* () {
83
- try {
84
- const { input } = args;
85
- const { ReturnConsumedCapacity } = input;
86
- const response = yield next(args);
87
- const { output } = response;
88
- const consumedCapacity = (0, lodash_1.get)(output, "ConsumedCapacity");
89
- yield consumedCapacityMiddlewareConfig.onConsumedCapacity({ ReturnConsumedCapacity, ConsumedCapacity: consumedCapacity });
90
- return response;
91
- }
92
- catch (error) {
93
- throw error;
94
- }
95
- });
96
- exports.consumedCapacityMiddleware = consumedCapacityMiddleware;
97
80
  class DynamoDbRepository {
98
- constructor(dynamoDBClient, tableName, hashKey, rangKey, returnConsumedCapacity = client_dynamodb_1.ReturnConsumedCapacity.TOTAL) {
99
- this.dynamoDBClient = dynamoDBClient;
100
- this.tableName = tableName;
101
- this.hashKey = hashKey;
102
- this.rangKey = rangKey;
103
- this.returnConsumedCapacity = returnConsumedCapacity;
81
+ constructor(options) {
82
+ var _a;
104
83
  this.getItem = (key) => __awaiter(this, void 0, void 0, function* () {
105
84
  return this.dynamoDBClient
106
85
  .send(new client_dynamodb_1.GetItemCommand({
@@ -169,7 +148,7 @@ class DynamoDbRepository {
169
148
  const projectionAttributeNames = !index && projectedAttributes ? projectedAttributes.reduce((reduction, attribute) => (Object.assign(Object.assign({}, reduction), { [`#${expressionAttributeKey(attribute)}`]: attribute })), Object.assign({})) : {};
170
149
  const hasFilterExpressions = Array.isArray(filterExpressions) && filterExpressions.length > 0;
171
150
  const FilterExpression = hasFilterExpressions
172
- ? (0, exports.mapFilterExpressions)(filterExpressions)
151
+ ? mapFilterExpressions(filterExpressions)
173
152
  : undefined;
174
153
  const filterAttributeNames = hasFilterExpressions
175
154
  ? filterExpressions.reduce((reduction, filterExpression) => (Object.assign(Object.assign({}, reduction), { [`#${expressionAttributeKey(filterExpression.attribute)}`]: filterExpression.attribute })), Object.assign({}))
@@ -255,6 +234,11 @@ class DynamoDbRepository {
255
234
  }))))
256
235
  .then((itemSets) => itemSets.flat());
257
236
  });
237
+ this.dynamoDBClient = options.client;
238
+ this.tableName = options.tableName;
239
+ this.hashKey = options.hashKey;
240
+ this.rangKey = options.rangeKey;
241
+ this.returnConsumedCapacity = (_a = options.returnConsumedCapacity) !== null && _a !== void 0 ? _a : client_dynamodb_1.ReturnConsumedCapacity.TOTAL;
258
242
  }
259
243
  }
260
244
  exports.DynamoDbRepository = DynamoDbRepository;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.consumedCapacityMiddleware = void 0;
13
+ const lodash_1 = require("lodash");
14
+ /* eslint-disable @typescript-eslint/no-explicit-any */
15
+ /* eslint-disable @typescript-eslint/no-unused-vars */
16
+ const consumedCapacityMiddleware = (consumedCapacityMiddlewareConfig) => (next, context) => (args) => __awaiter(void 0, void 0, void 0, function* () {
17
+ const { input } = args;
18
+ const { ReturnConsumedCapacity } = input;
19
+ const response = yield next(args);
20
+ const { output } = response;
21
+ const consumedCapacity = (0, lodash_1.get)(output, "ConsumedCapacity");
22
+ yield consumedCapacityMiddlewareConfig.onConsumedCapacity({ ReturnConsumedCapacity, ConsumedCapacity: consumedCapacity });
23
+ return response;
24
+ });
25
+ exports.consumedCapacityMiddleware = consumedCapacityMiddleware;
package/dist/index.js CHANGED
@@ -1,17 +1,8 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
2
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./DynamoDbRepository"), exports);
3
+ exports.consumedCapacityMiddleware = exports.DynamoDbRepository = exports.FilterOperator = void 0;
4
+ var DynamoDbRepository_1 = require("./DynamoDbRepository");
5
+ Object.defineProperty(exports, "FilterOperator", { enumerable: true, get: function () { return DynamoDbRepository_1.FilterOperator; } });
6
+ Object.defineProperty(exports, "DynamoDbRepository", { enumerable: true, get: function () { return DynamoDbRepository_1.DynamoDbRepository; } });
7
+ var consumed_capacity_middleware_1 = require("./consumed-capacity-middleware");
8
+ Object.defineProperty(exports, "consumedCapacityMiddleware", { enumerable: true, get: function () { return consumed_capacity_middleware_1.consumedCapacityMiddleware; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eresearchqut/ddb-repository",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -1,7 +1,6 @@
1
1
  import {
2
2
  BatchGetItemCommand,
3
3
  BatchGetItemCommandInput,
4
- ConsumedCapacity,
5
4
  DeleteItemCommand,
6
5
  DynamoDBClient,
7
6
  GetItemCommand,
@@ -12,16 +11,7 @@ import {
12
11
  UpdateItemCommand,
13
12
  } from "@aws-sdk/client-dynamodb";
14
13
  import {marshall, unmarshall} from "@aws-sdk/util-dynamodb";
15
- import {replace, uniqWith, isEqual, pickBy, get} from "lodash";
16
- import {
17
- HandlerExecutionContext,
18
- InitializeHandler,
19
- InitializeHandlerArguments,
20
- InitializeHandlerOutput,
21
- MetadataBearer
22
- } from "@smithy/types";
23
-
24
- const expressionAttributeKey = (key: string) => replace(key, /-/g, "_");
14
+ import {replace, uniqWith, isEqual, pickBy} from "lodash";
25
15
 
26
16
  export enum FilterOperator {
27
17
  EQUALS = "=",
@@ -63,6 +53,7 @@ export interface Query extends Partial<FilterableQuery>, Partial<ProjectedQuery>
63
53
  [key: string]: unknown;
64
54
  }
65
55
 
56
+ const expressionAttributeKey = (key: string) => replace(key, /-/g, "_");
66
57
 
67
58
  const mapInKeys = (filterExpression: FilterExpression) =>
68
59
  Array.isArray(filterExpression.value)
@@ -71,7 +62,7 @@ const mapInKeys = (filterExpression: FilterExpression) =>
71
62
  )
72
63
  : `:${filterExpression.attribute}`;
73
64
 
74
- export const mapFilterExpression = (filterExpression: FilterExpression) => {
65
+ const mapFilterExpression = (filterExpression: FilterExpression) => {
75
66
  switch (filterExpression.operator) {
76
67
  case FilterOperator.IN:
77
68
  return (
@@ -91,7 +82,7 @@ export const mapFilterExpression = (filterExpression: FilterExpression) => {
91
82
  }
92
83
  };
93
84
 
94
- export const mapFilterExpressions = (
85
+ const mapFilterExpressions = (
95
86
  filterExpressions: Array<FilterExpression>,
96
87
  ) =>
97
88
  filterExpressions
@@ -127,45 +118,27 @@ const paginate = <T>(array: Array<T>, pageSize: number) => {
127
118
  }, [] as Array<Array<T>>);
128
119
  }
129
120
 
130
- export interface ConsumedCapacityDetail {
131
- ReturnConsumedCapacity: ReturnConsumedCapacity | undefined
132
- ConsumedCapacity: ConsumedCapacity | ConsumedCapacity[] | undefined
133
- }
134
-
135
- export interface ConsumedCapacityMiddlewareConfig {
136
- onConsumedCapacity: (consumedCapacity: ConsumedCapacityDetail) => Promise<unknown>;
121
+ export interface DynamoDbRepositoryOptions {
122
+ client: DynamoDBClient;
123
+ tableName: string;
124
+ hashKey: string;
125
+ rangeKey?: string;
126
+ returnConsumedCapacity?: ReturnConsumedCapacity;
137
127
  }
138
128
 
139
- export const consumedCapacityMiddleware =
140
- (consumedCapacityMiddlewareConfig: ConsumedCapacityMiddlewareConfig) =>
141
- <Output extends MetadataBearer = MetadataBearer>(
142
- next: InitializeHandler<any, Output>,
143
- context: HandlerExecutionContext
144
- ): InitializeHandler<any, Output> =>
145
- async (args: InitializeHandlerArguments<any>): Promise<InitializeHandlerOutput<Output>> => {
146
- try {
147
- const {input} = args;
148
- const {ReturnConsumedCapacity} = input;
149
- const response = await next(args);
150
- const {output} = response;
151
- const consumedCapacity = get(output, "ConsumedCapacity") as ConsumedCapacity | undefined;
152
- await consumedCapacityMiddlewareConfig.onConsumedCapacity({ReturnConsumedCapacity, ConsumedCapacity: consumedCapacity});
153
- return response;
154
- } catch (error) {
155
- throw error;
156
- }
157
- };
158
-
159
129
  export class DynamoDbRepository<K, T> {
160
-
161
- constructor(
162
- private readonly dynamoDBClient: DynamoDBClient,
163
- private readonly tableName: string,
164
- private readonly hashKey: string,
165
- private readonly rangKey?: string,
166
- private readonly returnConsumedCapacity: ReturnConsumedCapacity | undefined = ReturnConsumedCapacity.TOTAL,
167
- ) {
168
-
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;
169
142
  }
170
143
 
171
144
  getItem = async (key: K): Promise<T | undefined> => {
@@ -402,6 +375,5 @@ export class DynamoDbRepository<K, T> {
402
375
  .then((itemSets) => itemSets.flat());
403
376
 
404
377
  };
405
-
406
378
  }
407
379
 
@@ -0,0 +1,41 @@
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 CHANGED
@@ -1,3 +1,4 @@
1
- export * from "./DynamoDbRepository";
1
+ export {Query, IndexedQuery, ProjectedQuery, FilterableQuery, FilterExpression, FilterOperator, DynamoDbRepository} from "./DynamoDbRepository";
2
+ export {consumedCapacityMiddleware, ConsumedCapacityDetail, ConsumedCapacityMiddlewareConfig} from "./consumed-capacity-middleware"
2
3
 
3
4
 
@@ -120,9 +120,23 @@ describe('DynamoDbRepository Integration Tests', () => {
120
120
  }
121
121
 
122
122
  // Initialize repositories
123
- repository = new DynamoDbRepository(dynamoDBClient, tableName, "id");
124
- compositeRepository = new DynamoDbRepository(dynamoDBClient, compositeTableName, "userId", "itemId");
125
- gsiRepository = new DynamoDbRepository(dynamoDBClient, gsiTableName, "userId", "itemId");
123
+ repository = new DynamoDbRepository({
124
+ client: dynamoDBClient,
125
+ tableName: tableName,
126
+ hashKey: "id"
127
+ });
128
+ compositeRepository = new DynamoDbRepository({
129
+ client: dynamoDBClient,
130
+ tableName: compositeTableName,
131
+ hashKey: "userId",
132
+ rangeKey: "itemId"
133
+ });
134
+ gsiRepository = new DynamoDbRepository({
135
+ client: dynamoDBClient,
136
+ tableName: gsiTableName,
137
+ hashKey: "userId",
138
+ rangeKey: "itemId"
139
+ });
126
140
  });
127
141
 
128
142
  beforeEach(async () => {