@moicky/dynamodb 3.0.0 → 3.0.2

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.
@@ -59,7 +59,14 @@ type GetItemsArgs = Partial<BatchGetItemCommandInput & {
59
59
  * );
60
60
  * ```
61
61
  */
62
- export declare function getItems<T extends DynamoDBItem = DynamoDBItem>(keys: Partial<T>[], args?: GetItemsArgs, retry?: number): Promise<Array<T | undefined>>;
62
+ export declare function getItems<T extends DynamoDBItem>(keys: Partial<T>[], args?: GetItemsArgs, retry?: number): Promise<Array<T | undefined>>;
63
+ export declare function getItems<T extends [DynamoDBItem, ...DynamoDBItem[]]>(keys: {
64
+ [K in keyof T]: Partial<T[K]>;
65
+ } & {
66
+ length: T["length"];
67
+ }, args?: GetItemsArgs, retry?: number): Promise<{
68
+ [K in keyof T]: T[K] | undefined;
69
+ }>;
63
70
  /**
64
71
  * Retrieves all items from the DynamoDB table.
65
72
  * @param args - The additional arguments to override or specify for {@link ScanCommandInput}
@@ -40,35 +40,6 @@ async function getItem(key, args = {}) {
40
40
  .then((res) => res?.Item ? (0, lib_1.unmarshallWithOptions)(res.Item) : undefined);
41
41
  }
42
42
  exports.getItem = getItem;
43
- /**
44
- * Retrieves multiple items from the DynamoDB table using their key schema.
45
- * @param keys - The items with at least the partition key and the sort key (if applicable) of the items to get.
46
- * @param args - The additional arguments to override or specify for {@link GetItemsArgs}
47
- * @returns A promise that resolves to an array of unmarshalled items
48
- *
49
- * @example
50
- * Get items in default table
51
- * ```javascript
52
- * await getItems([
53
- * { PK: "User/1", SK: "Book/1", title: "The Great Gatsby", released: 1925 },
54
- * { PK: "User/1", SK: "Book/2" },
55
- * { PK: "User/1", SK: "Book/3" },
56
- * // ... infinite more items (will be grouped into batches of 100 due to aws limit) and retried up to 3 times
57
- * ]);
58
- * ```
59
- * @example
60
- * Get items in a different table
61
- * ```javascript
62
- * await getItems(
63
- * [
64
- * { PK: "User/1", SK: "Book/1", title: "The Great Gatsby", released: 1925 },
65
- * { PK: "User/1", SK: "Book/2" },
66
- * { PK: "User/1", SK: "Book/3" },
67
- * ],
68
- * { TableName: "AnotherTable" }
69
- * );
70
- * ```
71
- */
72
43
  async function getItems(keys, args = {}, retry = 0) {
73
44
  args = (0, lib_1.withDefaults)(args, "getItems");
74
45
  // creates batches of 100 items each and performs batchGet on every batch.
@@ -10,10 +10,12 @@ export declare class Transaction {
10
10
  private createdAt;
11
11
  private updatedAt;
12
12
  private operations;
13
- constructor({ tableName, createdAt, updatedAt, }?: {
13
+ private shouldSplitTransactions;
14
+ constructor({ tableName, createdAt, updatedAt, shouldSplitTransactions, }?: {
14
15
  tableName?: string;
15
16
  createdAt?: any;
16
17
  updatedAt?: any;
18
+ shouldSplitTransactions?: boolean;
17
19
  });
18
20
  private getItemKey;
19
21
  create<T extends ItemWithKey>(item: WithoutReferences<T>, args?: CreateOperation["args"]): CreateOperations<T>;
@@ -11,10 +11,15 @@ class Transaction {
11
11
  createdAt;
12
12
  updatedAt;
13
13
  operations = {};
14
- constructor({ tableName = (0, __1.getDefaultTable)(), createdAt = (0, __1.getItemModificationTimestamp)("createdAt"), updatedAt = (0, __1.getItemModificationTimestamp)("updatedAt"), } = {}) {
15
- this.tableName = tableName;
14
+ shouldSplitTransactions;
15
+ constructor({ tableName, createdAt, updatedAt, shouldSplitTransactions, } = {}) {
16
+ this.tableName = tableName ?? (0, __1.getDefaultTable)();
16
17
  this.createdAt = createdAt ?? (0, __1.getItemModificationTimestamp)("createdAt");
17
18
  this.updatedAt = updatedAt ?? (0, __1.getItemModificationTimestamp)("updatedAt");
19
+ this.shouldSplitTransactions =
20
+ shouldSplitTransactions ??
21
+ (0, __1.getConfig)().splitTransactionsIfAboveLimit ??
22
+ false;
18
23
  }
19
24
  getItemKey(item, tableName) {
20
25
  return (0, __1.getItemKey)(item, { TableName: tableName || this.tableName });
@@ -127,10 +132,10 @@ class Transaction {
127
132
  async execute(args) {
128
133
  args = (0, __1.withDefaults)(args, "transactWriteItems");
129
134
  return new Promise(async (resolve, reject) => {
130
- const shouldSplitTransactions = (0, __1.getConfig)().splitTransactionsIfAboveLimit ?? false;
131
135
  const operations = Object.values(this.operations);
132
136
  if (operations.length === 0 ||
133
- (operations.length > OPERATIONS_LIMIT && !shouldSplitTransactions)) {
137
+ (operations.length > OPERATIONS_LIMIT &&
138
+ !this.shouldSplitTransactions)) {
134
139
  reject(new Error("[@moicky/dynamodb]: Invalid number of operations"));
135
140
  }
136
141
  const conditionCheckItems = operations
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moicky/dynamodb",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
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",
@@ -25,12 +25,12 @@
25
25
  "dependencies": {
26
26
  "@aws-sdk/client-dynamodb": "^3.338.0",
27
27
  "@aws-sdk/util-dynamodb": "^3.338.0",
28
- "aws-crt": "^1.15.20"
28
+ "aws-crt": "^1.15.20",
29
+ "@vercel/functions": "^2.0.0"
29
30
  },
30
31
  "devDependencies": {
31
32
  "@types/jest": "^29.5.1",
32
33
  "@types/node": "^20.2.4",
33
- "@vercel/functions": "^2.0.0",
34
34
  "dotenv": "^16.0.3",
35
35
  "jest": "^29.5.0",
36
36
  "typescript": "^5.0.4"
package/readme.md CHANGED
@@ -108,7 +108,7 @@ await putItems([
108
108
  ```ts
109
109
  import { getItem, getItems, getAllItems } from "@moicky/dynamodb";
110
110
 
111
- // Passing more than just the key is possible, but will be removed to avoid errors
111
+ // Passing more than just the key is possible, but will be discarded under the hood before forwarding to AWS
112
112
 
113
113
  // Get single item
114
114
  await getItem({
@@ -126,7 +126,7 @@ await getItems([
126
126
  {
127
127
  PK: "User/1",
128
128
  SK: "Book/1",
129
- title: "The Great Gatsby", // additional fields will be removed before sending
129
+ title: "The Great Gatsby", // additional fields will be discarded before sending
130
130
  author: "F. Scott Fitzgerald",
131
131
  released: 1925,
132
132
  },
@@ -146,7 +146,7 @@ import { deleteItem, deleteItems } from "@moicky/dynamodb";
146
146
  await deleteItem({
147
147
  PK: "User/1",
148
148
  SK: "Book/1",
149
- title: "The Great Gatsby", // additional fields will be removed before sending to avoid errors
149
+ title: "The Great Gatsby", // additional fields will be discarded before sending to AWS
150
150
  author: "F. Scott Fitzgerald",
151
151
  released: 1925,
152
152
  });