@moicky/dynamodb 1.1.2 → 1.1.3
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/operations/get.d.ts +1 -1
- package/dist/operations/misc.js +1 -3
- package/dist/operations/update.d.ts +1 -1
- package/dist/operations/update.js +17 -8
- package/package.json +1 -1
- package/readme.md +13 -0
package/dist/operations/get.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { BatchGetItemCommandInput, GetItemCommandInput, ScanCommandInput } from "@aws-sdk/client-dynamodb";
|
|
2
2
|
export declare function getItem(key: any, args?: Partial<GetItemCommandInput>): Promise<Record<string, any>>;
|
|
3
|
-
export declare function getItems(keys: any[], args?: Partial<BatchGetItemCommandInput>, retry?: number): Promise<Record<string, any>
|
|
3
|
+
export declare function getItems(keys: any[], args?: Partial<BatchGetItemCommandInput>, retry?: number): Promise<Record<string, any>[]>;
|
|
4
4
|
export declare function getAllItems(args?: Partial<ScanCommandInput>): Promise<Record<string, any>[]>;
|
package/dist/operations/misc.js
CHANGED
|
@@ -27,8 +27,6 @@ async function getNewId({ PK, SK, length = 8, }) {
|
|
|
27
27
|
}
|
|
28
28
|
const newId = parseInt(lastId) + 1 + "";
|
|
29
29
|
const withPadding = newId.padStart(length || 0, "0");
|
|
30
|
-
return
|
|
31
|
-
? `${SK}${!SK.endsWith("/") ? "/" : ""}${withPadding}`
|
|
32
|
-
: withPadding;
|
|
30
|
+
return withPadding;
|
|
33
31
|
}
|
|
34
32
|
exports.getNewId = getNewId;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { UpdateItemCommandInput, UpdateItemCommandOutput } from "@aws-sdk/client-dynamodb";
|
|
2
|
-
export declare function updateItem(key: any, data: any, args?: Partial<UpdateItemCommandInput>): Promise<
|
|
2
|
+
export declare function updateItem(key: any, data: any, args?: Partial<UpdateItemCommandInput>): Promise<undefined | Record<string, any>>;
|
|
3
3
|
export declare function removeAttributes(key: any, attributes: string[]): Promise<UpdateItemCommandOutput>;
|
|
@@ -4,22 +4,31 @@ exports.removeAttributes = exports.updateItem = void 0;
|
|
|
4
4
|
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
5
5
|
const client_1 = require("../lib/client");
|
|
6
6
|
const helpers_1 = require("../lib/helpers");
|
|
7
|
+
const util_dynamodb_1 = require("@aws-sdk/util-dynamodb");
|
|
7
8
|
async function updateItem(key, data, args = {}) {
|
|
8
9
|
if (!Object.keys(data).includes("updatedAt")) {
|
|
9
10
|
data.updatedAt = Date.now();
|
|
10
11
|
}
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return client_1.client
|
|
12
|
+
const valuesInCondition = (0, helpers_1.getAttributesFromExpression)(args?.ConditionExpression || "", ":");
|
|
13
|
+
const namesInCondition = (0, helpers_1.getAttributesFromExpression)(args?.ConditionExpression || "");
|
|
14
|
+
const attributesToUpdate = Object.keys(data).filter((key) => !valuesInCondition.includes(key));
|
|
15
|
+
const UpdateExpression = "SET " + attributesToUpdate.map((key) => `#${key} = :${key}`).join(", ");
|
|
16
|
+
return client_1.client
|
|
17
|
+
.send(new client_dynamodb_1.UpdateItemCommand({
|
|
16
18
|
TableName: client_1.TableName,
|
|
17
19
|
Key: (0, helpers_1.stripKey)(key),
|
|
18
20
|
UpdateExpression,
|
|
19
|
-
ExpressionAttributeValues: (0, helpers_1.getAttributeValues)(data
|
|
20
|
-
|
|
21
|
+
ExpressionAttributeValues: (0, helpers_1.getAttributeValues)(data, [
|
|
22
|
+
...attributesToUpdate,
|
|
23
|
+
...valuesInCondition,
|
|
24
|
+
]),
|
|
25
|
+
ExpressionAttributeNames: (0, helpers_1.getAttributeNames)(data, [
|
|
26
|
+
...attributesToUpdate,
|
|
27
|
+
...namesInCondition,
|
|
28
|
+
]),
|
|
21
29
|
...args,
|
|
22
|
-
}))
|
|
30
|
+
}))
|
|
31
|
+
.then((res) => args?.ReturnValues ? (0, util_dynamodb_1.unmarshall)(res.Attributes) : undefined);
|
|
23
32
|
}
|
|
24
33
|
exports.updateItem = updateItem;
|
|
25
34
|
async function removeAttributes(key, attributes) {
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -126,6 +126,19 @@ await updateItem(
|
|
|
126
126
|
{ description: "A book about a rich guy", author: "F. Scott Fitzgerald" }
|
|
127
127
|
);
|
|
128
128
|
|
|
129
|
+
await updateItem(
|
|
130
|
+
{ PK: "User/1", SK: "Book/1" },
|
|
131
|
+
{ released: 2000, maxReleased: 1950 }, // maxReleased will not be updated, since it is referenced inside the ConditionExpression
|
|
132
|
+
{ ConditionExpression: "#released < :maxReleased" }
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
const newItem = await updateItem(
|
|
136
|
+
{ PK: "User/1", SK: "Book/1" },
|
|
137
|
+
{ released: 2000 },
|
|
138
|
+
{ ReturnValues: "ALL_NEW" }
|
|
139
|
+
);
|
|
140
|
+
console.log(newItem); // { "PK": "User/1", "SK": "Book/1", "released": 2000 }
|
|
141
|
+
|
|
129
142
|
await removeAttributes({ PK: "User/1", SK: "Book/1" }, ["description"]);
|
|
130
143
|
```
|
|
131
144
|
|