@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.
- package/dist/lib/client.d.ts +8 -0
- package/dist/lib/client.js +8 -0
- package/dist/lib/defaultArguments.d.ts +33 -1
- package/dist/lib/defaultArguments.js +31 -0
- package/dist/lib/fixes.d.ts +50 -0
- package/dist/lib/fixes.js +33 -0
- package/dist/lib/schemas.d.ts +31 -0
- package/dist/lib/schemas.js +24 -0
- package/dist/operations/delete.d.ts +63 -2
- package/dist/operations/delete.js +60 -1
- package/dist/operations/get.d.ts +77 -2
- package/dist/operations/get.js +73 -0
- package/dist/operations/misc.d.ts +44 -0
- package/dist/operations/misc.js +44 -0
- package/dist/operations/put.d.ts +43 -2
- package/dist/operations/put.js +52 -2
- package/dist/operations/query.d.ts +124 -0
- package/dist/operations/query.js +188 -5
- package/dist/operations/update.d.ts +46 -0
- package/dist/operations/update.js +46 -0
- package/package.json +1 -1
- package/readme.md +19 -0
|
@@ -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
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
|