@moicky/dynamodb 2.2.1 → 2.3.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/defaultArguments.d.ts +5 -3
- package/dist/lib/defaultArguments.js +1 -1
- package/dist/lib/helpers.d.ts +2 -2
- package/dist/operations/delete.d.ts +2 -2
- package/dist/operations/get.d.ts +2 -2
- package/dist/operations/index.d.ts +2 -0
- package/dist/operations/index.js +2 -0
- package/dist/operations/misc.d.ts +1 -1
- package/dist/operations/put.d.ts +3 -3
- package/dist/operations/put.js +5 -5
- package/dist/operations/query.d.ts +4 -4
- package/dist/operations/transactGetItems.d.ts +42 -0
- package/dist/operations/transactGetItems.js +70 -0
- package/dist/operations/transactWriteItems.d.ts +33 -0
- package/dist/operations/transactWriteItems.js +114 -0
- package/dist/operations/update.d.ts +2 -2
- package/package.json +1 -1
- package/readme.md +2 -1
|
@@ -1,19 +1,21 @@
|
|
|
1
|
-
import { deleteItem, deleteItems, getAllItems, getItem, getItems, itemExists, putItem, putItems, query, queryAllItems, queryItems, queryPaginatedItems, removeAttributes, updateItem } from "../operations";
|
|
1
|
+
import { deleteItem, deleteItems, getAllItems, getItem, getItems, itemExists, putItem, putItems, query, queryAllItems, queryItems, queryPaginatedItems, removeAttributes, transactGetItems, transactWriteItems, updateItem } from "../operations";
|
|
2
2
|
export interface OperationArguments {
|
|
3
3
|
deleteItem?: Parameters<typeof deleteItem>[1];
|
|
4
4
|
deleteItems?: Parameters<typeof deleteItems>[1];
|
|
5
5
|
getItem?: Parameters<typeof getItem>[1];
|
|
6
6
|
getItems?: Parameters<typeof getItems>[1];
|
|
7
7
|
getAllItems?: Parameters<typeof getAllItems>[0];
|
|
8
|
-
queryPaginatedItems?: Parameters<typeof queryPaginatedItems>[2];
|
|
9
8
|
itemExists?: Parameters<typeof itemExists>[1];
|
|
10
9
|
putItem?: Parameters<typeof putItem>[1];
|
|
11
10
|
putItems?: Parameters<typeof putItems>[1];
|
|
12
11
|
query?: Parameters<typeof query>[2];
|
|
13
12
|
queryItems?: Parameters<typeof queryItems>[2];
|
|
14
13
|
queryAllItems?: Parameters<typeof queryAllItems>[2];
|
|
14
|
+
queryPaginatedItems?: Parameters<typeof queryPaginatedItems>[2];
|
|
15
15
|
updateItem?: Parameters<typeof updateItem>[2];
|
|
16
16
|
removeAttributes?: Parameters<typeof removeAttributes>[2];
|
|
17
|
+
transactGetItems?: Parameters<typeof transactGetItems>[1];
|
|
18
|
+
transactWriteItems?: Parameters<typeof transactWriteItems>[1];
|
|
17
19
|
}
|
|
18
20
|
/**
|
|
19
21
|
* Initializes the default arguments to use for all operations.
|
|
@@ -25,13 +27,13 @@ export interface OperationArguments {
|
|
|
25
27
|
* initDefaultArguments({
|
|
26
28
|
* getItem: { ConsistentRead: true },
|
|
27
29
|
* getAllItems: { ConsistentRead: true },
|
|
28
|
-
* queryPaginatedItems: { ConsistentRead: true, pageSize: 100 },
|
|
29
30
|
*
|
|
30
31
|
* itemExists: { ConsistentRead: true },
|
|
31
32
|
*
|
|
32
33
|
* query: { ConsistentRead: true },
|
|
33
34
|
* queryItems: { ConsistentRead: true },
|
|
34
35
|
* queryAllItems: { ConsistentRead: true },
|
|
36
|
+
* queryPaginatedItems: { ConsistentRead: true, pageSize: 100 },
|
|
35
37
|
* });
|
|
36
38
|
* ```
|
|
37
39
|
*/
|
|
@@ -12,13 +12,13 @@ let defaultArguments = {};
|
|
|
12
12
|
* initDefaultArguments({
|
|
13
13
|
* getItem: { ConsistentRead: true },
|
|
14
14
|
* getAllItems: { ConsistentRead: true },
|
|
15
|
-
* queryPaginatedItems: { ConsistentRead: true, pageSize: 100 },
|
|
16
15
|
*
|
|
17
16
|
* itemExists: { ConsistentRead: true },
|
|
18
17
|
*
|
|
19
18
|
* query: { ConsistentRead: true },
|
|
20
19
|
* queryItems: { ConsistentRead: true },
|
|
21
20
|
* queryAllItems: { ConsistentRead: true },
|
|
21
|
+
* queryPaginatedItems: { ConsistentRead: true, pageSize: 100 },
|
|
22
22
|
* });
|
|
23
23
|
* ```
|
|
24
24
|
*/
|
package/dist/lib/helpers.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export declare function stripKey(key: any, args?: {
|
|
2
2
|
TableName?: string;
|
|
3
|
-
}): Record<string,
|
|
3
|
+
}): Record<string, import("@aws-sdk/client-dynamodb").AttributeValue>;
|
|
4
4
|
export declare function splitEvery<T>(items: T[], limit?: number): T[][];
|
|
5
|
-
export declare function getAttributeValues(key: any, attributesToGet?: string[]): Record<string,
|
|
5
|
+
export declare function getAttributeValues(key: any, attributesToGet?: string[]): Record<string, import("@aws-sdk/client-dynamodb").AttributeValue>;
|
|
6
6
|
export declare function getAttributeNames(key: any, attributesToGet?: string[]): Record<string, string>;
|
|
7
7
|
export declare function getAttributesFromExpression(expression: string, prefix?: string): string[];
|
|
@@ -26,7 +26,7 @@ import { BatchWriteItemCommandInput, DeleteItemCommandInput, DeleteItemCommandOu
|
|
|
26
26
|
* );
|
|
27
27
|
* ```
|
|
28
28
|
*/
|
|
29
|
-
export declare function deleteItem(key:
|
|
29
|
+
export declare function deleteItem(key: Record<string, any>, args?: Partial<DeleteItemCommandInput>): Promise<DeleteItemCommandOutput>;
|
|
30
30
|
type DeleteItemsArgs = Partial<BatchWriteItemCommandInput & {
|
|
31
31
|
TableName?: string;
|
|
32
32
|
}>;
|
|
@@ -62,5 +62,5 @@ type DeleteItemsArgs = Partial<BatchWriteItemCommandInput & {
|
|
|
62
62
|
* );
|
|
63
63
|
* ```
|
|
64
64
|
*/
|
|
65
|
-
export declare function deleteItems(keys: any[], args?: DeleteItemsArgs, retry?: number): Promise<void>;
|
|
65
|
+
export declare function deleteItems(keys: Record<string, any>[], args?: DeleteItemsArgs, retry?: number): Promise<void>;
|
|
66
66
|
export {};
|
package/dist/operations/get.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ import { BatchGetItemCommandInput, GetItemCommandInput, ScanCommandInput } from
|
|
|
25
25
|
* );
|
|
26
26
|
* ```
|
|
27
27
|
*/
|
|
28
|
-
export declare function getItem(key:
|
|
28
|
+
export declare function getItem(key: Record<string, any>, args?: Partial<GetItemCommandInput>): Promise<Record<string, any> | undefined>;
|
|
29
29
|
type GetItemsArgs = Partial<BatchGetItemCommandInput & {
|
|
30
30
|
TableName?: string;
|
|
31
31
|
}>;
|
|
@@ -58,7 +58,7 @@ type GetItemsArgs = Partial<BatchGetItemCommandInput & {
|
|
|
58
58
|
* );
|
|
59
59
|
* ```
|
|
60
60
|
*/
|
|
61
|
-
export declare function getItems(keys: any[], args?: GetItemsArgs, retry?: number): Promise<Record<string, any>[]>;
|
|
61
|
+
export declare function getItems(keys: Record<string, any>[], args?: GetItemsArgs, retry?: number): Promise<Record<string, any>[]>;
|
|
62
62
|
/**
|
|
63
63
|
* Retrieves all items from the DynamoDB table.
|
|
64
64
|
* @param args - The additional arguments to override or specify for {@link ScanCommandInput}
|
package/dist/operations/index.js
CHANGED
|
@@ -20,3 +20,5 @@ __exportStar(require("./misc"), exports);
|
|
|
20
20
|
__exportStar(require("./put"), exports);
|
|
21
21
|
__exportStar(require("./query"), exports);
|
|
22
22
|
__exportStar(require("./update"), exports);
|
|
23
|
+
__exportStar(require("./transactGetItems"), exports);
|
|
24
|
+
__exportStar(require("./transactWriteItems"), exports);
|
|
@@ -12,7 +12,7 @@ import { GetItemCommandInput } from "@aws-sdk/client-dynamodb";
|
|
|
12
12
|
* console.log(exists); // true / false
|
|
13
13
|
* ```
|
|
14
14
|
*/
|
|
15
|
-
export declare function itemExists(key:
|
|
15
|
+
export declare function itemExists(key: Record<string, any>, args?: Partial<GetItemCommandInput>): Promise<boolean>;
|
|
16
16
|
/**
|
|
17
17
|
* Generate an ascending ID for an item in the DynamoDB table using the key schema.
|
|
18
18
|
* @param params - An object containing key schema information and optional ID length and TableName
|
package/dist/operations/put.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BatchWriteItemCommandInput, BatchWriteItemCommandOutput, PutItemCommandInput, PutItemCommandOutput } from "@aws-sdk/client-dynamodb";
|
|
2
2
|
/**
|
|
3
3
|
* Inserts an item into the DynamoDB table.
|
|
4
|
-
* @param
|
|
4
|
+
* @param item - The item to insert into the table.
|
|
5
5
|
* @param args - The additional arguments to override or specify for {@link PutItemCommandInput}
|
|
6
6
|
* @returns A promise that resolves to the output of {@link PutItemCommandOutput} or the unmarshalled attributes if 'ReturnValues' is specified in 'args'.
|
|
7
7
|
*
|
|
@@ -17,7 +17,7 @@ import { BatchWriteItemCommandInput, BatchWriteItemCommandOutput, PutItemCommand
|
|
|
17
17
|
* });
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
20
|
-
export declare function putItem(
|
|
20
|
+
export declare function putItem(item: Record<string, any>, args?: Partial<PutItemCommandInput>): Promise<PutItemCommandOutput | Record<string, any>>;
|
|
21
21
|
type PutItemsArgs = Partial<BatchWriteItemCommandInput & {
|
|
22
22
|
TableName?: string;
|
|
23
23
|
}>;
|
|
@@ -42,5 +42,5 @@ type PutItemsArgs = Partial<BatchWriteItemCommandInput & {
|
|
|
42
42
|
* ]);
|
|
43
43
|
* ```
|
|
44
44
|
*/
|
|
45
|
-
export declare function putItems(items: any[], args?: PutItemsArgs, retry?: number): Promise<BatchWriteItemCommandOutput[]>;
|
|
45
|
+
export declare function putItems(items: Record<string, any>[], args?: PutItemsArgs, retry?: number): Promise<BatchWriteItemCommandOutput[]>;
|
|
46
46
|
export {};
|
package/dist/operations/put.js
CHANGED
|
@@ -5,7 +5,7 @@ const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
|
5
5
|
const lib_1 = require("../lib");
|
|
6
6
|
/**
|
|
7
7
|
* Inserts an item into the DynamoDB table.
|
|
8
|
-
* @param
|
|
8
|
+
* @param item - The item to insert into the table.
|
|
9
9
|
* @param args - The additional arguments to override or specify for {@link PutItemCommandInput}
|
|
10
10
|
* @returns A promise that resolves to the output of {@link PutItemCommandOutput} or the unmarshalled attributes if 'ReturnValues' is specified in 'args'.
|
|
11
11
|
*
|
|
@@ -21,14 +21,14 @@ const lib_1 = require("../lib");
|
|
|
21
21
|
* });
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
|
-
async function putItem(
|
|
24
|
+
async function putItem(item, args = {}) {
|
|
25
25
|
args = (0, lib_1.withDefaults)(args, "putItem");
|
|
26
|
-
if (!Object.keys(
|
|
27
|
-
|
|
26
|
+
if (!Object.keys(item).includes("createdAt")) {
|
|
27
|
+
item.createdAt = Date.now();
|
|
28
28
|
}
|
|
29
29
|
return (0, lib_1.getClient)()
|
|
30
30
|
.send(new client_dynamodb_1.PutItemCommand({
|
|
31
|
-
Item: (0, lib_1.marshallWithOptions)(
|
|
31
|
+
Item: (0, lib_1.marshallWithOptions)(item),
|
|
32
32
|
...args,
|
|
33
33
|
TableName: args?.TableName || (0, lib_1.getDefaultTable)(),
|
|
34
34
|
}))
|
|
@@ -15,7 +15,7 @@ import { QueryCommandInput, QueryCommandOutput } from "@aws-sdk/client-dynamodb"
|
|
|
15
15
|
* });
|
|
16
16
|
* ```
|
|
17
17
|
*/
|
|
18
|
-
export declare function query(keyCondition: string, key:
|
|
18
|
+
export declare function query(keyCondition: string, key: Record<string, any>, args?: Partial<QueryCommandInput>): Promise<QueryCommandOutput>;
|
|
19
19
|
/**
|
|
20
20
|
* Query multiple items from the DynamoDB table using a key condition and unmarshalls the result.
|
|
21
21
|
* @param keyCondition - The condition for the key in DynamoDB QueryCommand.
|
|
@@ -32,7 +32,7 @@ export declare function query(keyCondition: string, key: any, args?: Partial<Que
|
|
|
32
32
|
* });
|
|
33
33
|
* ```
|
|
34
34
|
*/
|
|
35
|
-
export declare function queryItems(keyCondition: string, key:
|
|
35
|
+
export declare function queryItems(keyCondition: string, key: Record<string, any>, args?: Partial<QueryCommandInput>): Promise<Record<string, any>[]>;
|
|
36
36
|
/**
|
|
37
37
|
* Query all items from the DynamoDB table using a key condition and unmarshalls the result.
|
|
38
38
|
* This function retries until all items are retrieved due to the AWS limit of 1MB per query.
|
|
@@ -62,7 +62,7 @@ export declare function queryItems(keyCondition: string, key: any, args?: Partia
|
|
|
62
62
|
* );
|
|
63
63
|
* ```
|
|
64
64
|
*/
|
|
65
|
-
export declare function queryAllItems(keyCondition: string, key:
|
|
65
|
+
export declare function queryAllItems(keyCondition: string, key: Record<string, any>, args?: Partial<QueryCommandInput>): Promise<Record<string, any>[]>;
|
|
66
66
|
/**
|
|
67
67
|
* The structure for the PaginationPage type.
|
|
68
68
|
* @property number - The page number. Cannot be set manually.
|
|
@@ -125,4 +125,4 @@ export interface PaginationArgs extends Partial<Omit<QueryCommandInput, "Limit">
|
|
|
125
125
|
* // currentPage: { number: 2, firstKey: { ... }, lastKey: { ... } }
|
|
126
126
|
* ```
|
|
127
127
|
*/
|
|
128
|
-
export declare function queryPaginatedItems(keyCondition: string, key:
|
|
128
|
+
export declare function queryPaginatedItems(keyCondition: string, key: Record<string, any>, args: PaginationArgs): Promise<PaginationResult>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Get, TransactGetItemsCommandInput } from "@aws-sdk/client-dynamodb";
|
|
2
|
+
/**
|
|
3
|
+
* Performs a TransactGetItems operation against DynamoDB. This allows you to retrieve many items at once.
|
|
4
|
+
* @param keys - Array of items to retrieve. Each item requires a `key` property, with at least the partition and the sort key (according to the table definition).
|
|
5
|
+
* @param args - The additional arguments to override or specify for {@link TransactGetItemsCommandInput}
|
|
6
|
+
* @returns A promise that resolves to an unmarshalled items array
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* Get items from the default-table
|
|
10
|
+
* ```javascript
|
|
11
|
+
* const items = await transactGetItems([
|
|
12
|
+
* { key: { PK: "User/1", SK: "Book/1", title: "The Great Gatsby", released: 1925 }},
|
|
13
|
+
* { key: { PK: "User/1", SK: "Book/2" }},
|
|
14
|
+
* { key: { PK: "User/1", SK: "Book/3" }},
|
|
15
|
+
* ]);
|
|
16
|
+
* ```
|
|
17
|
+
* @example
|
|
18
|
+
* Get items from a different tables
|
|
19
|
+
* ```javascript
|
|
20
|
+
* const items = await transactGetItems([
|
|
21
|
+
* { TableName: "yourTable", key: { PK: "User/1", SK: "Book/2" }},
|
|
22
|
+
* { TableName: "yourOtherTable", key: { PK: "User/1", SK: "Book/3" }},
|
|
23
|
+
* ]);
|
|
24
|
+
* ```
|
|
25
|
+
* @example
|
|
26
|
+
* Get all items from a non-default table
|
|
27
|
+
* ```javascript
|
|
28
|
+
* const items = await transactGetItems(
|
|
29
|
+
* [
|
|
30
|
+
* { key: { PK: "User/1", SK: "Book/2" } },
|
|
31
|
+
* { key: { PK: "User/1", SK: "Book/3" } },
|
|
32
|
+
* ],
|
|
33
|
+
* { TableName: "yourTable" }
|
|
34
|
+
* );
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function transactGetItems(keys: Array<Omit<Get, "TableName" | "Key"> & {
|
|
38
|
+
key: Record<string, any>;
|
|
39
|
+
TableName?: string;
|
|
40
|
+
}>, args?: Partial<TransactGetItemsCommandInput & {
|
|
41
|
+
TableName?: string;
|
|
42
|
+
}>): Promise<Array<Record<string, any> | undefined>>;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transactGetItems = void 0;
|
|
4
|
+
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
5
|
+
const lib_1 = require("../lib");
|
|
6
|
+
/**
|
|
7
|
+
* Performs a TransactGetItems operation against DynamoDB. This allows you to retrieve many items at once.
|
|
8
|
+
* @param keys - Array of items to retrieve. Each item requires a `key` property, with at least the partition and the sort key (according to the table definition).
|
|
9
|
+
* @param args - The additional arguments to override or specify for {@link TransactGetItemsCommandInput}
|
|
10
|
+
* @returns A promise that resolves to an unmarshalled items array
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* Get items from the default-table
|
|
14
|
+
* ```javascript
|
|
15
|
+
* const items = await transactGetItems([
|
|
16
|
+
* { key: { PK: "User/1", SK: "Book/1", title: "The Great Gatsby", released: 1925 }},
|
|
17
|
+
* { key: { PK: "User/1", SK: "Book/2" }},
|
|
18
|
+
* { key: { PK: "User/1", SK: "Book/3" }},
|
|
19
|
+
* ]);
|
|
20
|
+
* ```
|
|
21
|
+
* @example
|
|
22
|
+
* Get items from a different tables
|
|
23
|
+
* ```javascript
|
|
24
|
+
* const items = await transactGetItems([
|
|
25
|
+
* { TableName: "yourTable", key: { PK: "User/1", SK: "Book/2" }},
|
|
26
|
+
* { TableName: "yourOtherTable", key: { PK: "User/1", SK: "Book/3" }},
|
|
27
|
+
* ]);
|
|
28
|
+
* ```
|
|
29
|
+
* @example
|
|
30
|
+
* Get all items from a non-default table
|
|
31
|
+
* ```javascript
|
|
32
|
+
* const items = await transactGetItems(
|
|
33
|
+
* [
|
|
34
|
+
* { key: { PK: "User/1", SK: "Book/2" } },
|
|
35
|
+
* { key: { PK: "User/1", SK: "Book/3" } },
|
|
36
|
+
* ],
|
|
37
|
+
* { TableName: "yourTable" }
|
|
38
|
+
* );
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
function transactGetItems(keys, args) {
|
|
42
|
+
return new Promise(async (resolve, reject) => {
|
|
43
|
+
args = (0, lib_1.withDefaults)(args, "transactGetItems");
|
|
44
|
+
const { TableName, ...otherArgs } = args;
|
|
45
|
+
const defaultTable = TableName || (0, lib_1.getDefaultTable)();
|
|
46
|
+
const batches = (0, lib_1.splitEvery)(keys, 100);
|
|
47
|
+
const results = [];
|
|
48
|
+
for (const batch of batches) {
|
|
49
|
+
await (0, lib_1.getClient)()
|
|
50
|
+
.send(new client_dynamodb_1.TransactGetItemsCommand({
|
|
51
|
+
TransactItems: batch.map((item) => {
|
|
52
|
+
const table = item.TableName || defaultTable;
|
|
53
|
+
return {
|
|
54
|
+
Get: {
|
|
55
|
+
Key: (0, lib_1.stripKey)(item.key, { TableName: table }),
|
|
56
|
+
TableName: table,
|
|
57
|
+
ExpressionAttributeNames: item.ExpressionAttributeNames,
|
|
58
|
+
ProjectionExpression: item.ProjectionExpression,
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}),
|
|
62
|
+
...otherArgs,
|
|
63
|
+
}))
|
|
64
|
+
.then((res) => results.push(res))
|
|
65
|
+
.catch(reject);
|
|
66
|
+
}
|
|
67
|
+
resolve(results.flatMap((result) => result.Responses?.map((item) => item?.Item ? (0, lib_1.unmarshallWithOptions)(item.Item) : undefined) || []));
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
exports.transactGetItems = transactGetItems;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ConditionCheck, Delete, ItemCollectionMetrics, Put, TransactWriteItem, TransactWriteItemsCommandInput, Update } from "@aws-sdk/client-dynamodb";
|
|
2
|
+
type TransactItem = {
|
|
3
|
+
ConditionCheck?: Omit<ConditionCheck, "Key" | "TableName"> & {
|
|
4
|
+
key: Record<string, any>;
|
|
5
|
+
conditionData: Record<string, any>;
|
|
6
|
+
TableName?: string;
|
|
7
|
+
};
|
|
8
|
+
Put?: Omit<Put, "Item" | "TableName"> & {
|
|
9
|
+
item: Record<string, any>;
|
|
10
|
+
TableName?: string;
|
|
11
|
+
conditionData?: Record<string, any>;
|
|
12
|
+
};
|
|
13
|
+
Delete?: Omit<Delete, "Key" | "TableName"> & {
|
|
14
|
+
key: Record<string, any>;
|
|
15
|
+
conditionData?: Record<string, any>;
|
|
16
|
+
TableName?: string;
|
|
17
|
+
};
|
|
18
|
+
Update?: Partial<Omit<Update, "Key" | "TableName">> & {
|
|
19
|
+
key: Record<string, any>;
|
|
20
|
+
updateData: Record<string, any>;
|
|
21
|
+
conditionData?: Record<string, any>;
|
|
22
|
+
TableName?: string;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
type TransactWriteItemsInput = Partial<Omit<TransactWriteItemsCommandInput, "TransactItems">> & {
|
|
26
|
+
TransactItems?: TransactWriteItem[];
|
|
27
|
+
TableName?: string;
|
|
28
|
+
};
|
|
29
|
+
type ResponseItem = Pick<ItemCollectionMetrics, "SizeEstimateRangeGB"> & {
|
|
30
|
+
Key: Record<string, any>;
|
|
31
|
+
};
|
|
32
|
+
export declare function transactWriteItems(transactItems: TransactItem[], args?: TransactWriteItemsInput): Promise<Record<string, ResponseItem[]>>;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transactWriteItems = void 0;
|
|
4
|
+
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
5
|
+
const lib_1 = require("../lib");
|
|
6
|
+
async function transactWriteItems(transactItems, args = {}) {
|
|
7
|
+
return new Promise(async (resolve, reject) => {
|
|
8
|
+
args = (0, lib_1.withDefaults)(args, "transactWriteItems");
|
|
9
|
+
if (transactItems.length > 100) {
|
|
10
|
+
throw new Error("[@moicky/dynamodb]: TransactWriteItems can only handle up to 100 items");
|
|
11
|
+
}
|
|
12
|
+
const now = Date.now();
|
|
13
|
+
const table = args.TableName || (0, lib_1.getDefaultTable)();
|
|
14
|
+
const populatedItems = transactItems.map((item) => {
|
|
15
|
+
if (item.ConditionCheck) {
|
|
16
|
+
return handleConditionCheck(item.ConditionCheck, { now, table });
|
|
17
|
+
}
|
|
18
|
+
else if (item.Put) {
|
|
19
|
+
return handlePutItem(item.Put, { now, table });
|
|
20
|
+
}
|
|
21
|
+
else if (item.Delete) {
|
|
22
|
+
return handleDeleteItem(item.Delete, { now, table });
|
|
23
|
+
}
|
|
24
|
+
else if (item.Update) {
|
|
25
|
+
return handleUpdateItem(item.Update, { now, table });
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
throw new Error("[@moicky/dynamodb]: Invalid TransactItem: " + JSON.stringify(item));
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
return (0, lib_1.getClient)()
|
|
32
|
+
.send(new client_dynamodb_1.TransactWriteItemsCommand({
|
|
33
|
+
TransactItems: populatedItems,
|
|
34
|
+
...args,
|
|
35
|
+
}))
|
|
36
|
+
.then((res) => {
|
|
37
|
+
const results = {};
|
|
38
|
+
Object.entries(res.ItemCollectionMetrics || {}).forEach(([tableName, metrics]) => {
|
|
39
|
+
const unmarshalledMetrics = metrics.map((metric) => ({
|
|
40
|
+
Key: (0, lib_1.unmarshallWithOptions)(metric.ItemCollectionKey || {}),
|
|
41
|
+
SizeEstimateRangeGB: metric.SizeEstimateRangeGB,
|
|
42
|
+
}));
|
|
43
|
+
if (!results[tableName]) {
|
|
44
|
+
results[tableName] = [];
|
|
45
|
+
}
|
|
46
|
+
results[tableName].push(...unmarshalledMetrics);
|
|
47
|
+
});
|
|
48
|
+
resolve(results);
|
|
49
|
+
})
|
|
50
|
+
.catch(reject);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
exports.transactWriteItems = transactWriteItems;
|
|
54
|
+
function handleExpressionAttributes(rest, data) {
|
|
55
|
+
return {
|
|
56
|
+
ExpressionAttributeValues: (0, lib_1.getAttributeValues)(data || {}, (0, lib_1.getAttributesFromExpression)(rest.ConditionExpression || "", ":")),
|
|
57
|
+
ExpressionAttributeNames: (0, lib_1.getAttributeNames)(data || {}, (0, lib_1.getAttributesFromExpression)(rest.ConditionExpression || "")),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function handleConditionCheck(params, args) {
|
|
61
|
+
const { key, conditionData, ...rest } = params;
|
|
62
|
+
return {
|
|
63
|
+
ConditionCheck: {
|
|
64
|
+
Key: (0, lib_1.stripKey)(key, { TableName: rest.TableName || args.table }),
|
|
65
|
+
...handleExpressionAttributes(rest, conditionData),
|
|
66
|
+
...rest,
|
|
67
|
+
TableName: rest.TableName || args.table,
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function handlePutItem(params, args) {
|
|
72
|
+
const populatedData = structuredClone(params.item);
|
|
73
|
+
populatedData.createdAt ??= args.now;
|
|
74
|
+
const { item, conditionData, ...rest } = params;
|
|
75
|
+
return {
|
|
76
|
+
Put: {
|
|
77
|
+
Item: (0, lib_1.marshallWithOptions)(populatedData),
|
|
78
|
+
...handleExpressionAttributes(rest, conditionData),
|
|
79
|
+
...rest,
|
|
80
|
+
TableName: rest.TableName || args.table,
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function handleDeleteItem(params, args) {
|
|
85
|
+
const { key, conditionData, ...rest } = params;
|
|
86
|
+
return {
|
|
87
|
+
Delete: {
|
|
88
|
+
Key: (0, lib_1.stripKey)(key, { TableName: rest.TableName || args.table }),
|
|
89
|
+
...handleExpressionAttributes(rest, conditionData),
|
|
90
|
+
...rest,
|
|
91
|
+
TableName: rest.TableName || args.table,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function handleUpdateItem(params, args) {
|
|
96
|
+
const { key, updateData, conditionData, ...rest } = params;
|
|
97
|
+
const populatedData = structuredClone(updateData);
|
|
98
|
+
populatedData.updatedAt ??= args.now;
|
|
99
|
+
const mergedData = { ...populatedData, ...conditionData };
|
|
100
|
+
const UpdateExpression = "SET " +
|
|
101
|
+
Object.keys(populatedData)
|
|
102
|
+
.map((key) => `#${key} = :${key}`)
|
|
103
|
+
.join(", ");
|
|
104
|
+
return {
|
|
105
|
+
Update: {
|
|
106
|
+
Key: (0, lib_1.stripKey)(key, { TableName: rest.TableName || args.table }),
|
|
107
|
+
UpdateExpression,
|
|
108
|
+
ExpressionAttributeValues: (0, lib_1.getAttributeValues)(mergedData),
|
|
109
|
+
ExpressionAttributeNames: (0, lib_1.getAttributeNames)({}, (0, lib_1.getAttributesFromExpression)(rest.ConditionExpression || "").concat(Object.keys(populatedData))),
|
|
110
|
+
...rest,
|
|
111
|
+
TableName: rest.TableName || args.table,
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -31,7 +31,7 @@ import { UpdateItemCommandInput, UpdateItemCommandOutput } from "@aws-sdk/client
|
|
|
31
31
|
* console.log(newItem); // { "PK": "User/1", "SK": "Book/1", "released": 2000 }
|
|
32
32
|
* ```
|
|
33
33
|
*/
|
|
34
|
-
export declare function updateItem(key:
|
|
34
|
+
export declare function updateItem(key: Record<string, any>, data: Record<string, any>, args?: Partial<UpdateItemCommandInput>): Promise<undefined | Record<string, any>>;
|
|
35
35
|
/**
|
|
36
36
|
* Removes specified attributes from an item in DynamoDB.
|
|
37
37
|
*
|
|
@@ -46,4 +46,4 @@ export declare function updateItem(key: any, data: any, args?: Partial<UpdateIte
|
|
|
46
46
|
* await removeAttributes({ PK: "User/1", SK: "Book/1" }, ["description"]);
|
|
47
47
|
* ```
|
|
48
48
|
*/
|
|
49
|
-
export declare function removeAttributes(key:
|
|
49
|
+
export declare function removeAttributes(key: Record<string, any>, attributes: string[], args?: Partial<UpdateItemCommandInput>): Promise<UpdateItemCommandOutput>;
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -16,6 +16,7 @@ Contains convenience functions for all major dynamodb operations. Requires very
|
|
|
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
18
|
- 📖 Offers a convenient way to use pagination with queries
|
|
19
|
+
- 🔨 Supports transactGetItems & transactWriteItems
|
|
19
20
|
|
|
20
21
|
## Installation
|
|
21
22
|
|
|
@@ -324,7 +325,7 @@ initFixes({
|
|
|
324
325
|
enabled: true, // default,
|
|
325
326
|
|
|
326
327
|
// Won't disable ConsistantRead if IndexName is specified here.
|
|
327
|
-
stillUseOnLocalIndexes: ["localIndexName1", "
|
|
328
|
+
stillUseOnLocalIndexes: ["localIndexName1", "localIndexName2"],
|
|
328
329
|
},
|
|
329
330
|
});
|
|
330
331
|
```
|