@moicky/dynamodb 2.0.1 → 2.2.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.
@@ -3,6 +3,38 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.removeAttributes = exports.updateItem = void 0;
4
4
  const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
5
5
  const lib_1 = require("../lib");
6
+ /**
7
+ * Updates an item in DynamoDB. All provided fields are overwritten.
8
+ *
9
+ * @param key - The primary key of the item to be updated
10
+ * @param data - An object containing the data to be updated
11
+ * @param args - Optional parameters for the {@link UpdateItemCommand}
12
+ * @returns Returns updated item if ReturnValues argument is set, otherwise undefined
13
+ *
14
+ * @example
15
+ * Update the item and overwrite all supplied fields
16
+ * ```javascript
17
+ * await updateItem(
18
+ * { PK: "User/1", SK: "Book/1" }, // reference to item
19
+ * { description: "A book about a rich guy", author: "F. Scott Fitzgerald" } // fields to update
20
+ * );
21
+ *
22
+ * // Conditionally update an item. 'maxReleased' will not be updated since it is referenced inside the ConditionExpression
23
+ * await updateItem(
24
+ * { PK: "User/1", SK: "Book/1" },
25
+ * { released: 2000, maxReleased: 1950 },
26
+ * { ConditionExpression: "#released < :maxReleased" }
27
+ * );
28
+ *
29
+ * // Return all attributes of the new item
30
+ * const newItem = await updateItem(
31
+ * { PK: "User/1", SK: "Book/1" },
32
+ * { released: 2000 },
33
+ * { ReturnValues: "ALL_NEW" }
34
+ * );
35
+ * console.log(newItem); // { "PK": "User/1", "SK": "Book/1", "released": 2000 }
36
+ * ```
37
+ */
6
38
  async function updateItem(key, data, args = {}) {
7
39
  args = (0, lib_1.withDefaults)(args, "updateItem");
8
40
  if (!Object.keys(data).includes("updatedAt")) {
@@ -30,6 +62,20 @@ async function updateItem(key, data, args = {}) {
30
62
  .then((res) => args?.ReturnValues ? (0, lib_1.unmarshallWithOptions)(res.Attributes) : undefined);
31
63
  }
32
64
  exports.updateItem = updateItem;
65
+ /**
66
+ * Removes specified attributes from an item in DynamoDB.
67
+ *
68
+ * @param key - The primary key of the item from which attributes should be removed
69
+ * @param {string[]} attributes - Array of attribute names to be removed
70
+ * @param args - Optional parameters for the {@link UpdateItemCommand}
71
+ * @returns Promise object representing the output of the DynamoDB UpdateItem command
72
+ *
73
+ * @example
74
+ * Remove a specific attribute from an item
75
+ * ```javascript
76
+ * await removeAttributes({ PK: "User/1", SK: "Book/1" }, ["description"]);
77
+ * ```
78
+ */
33
79
  async function removeAttributes(key, attributes, args = {}) {
34
80
  args = (0, lib_1.withDefaults)(args, "removeAttributes");
35
81
  const UpdateExpression = "REMOVE " + attributes.map((att) => `#${att}`).join(", ");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moicky/dynamodb",
3
- "version": "2.0.1",
3
+ "version": "2.2.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",
package/readme.md CHANGED
@@ -15,6 +15,7 @@ Contains convenience functions for all major dynamodb operations. Requires very
15
15
  - 👻 Will **use placeholders** to avoid colliding with [reserved words](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html) if applicable
16
16
  - 🌎 Supports globally defined default arguments for each operation ([example](#configuring-global-defaults))
17
17
  - 🔨 Supports fixes for several issues with dynamodb ([example](#applying-fixes))
18
+ - 📖 Offers a convenient way to use pagination with queries
18
19
 
19
20
  ## Installation
20
21
 
@@ -219,6 +220,24 @@ const booksWithFilter = await queryAllItems(
219
220
  // additional args with filterExpression for example
220
221
  { FilterExpression: "#released BETWEEN :from AND :to" }
221
222
  );
223
+
224
+ // Pagination
225
+ const { items, hasNextPage, hasPreviousPage, currentPage } =
226
+ await queryPaginatedItems(
227
+ "#PK = :PK and begins_with(#SK, :SK)",
228
+ { PK: "User/1", SK: "Book/" },
229
+ { pageSize: 100 }
230
+ );
231
+ // items: The items on the current page.
232
+ // currentPage: { number: 1, firstKey: { ... }, lastKey: { ... } }
233
+
234
+ const { items: nextItems, currentPage: nextPage } = await queryPaginatedItems(
235
+ "#PK = :PK and begins_with(#SK, :SK)",
236
+ { PK: "User/1", SK: "Book/" },
237
+ { pageSize: 100, currentPage }
238
+ );
239
+ // items: The items on the second page.
240
+ // currentPage: { number: 2, firstKey: { ... }, lastKey: { ... } }
222
241
  ```
223
242
 
224
243
  ### Miscellaneous