@moicky/dynamodb 2.5.10 → 2.6.1

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.
@@ -2,9 +2,18 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getClient = exports.initClient = void 0;
4
4
  const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
5
+ const credential_providers_1 = require("@aws-sdk/credential-providers");
5
6
  const schemas_1 = require("./schemas");
6
7
  let client = new client_dynamodb_1.DynamoDBClient({
7
8
  region: process.env.AWS_REGION || "eu-central-1",
9
+ ...(process.env.DYNAMODB_ASSUME_ROLE && {
10
+ credentials: (0, credential_providers_1.fromTemporaryCredentials)({
11
+ params: {
12
+ RoleArn: process.env.DYNAMODB_ASSUME_ROLE,
13
+ RoleSessionName: "moicky-dynamodb",
14
+ },
15
+ }),
16
+ }),
8
17
  });
9
18
  const defaultTable = process.env.DYNAMODB_TABLE;
10
19
  if (defaultTable) {
@@ -143,11 +143,17 @@ exports.getItems = getItems;
143
143
  */
144
144
  async function getAllItems(args = {}) {
145
145
  args = (0, lib_1.withFixes)((0, lib_1.withDefaults)(args, "getAllItems"));
146
- return (0, lib_1.getClient)()
147
- .send(new client_dynamodb_1.ScanCommand({
148
- ...args,
149
- TableName: args?.TableName || (0, lib_1.getDefaultTable)(),
150
- }))
151
- .then((res) => res.Items.map((item) => (0, lib_1.unmarshallWithOptions)(item)));
146
+ let items = [];
147
+ let lastEvaluatedKey;
148
+ do {
149
+ const response = await (0, lib_1.getClient)().send(new client_dynamodb_1.ScanCommand({
150
+ ...args,
151
+ TableName: args?.TableName || (0, lib_1.getDefaultTable)(),
152
+ ExclusiveStartKey: lastEvaluatedKey,
153
+ }));
154
+ items = items.concat(response.Items.map((item) => (0, lib_1.unmarshallWithOptions)(item)));
155
+ lastEvaluatedKey = response.LastEvaluatedKey;
156
+ } while (lastEvaluatedKey);
157
+ return items;
152
158
  }
153
159
  exports.getAllItems = getAllItems;
@@ -25,6 +25,7 @@ async function _query(keyCondition, key, args = {}) {
25
25
  attributesToGet: [
26
26
  ...(0, lib_1.getAttributesFromExpression)(keyCondition),
27
27
  ...(0, lib_1.getAttributesFromExpression)(args?.FilterExpression || ""),
28
+ ...(0, lib_1.getAttributesFromExpression)(args?.ProjectionExpression || ""),
28
29
  ],
29
30
  }),
30
31
  ...args,
@@ -124,6 +125,9 @@ async function queryAllItems(keyCondition, key, args = {}) {
124
125
  break;
125
126
  }
126
127
  }
128
+ if (args.Limit && data.Items.length > args.Limit) {
129
+ data.Items = data.Items.slice(0, args.Limit);
130
+ }
127
131
  return (data?.Items || [])
128
132
  .map((item) => item && (0, lib_1.unmarshallWithOptions)(item))
129
133
  .filter(Boolean);
@@ -29,5 +29,49 @@ type TransactWriteItemsInput = Partial<Omit<TransactWriteItemsCommandInput, "Tra
29
29
  type ResponseItem = Pick<ItemCollectionMetrics, "SizeEstimateRangeGB"> & {
30
30
  Key: Record<string, any>;
31
31
  };
32
+ /**
33
+ * Performs a TransactWriteItems operation against DynamoDB. This allows you to perform multiple write operations in a single transaction.
34
+ * @param transactItems - Array of items to transact. Each item can be a Put, Update, Delete, or ConditionCheck operation.
35
+ * @param args - The additional arguments to override or specify for {@link TransactWriteItemsCommandInput}
36
+ * @returns A promise that resolves to a record of response items
37
+ *
38
+ * @example
39
+ * Perform a TransactWriteItems operation
40
+ * ```javascript
41
+ * const response = await transactWriteItems([
42
+ * {
43
+ * Put: {
44
+ * item: {
45
+ * PK: "User/1",
46
+ * SK: "Book/1",
47
+ * title: "The Great Gatsby",
48
+ * author: "F. Scott Fitzgerald",
49
+ * released: 1925,
50
+ * },
51
+ * },
52
+ * },
53
+ * {
54
+ * Update: {
55
+ * key: { PK: "User/1", SK: "Book/1" },
56
+ * updateData: { title: "The Great Gatsby - Updated" },
57
+ * },
58
+ * },
59
+ * {
60
+ * Delete: {
61
+ * key: { PK: "User/1", SK: "Book/1" },
62
+ * },
63
+ * },
64
+ * {
65
+ * ConditionCheck: {
66
+ * key: { PK: "User/1", SK: "Book/1" },
67
+ * ConditionExpression: "#title = :title",
68
+ * conditionData: { title: "The Great Gatsby" },
69
+ * },
70
+ * },
71
+ * ]);
72
+ *
73
+ * console.log(response);
74
+ * ```
75
+ */
32
76
  export declare function transactWriteItems(transactItems: TransactItem[], args?: TransactWriteItemsInput): Promise<Record<string, ResponseItem[]>>;
33
77
  export {};
@@ -3,6 +3,50 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.transactWriteItems = void 0;
4
4
  const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
5
5
  const lib_1 = require("../lib");
6
+ /**
7
+ * Performs a TransactWriteItems operation against DynamoDB. This allows you to perform multiple write operations in a single transaction.
8
+ * @param transactItems - Array of items to transact. Each item can be a Put, Update, Delete, or ConditionCheck operation.
9
+ * @param args - The additional arguments to override or specify for {@link TransactWriteItemsCommandInput}
10
+ * @returns A promise that resolves to a record of response items
11
+ *
12
+ * @example
13
+ * Perform a TransactWriteItems operation
14
+ * ```javascript
15
+ * const response = await transactWriteItems([
16
+ * {
17
+ * Put: {
18
+ * item: {
19
+ * PK: "User/1",
20
+ * SK: "Book/1",
21
+ * title: "The Great Gatsby",
22
+ * author: "F. Scott Fitzgerald",
23
+ * released: 1925,
24
+ * },
25
+ * },
26
+ * },
27
+ * {
28
+ * Update: {
29
+ * key: { PK: "User/1", SK: "Book/1" },
30
+ * updateData: { title: "The Great Gatsby - Updated" },
31
+ * },
32
+ * },
33
+ * {
34
+ * Delete: {
35
+ * key: { PK: "User/1", SK: "Book/1" },
36
+ * },
37
+ * },
38
+ * {
39
+ * ConditionCheck: {
40
+ * key: { PK: "User/1", SK: "Book/1" },
41
+ * ConditionExpression: "#title = :title",
42
+ * conditionData: { title: "The Great Gatsby" },
43
+ * },
44
+ * },
45
+ * ]);
46
+ *
47
+ * console.log(response);
48
+ * ```
49
+ */
6
50
  async function transactWriteItems(transactItems, args = {}) {
7
51
  return new Promise(async (resolve, reject) => {
8
52
  args = (0, lib_1.withDefaults)(args, "transactWriteItems");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moicky/dynamodb",
3
- "version": "2.5.10",
3
+ "version": "2.6.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "description": "Contains a collection of convenience functions for working with AWS DynamoDB",
@@ -24,6 +24,7 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@aws-sdk/client-dynamodb": "^3.338.0",
27
+ "@aws-sdk/credential-providers": "^3.744.0",
27
28
  "@aws-sdk/util-dynamodb": "^3.338.0",
28
29
  "aws-crt": "^1.15.20"
29
30
  },
package/readme.md CHANGED
@@ -284,6 +284,62 @@ const id4 = await getAscendingId({
284
284
  console.log(id4); // "00000010"
285
285
  ```
286
286
 
287
+ ### TransactWriteItems
288
+
289
+ ```ts
290
+ import { transactWriteItems } from "@moicky/dynamodb";
291
+
292
+ // Perform a TransactWriteItems operation
293
+ const response = await transactWriteItems([
294
+ {
295
+ Put: {
296
+ item: {
297
+ PK: "User/1",
298
+ SK: "Book/1",
299
+ title: "The Great Gatsby",
300
+ author: "F. Scott Fitzgerald",
301
+ released: 1925,
302
+ },
303
+ },
304
+ },
305
+ {
306
+ Update: {
307
+ key: { PK: "User/1", SK: "Book/1" },
308
+ updateData: { title: "The Great Gatsby - Updated" },
309
+ },
310
+ },
311
+ {
312
+ Delete: {
313
+ key: { PK: "User/1", SK: "Book/1" },
314
+ },
315
+ },
316
+ {
317
+ ConditionCheck: {
318
+ key: { PK: "User/1", SK: "Book/1" },
319
+ ConditionExpression: "#title = :title",
320
+ conditionData: { title: "The Great Gatsby" },
321
+ },
322
+ },
323
+ ]);
324
+
325
+ console.log(response);
326
+ ```
327
+
328
+ ### TransactGetItems
329
+
330
+ ```ts
331
+ import { transactGetItems } from "@moicky/dynamodb";
332
+
333
+ // Perform a TransactGetItems operation
334
+ const items = await transactGetItems([
335
+ { key: { PK: "User/1", SK: "Book/1" } },
336
+ { key: { PK: "User/1", SK: "Book/2" } },
337
+ { key: { PK: "User/1", SK: "Book/3" } },
338
+ ]);
339
+
340
+ console.log(items);
341
+ ```
342
+
287
343
  ## Configuring global defaults
288
344
 
289
345
  Global defaults can be configured using the `initDefaults` function. This allows to provide but still override every property of the `args` parameter.