@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.
@@ -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> | 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>[]>;
@@ -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 SK
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<UpdateItemCommandOutput>;
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 UpdateExpression = "SET " +
12
- Object.keys(data)
13
- .map((key) => `#${key} = :${key}`)
14
- .join(", ");
15
- return client_1.client.send(new client_dynamodb_1.UpdateItemCommand({
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
- ExpressionAttributeNames: (0, helpers_1.getAttributeNames)(data),
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moicky/dynamodb",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
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
@@ -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