@moicky/dynamodb 2.3.6 → 2.4.0

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/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./lib";
2
2
  export * from "./operations";
3
+ export type * from "./types";
@@ -1,5 +1,6 @@
1
1
  import { QueryCommandInput, ScanCommandInput } from "@aws-sdk/client-dynamodb";
2
2
  import { marshall, marshallOptions, unmarshall, unmarshallOptions } from "@aws-sdk/util-dynamodb";
3
+ import { DynamoDBItem } from "../types";
3
4
  /**
4
5
  * DynamoDBFixes is a collection of fixes for DynamoDB.
5
6
  * @property disableConsistantReadWhenUsingIndexes - Disables ConsistentRead when using indexes.
@@ -63,4 +64,4 @@ export declare const marshallWithOptions: (input: Parameters<typeof marshall>[0]
63
64
  * @returns The unmarshalled input
64
65
  * @private
65
66
  */
66
- export declare const unmarshallWithOptions: (input: Parameters<typeof unmarshall>[0]) => Record<string, any>;
67
+ export declare const unmarshallWithOptions: <T extends DynamoDBItem = DynamoDBItem>(input: Parameters<typeof unmarshall>[0]) => T;
@@ -1,4 +1,5 @@
1
1
  import { BatchWriteItemCommandInput, DeleteItemCommandInput, DeleteItemCommandOutput } from "@aws-sdk/client-dynamodb";
2
+ import { DynamoDBItem } from "../types";
2
3
  /**
3
4
  * Deletes an item from the DynamoDB table using its key schema.
4
5
  * @param key - The item with at least the partition key and the sort key (if applicable) of the item to delete.
@@ -26,7 +27,7 @@ import { BatchWriteItemCommandInput, DeleteItemCommandInput, DeleteItemCommandOu
26
27
  * );
27
28
  * ```
28
29
  */
29
- export declare function deleteItem(key: Record<string, any>, args?: Partial<DeleteItemCommandInput>): Promise<DeleteItemCommandOutput>;
30
+ export declare function deleteItem(key: DynamoDBItem, args?: Partial<DeleteItemCommandInput>): Promise<DeleteItemCommandOutput>;
30
31
  type DeleteItemsArgs = Partial<BatchWriteItemCommandInput & {
31
32
  TableName?: string;
32
33
  }>;
@@ -62,5 +63,5 @@ type DeleteItemsArgs = Partial<BatchWriteItemCommandInput & {
62
63
  * );
63
64
  * ```
64
65
  */
65
- export declare function deleteItems(keys: Record<string, any>[], args?: DeleteItemsArgs, retry?: number): Promise<void>;
66
+ export declare function deleteItems(keys: DynamoDBItem[], args?: DeleteItemsArgs, retry?: number): Promise<void>;
66
67
  export {};
@@ -1,4 +1,5 @@
1
1
  import { BatchGetItemCommandInput, GetItemCommandInput, ScanCommandInput } from "@aws-sdk/client-dynamodb";
2
+ import { DynamoDBItem } from "../types";
2
3
  /**
3
4
  * Retrieves an item from the DynamoDB table using its key schema.
4
5
  * @param key - The item with at least the partition key and the sort key (if applicable) of the item to get.
@@ -25,7 +26,7 @@ import { BatchGetItemCommandInput, GetItemCommandInput, ScanCommandInput } from
25
26
  * );
26
27
  * ```
27
28
  */
28
- export declare function getItem(key: Record<string, any>, args?: Partial<GetItemCommandInput>): Promise<Record<string, any> | undefined>;
29
+ export declare function getItem<T extends DynamoDBItem = DynamoDBItem>(key: T, args?: Partial<GetItemCommandInput>): Promise<T | undefined>;
29
30
  type GetItemsArgs = Partial<BatchGetItemCommandInput & {
30
31
  TableName?: string;
31
32
  }>;
@@ -58,7 +59,7 @@ type GetItemsArgs = Partial<BatchGetItemCommandInput & {
58
59
  * );
59
60
  * ```
60
61
  */
61
- export declare function getItems(keys: Record<string, any>[], args?: GetItemsArgs, retry?: number): Promise<Record<string, any>[]>;
62
+ export declare function getItems<T extends DynamoDBItem = DynamoDBItem>(keys: T[], args?: GetItemsArgs, retry?: number): Promise<Array<T | undefined>>;
62
63
  /**
63
64
  * Retrieves all items from the DynamoDB table.
64
65
  * @param args - The additional arguments to override or specify for {@link ScanCommandInput}
@@ -77,5 +78,5 @@ export declare function getItems(keys: Record<string, any>[], args?: GetItemsArg
77
78
  * );
78
79
  * ```
79
80
  */
80
- export declare function getAllItems(args?: Partial<ScanCommandInput>): Promise<Record<string, any>[]>;
81
+ export declare function getAllItems<T extends DynamoDBItem = DynamoDBItem>(args?: Partial<ScanCommandInput>): Promise<T[]>;
81
82
  export {};
@@ -114,7 +114,7 @@ async function getItems(keys, args = {}, retry = 0) {
114
114
  .then((items) => results.push(...items));
115
115
  }));
116
116
  const resultItems = results
117
- .filter((i) => i)
117
+ .filter(Boolean)
118
118
  .reduce((acc, item) => {
119
119
  const keyString = JSON.stringify((0, lib_1.stripKey)(item, { TableName }));
120
120
  acc[keyString] = item;
@@ -148,6 +148,6 @@ async function getAllItems(args = {}) {
148
148
  ...args,
149
149
  TableName: args?.TableName || (0, lib_1.getDefaultTable)(),
150
150
  }))
151
- .then((res) => res.Items.map((item) => item && (0, lib_1.unmarshallWithOptions)(item)));
151
+ .then((res) => res.Items.map((item) => (0, lib_1.unmarshallWithOptions)(item)));
152
152
  }
153
153
  exports.getAllItems = getAllItems;
@@ -1,4 +1,5 @@
1
1
  import { GetItemCommandInput } from "@aws-sdk/client-dynamodb";
2
+ import { DynamoDBItem } from "../types";
2
3
  /**
3
4
  * Check if an item exists in the DynamoDB table using its key schema.
4
5
  * @param key - The item with at least the partition key and the sort key (if applicable) of the item to check.
@@ -12,7 +13,7 @@ import { GetItemCommandInput } from "@aws-sdk/client-dynamodb";
12
13
  * console.log(exists); // true / false
13
14
  * ```
14
15
  */
15
- export declare function itemExists(key: Record<string, any>, args?: Partial<GetItemCommandInput>): Promise<boolean>;
16
+ export declare function itemExists(key: DynamoDBItem, args?: Partial<GetItemCommandInput>): Promise<boolean>;
16
17
  /**
17
18
  * Generate an ascending ID for an item in the DynamoDB table using the key schema.
18
19
  * @param params - An object containing key schema information and optional ID length and TableName
@@ -1,4 +1,5 @@
1
1
  import { BatchWriteItemCommandInput, BatchWriteItemCommandOutput, PutItemCommandInput, PutItemCommandOutput } from "@aws-sdk/client-dynamodb";
2
+ import { DynamoDBItem } from "../types";
2
3
  /**
3
4
  * Inserts an item into the DynamoDB table.
4
5
  * @param item - The item to insert into the table.
@@ -17,7 +18,10 @@ import { BatchWriteItemCommandInput, BatchWriteItemCommandOutput, PutItemCommand
17
18
  * });
18
19
  * ```
19
20
  */
20
- export declare function putItem(item: Record<string, any>, args?: Partial<PutItemCommandInput>): Promise<PutItemCommandOutput | Record<string, any>>;
21
+ export declare function putItem<T extends DynamoDBItem>(item: T, args: Partial<PutItemCommandInput> & {
22
+ ReturnValues: string;
23
+ }): Promise<T>;
24
+ export declare function putItem<T extends DynamoDBItem, K extends Partial<PutItemCommandInput> = Partial<PutItemCommandInput>>(item: T, args?: K): Promise<PutItemCommandOutput>;
21
25
  type PutItemsArgs = Partial<BatchWriteItemCommandInput & {
22
26
  TableName?: string;
23
27
  }>;
@@ -42,5 +46,5 @@ type PutItemsArgs = Partial<BatchWriteItemCommandInput & {
42
46
  * ]);
43
47
  * ```
44
48
  */
45
- export declare function putItems(items: Record<string, any>[], args?: PutItemsArgs, retry?: number): Promise<BatchWriteItemCommandOutput[]>;
49
+ export declare function putItems(items: DynamoDBItem[], args?: PutItemsArgs, retry?: number): Promise<BatchWriteItemCommandOutput[]>;
46
50
  export {};
@@ -3,36 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.putItems = exports.putItem = void 0;
4
4
  const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
5
5
  const lib_1 = require("../lib");
6
- /**
7
- * Inserts an item into the DynamoDB table.
8
- * @param item - The item to insert into the table.
9
- * @param args - The additional arguments to override or specify for {@link PutItemCommandInput}
10
- * @returns A promise that resolves to the output of {@link PutItemCommandOutput} or the unmarshalled attributes if 'ReturnValues' is specified in 'args'.
11
- *
12
- * @example
13
- * Put a single item into DynamoDB
14
- * ```javascript
15
- * await putItem({
16
- * PK: "User/1",
17
- * SK: "Book/1",
18
- * title: "The Great Gatsby",
19
- * author: "F. Scott Fitzgerald",
20
- * released: 1925,
21
- * });
22
- * ```
23
- */
24
- async function putItem(item, args = {}) {
25
- args = (0, lib_1.withDefaults)(args, "putItem");
6
+ async function putItem(item, args) {
7
+ const argsWithDefaults = (0, lib_1.withDefaults)(args || {}, "putItem");
26
8
  if (!Object.keys(item).includes("createdAt")) {
27
9
  item.createdAt = Date.now();
28
10
  }
29
11
  return (0, lib_1.getClient)()
30
12
  .send(new client_dynamodb_1.PutItemCommand({
31
13
  Item: (0, lib_1.marshallWithOptions)(item),
32
- ...args,
33
- TableName: args?.TableName || (0, lib_1.getDefaultTable)(),
14
+ ...argsWithDefaults,
15
+ TableName: argsWithDefaults?.TableName || (0, lib_1.getDefaultTable)(),
34
16
  }))
35
- .then((res) => args?.ReturnValues ? (0, lib_1.unmarshallWithOptions)(res?.Attributes) : res);
17
+ .then((res) => argsWithDefaults?.ReturnValues
18
+ ? (0, lib_1.unmarshallWithOptions)(res?.Attributes)
19
+ : res);
36
20
  }
37
21
  exports.putItem = putItem;
38
22
  /**
@@ -1,4 +1,5 @@
1
1
  import { QueryCommandInput, QueryCommandOutput } from "@aws-sdk/client-dynamodb";
2
+ import { DynamoDBItem } from "../types";
2
3
  /**
3
4
  * Query a single item in a DynamoDB table using a key condition.
4
5
  * @param keyCondition - The condition for the key in DynamoDB QueryCommand.
@@ -32,7 +33,7 @@ export declare function query(keyCondition: string, key: Record<string, any>, ar
32
33
  * });
33
34
  * ```
34
35
  */
35
- export declare function queryItems(keyCondition: string, key: Record<string, any>, args?: Partial<QueryCommandInput>): Promise<Record<string, any>[]>;
36
+ export declare function queryItems<T extends DynamoDBItem = DynamoDBItem>(keyCondition: string, key: Record<string, any>, args?: Partial<QueryCommandInput>): Promise<T[]>;
36
37
  /**
37
38
  * Query all items from the DynamoDB table using a key condition and unmarshalls the result.
38
39
  * This function retries until all items are retrieved due to the AWS limit of 1MB per query.
@@ -62,7 +63,7 @@ export declare function queryItems(keyCondition: string, key: Record<string, any
62
63
  * );
63
64
  * ```
64
65
  */
65
- export declare function queryAllItems(keyCondition: string, key: Record<string, any>, args?: Partial<QueryCommandInput>): Promise<Record<string, any>[]>;
66
+ export declare function queryAllItems<T extends DynamoDBItem = DynamoDBItem>(keyCondition: string, key: Record<string, any>, args?: Partial<QueryCommandInput>): Promise<T[]>;
66
67
  /**
67
68
  * The structure for the PaginationPage type.
68
69
  * @property number - The page number. Cannot be set manually.
@@ -81,8 +82,8 @@ export type PaginationPage = {
81
82
  * @property hasNextPage - Whether there is a next page.
82
83
  * @property currentPage - The current page.
83
84
  */
84
- export type PaginationResult = {
85
- items: Record<string, any>[];
85
+ export type PaginationResult<T extends DynamoDBItem = DynamoDBItem> = {
86
+ items: T[];
86
87
  hasPreviousPage: boolean;
87
88
  hasNextPage: boolean;
88
89
  currentPage: PaginationPage;
@@ -125,4 +126,4 @@ export interface PaginationArgs extends Partial<Omit<QueryCommandInput, "Limit">
125
126
  * // currentPage: { number: 2, firstKey: { ... }, lastKey: { ... } }
126
127
  * ```
127
128
  */
128
- export declare function queryPaginatedItems(keyCondition: string, key: Record<string, any>, args: PaginationArgs): Promise<PaginationResult>;
129
+ export declare function queryPaginatedItems<T extends DynamoDBItem = DynamoDBItem>(keyCondition: string, key: Record<string, any>, args: PaginationArgs): Promise<PaginationResult<T>>;
@@ -67,7 +67,7 @@ async function queryItems(keyCondition, key, args = {}) {
67
67
  args = (0, lib_1.withDefaults)(args, "queryItems");
68
68
  return _query(keyCondition, key, args).then((res) => (res?.Items || [])
69
69
  .map((item) => item && (0, lib_1.unmarshallWithOptions)(item))
70
- .filter((item) => item));
70
+ .filter(Boolean));
71
71
  }
72
72
  exports.queryItems = queryItems;
73
73
  /**
@@ -1,4 +1,5 @@
1
1
  import { Get, TransactGetItemsCommandInput } from "@aws-sdk/client-dynamodb";
2
+ import { DynamoDBItem } from "../types";
2
3
  /**
3
4
  * Performs a TransactGetItems operation against DynamoDB. This allows you to retrieve many items at once.
4
5
  * @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).
@@ -34,9 +35,9 @@ import { Get, TransactGetItemsCommandInput } from "@aws-sdk/client-dynamodb";
34
35
  * );
35
36
  * ```
36
37
  */
37
- export declare function transactGetItems(keys: Array<Omit<Get, "TableName" | "Key"> & {
38
- key: Record<string, any>;
38
+ export declare function transactGetItems<T extends DynamoDBItem = DynamoDBItem>(keys: Array<Omit<Get, "TableName" | "Key"> & {
39
+ key: T;
39
40
  TableName?: string;
40
41
  }>, args?: Partial<TransactGetItemsCommandInput & {
41
42
  TableName?: string;
42
- }>): Promise<Array<Record<string, any> | undefined>>;
43
+ }>): Promise<Array<T | undefined>>;
@@ -1,4 +1,5 @@
1
1
  import { UpdateItemCommandInput, UpdateItemCommandOutput } from "@aws-sdk/client-dynamodb";
2
+ import { DynamoDBItem } from "../types";
2
3
  /**
3
4
  * Updates an item in DynamoDB. All provided fields are overwritten.
4
5
  *
@@ -31,7 +32,10 @@ import { UpdateItemCommandInput, UpdateItemCommandOutput } from "@aws-sdk/client
31
32
  * console.log(newItem); // { "PK": "User/1", "SK": "Book/1", "released": 2000 }
32
33
  * ```
33
34
  */
34
- export declare function updateItem(key: Record<string, any>, data: Record<string, any>, args?: Partial<UpdateItemCommandInput>): Promise<undefined | Record<string, any>>;
35
+ export declare function updateItem<T extends DynamoDBItem>(key: T, data: Partial<T>, args: Partial<UpdateItemCommandInput>): Promise<T>;
36
+ export declare function updateItem<T extends DynamoDBItem, K extends Partial<UpdateItemCommandInput> = Partial<UpdateItemCommandInput>>(key: T, data: Partial<T>, args?: K): Promise<K extends {
37
+ ReturnValues: string;
38
+ } ? T : undefined>;
35
39
  /**
36
40
  * Removes specified attributes from an item in DynamoDB.
37
41
  *
@@ -46,4 +50,4 @@ export declare function updateItem(key: Record<string, any>, data: Record<string
46
50
  * await removeAttributes({ PK: "User/1", SK: "Book/1" }, ["description"]);
47
51
  * ```
48
52
  */
49
- export declare function removeAttributes(key: Record<string, any>, attributes: string[], args?: Partial<UpdateItemCommandInput>): Promise<UpdateItemCommandOutput>;
53
+ export declare function removeAttributes<T extends DynamoDBItem = DynamoDBItem>(key: T, attributes: Array<keyof T>, args?: Partial<UpdateItemCommandInput>): Promise<UpdateItemCommandOutput>;
@@ -3,50 +3,18 @@ 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
- */
38
- async function updateItem(key, data, args = {}) {
39
- args = (0, lib_1.withDefaults)(args, "updateItem");
6
+ async function updateItem(key, data, args) {
7
+ const argsWithDefaults = (0, lib_1.withDefaults)(args || {}, "updateItem");
40
8
  if (!Object.keys(data).includes("updatedAt")) {
41
9
  data.updatedAt = Date.now();
42
10
  }
43
- const valuesInCondition = (0, lib_1.getAttributesFromExpression)(args?.ConditionExpression || "", ":");
44
- const namesInCondition = (0, lib_1.getAttributesFromExpression)(args?.ConditionExpression || "");
11
+ const valuesInCondition = (0, lib_1.getAttributesFromExpression)(argsWithDefaults?.ConditionExpression || "", ":");
12
+ const namesInCondition = (0, lib_1.getAttributesFromExpression)(argsWithDefaults?.ConditionExpression || "");
45
13
  const attributesToUpdate = Object.keys(data).filter((key) => !valuesInCondition.includes(key));
46
14
  const UpdateExpression = "SET " + attributesToUpdate.map((key) => `#${key} = :${key}`).join(", ");
47
15
  return (0, lib_1.getClient)()
48
16
  .send(new client_dynamodb_1.UpdateItemCommand({
49
- Key: (0, lib_1.stripKey)(key, args),
17
+ Key: (0, lib_1.stripKey)(key, argsWithDefaults),
50
18
  UpdateExpression,
51
19
  ExpressionAttributeValues: (0, lib_1.getAttributeValues)(data, [
52
20
  ...attributesToUpdate,
@@ -56,10 +24,12 @@ async function updateItem(key, data, args = {}) {
56
24
  ...attributesToUpdate,
57
25
  ...namesInCondition,
58
26
  ]),
59
- ...args,
60
- TableName: args?.TableName || (0, lib_1.getDefaultTable)(),
27
+ ...argsWithDefaults,
28
+ TableName: argsWithDefaults?.TableName || (0, lib_1.getDefaultTable)(),
61
29
  }))
62
- .then((res) => args?.ReturnValues ? (0, lib_1.unmarshallWithOptions)(res.Attributes) : undefined);
30
+ .then((res) => argsWithDefaults?.ReturnValues
31
+ ? (0, lib_1.unmarshallWithOptions)(res.Attributes)
32
+ : undefined);
63
33
  }
64
34
  exports.updateItem = updateItem;
65
35
  /**
@@ -78,7 +48,7 @@ exports.updateItem = updateItem;
78
48
  */
79
49
  async function removeAttributes(key, attributes, args = {}) {
80
50
  args = (0, lib_1.withDefaults)(args, "removeAttributes");
81
- const UpdateExpression = "REMOVE " + attributes.map((att) => `#${att}`).join(", ");
51
+ const UpdateExpression = "REMOVE " + attributes.map((att) => `#${String(att)}`).join(", ");
82
52
  return (0, lib_1.getClient)().send(new client_dynamodb_1.UpdateItemCommand({
83
53
  Key: (0, lib_1.stripKey)(key, args),
84
54
  UpdateExpression,
@@ -0,0 +1,4 @@
1
+ export type DynamoDBItem = Record<string, any> & {
2
+ createdAt?: number;
3
+ updatedAt?: number;
4
+ };
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moicky/dynamodb",
3
- "version": "2.3.6",
3
+ "version": "2.4.0",
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",