@moicky/dynamodb 1.2.1 → 1.2.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.
@@ -5,3 +5,6 @@ export declare function splitEvery<T>(items: T[], limit?: number): T[][];
5
5
  export declare function getAttributeValues(key: any, attributesToGet?: string[]): Record<string, any>;
6
6
  export declare function getAttributeNames(key: any, attributesToGet?: string[]): Record<string, string>;
7
7
  export declare function getAttributesFromExpression(expression: string, prefix?: string): string[];
8
+ export declare function handleAliases(aliases: {
9
+ [attr: string]: string[];
10
+ }, args?: Record<string, any>): Record<string, any>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getAttributesFromExpression = exports.getAttributeNames = exports.getAttributeValues = exports.splitEvery = exports.stripKey = void 0;
3
+ exports.handleAliases = exports.getAttributesFromExpression = exports.getAttributeNames = exports.getAttributeValues = exports.splitEvery = exports.stripKey = void 0;
4
4
  const util_dynamodb_1 = require("@aws-sdk/util-dynamodb");
5
5
  const client_1 = require("../lib/client");
6
6
  // Since dynamo only accepts key atrtributes which are described in table schema
@@ -42,3 +42,15 @@ function getAttributesFromExpression(expression, prefix = "#") {
42
42
  ?.map((attr) => attr.slice(1)) || []);
43
43
  }
44
44
  exports.getAttributesFromExpression = getAttributesFromExpression;
45
+ function handleAliases(aliases, args = {}) {
46
+ Object.keys(aliases).forEach((key) => {
47
+ aliases[key].forEach((alias) => {
48
+ if (args[alias]) {
49
+ args[key] = args[alias];
50
+ delete args[alias];
51
+ }
52
+ });
53
+ });
54
+ return args;
55
+ }
56
+ exports.handleAliases = handleAliases;
@@ -1,4 +1,9 @@
1
- import { QueryCommandInput, QueryCommandOutput } from "@aws-sdk/client-dynamodb";
1
+ import { QueryCommandInput as _QueryCommandInput, QueryCommandOutput } from "@aws-sdk/client-dynamodb";
2
+ type QueryCommandInput = {
3
+ GSI: _QueryCommandInput["IndexName"];
4
+ Index: _QueryCommandInput["IndexName"];
5
+ } & _QueryCommandInput;
2
6
  export declare function query(keyCondition: string, key: any, args?: Partial<QueryCommandInput>): Promise<QueryCommandOutput>;
3
7
  export declare function queryItems(keyCondition: string, key: any, args?: Partial<QueryCommandInput>): Promise<Record<string, any>[]>;
4
8
  export declare function queryAllItems(keyCondition: string, key: any, args?: Partial<QueryCommandInput>): Promise<Record<string, any>[]>;
9
+ export {};
@@ -5,7 +5,11 @@ const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
5
5
  const util_dynamodb_1 = require("@aws-sdk/util-dynamodb");
6
6
  const client_1 = require("../lib/client");
7
7
  const helpers_1 = require("../lib/helpers");
8
+ const aliases = {
9
+ IndexName: ["GSI", "Index"],
10
+ };
8
11
  async function query(keyCondition, key, args = {}) {
12
+ args = (0, helpers_1.handleAliases)(aliases, args);
9
13
  return client_1.client.send(new client_dynamodb_1.QueryCommand({
10
14
  KeyConditionExpression: keyCondition,
11
15
  ExpressionAttributeValues: (0, helpers_1.getAttributeValues)(key, [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moicky/dynamodb",
3
- "version": "1.2.1",
3
+ "version": "1.2.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
@@ -31,7 +31,7 @@ import { initSchema } from "@moicky/dynamodb";
31
31
 
32
32
  // Should be called once at the start of the runtime before any operation is executed
33
33
  initSchema({
34
- // first one will be used if no TableName is specified
34
+ // first one will be used by default if no TableName is specified
35
35
  [process.env.DEFAULT_TABLE]: {
36
36
  hash: "PK",
37
37
  range: "SK",
@@ -146,8 +146,7 @@ await deleteItem({
146
146
  });
147
147
 
148
148
  // Delete multiple items
149
- // KeySchemas will be grouped into batches of 25 and will be retried up to 3 times if there are unprocessed items
150
- // Will only delete each keySchema only once, even if it is present multiple times in the array to improve performance
149
+ // Will only delete each keySchema once, even if it is present multiple times in the array to improve performance and avoid aws errors
151
150
  await deleteItems([
152
151
  { PK: "User/1", SK: "Book/1" },
153
152
  // ... infinite more items (will be grouped into batches of 25 due to aws limit) and retried up to 3 times
@@ -161,13 +160,13 @@ import { updateItem, removeAttributes } from "@moicky/dynamodb";
161
160
 
162
161
  // Update the item and overwrite all supplied fields
163
162
  await updateItem(
164
- { PK: "User/1", SK: "Book/1" },
165
- { description: "A book about a rich guy", author: "F. Scott Fitzgerald" }
163
+ { PK: "User/1", SK: "Book/1" }, // reference to item
164
+ { description: "A book about a rich guy", author: "F. Scott Fitzgerald" } // fields to update
166
165
  );
167
166
 
168
167
  await updateItem(
169
168
  { PK: "User/1", SK: "Book/1" },
170
- { released: 2000, maxReleased: 1950 }, // maxReleased will not be updated, since it is referenced inside the ConditionExpression
169
+ { released: 2000, maxReleased: 1950 }, // maxReleased will not be updated on the item, since it is referenced inside the ConditionExpression
171
170
  { ConditionExpression: "#released < :maxReleased" }
172
171
  );
173
172
 
@@ -187,7 +186,7 @@ await removeAttributes({ PK: "User/1", SK: "Book/1" }, ["description"]);
187
186
  import { query, queryItems, queryAllItems } from "@moicky/dynamodb";
188
187
 
189
188
  // You HAVE TO use placeholders for the keyCondition & filterExpression:
190
- // prefix the attributeNames with a hash (#) and the attributeValues with a colon (:)
189
+ // Prefix the attributeNames with a hash (#) and the attributeValues with a colon (:)
191
190
 
192
191
  // Query only using keyCondition and retrieve complete response
193
192
  const booksResponse = await query("#PK = :PK and begins_with(#SK, :SK)", {
@@ -217,8 +216,8 @@ const booksWithFilter = await queryAllItems(
217
216
  from: 1950,
218
217
  to: 2000,
219
218
  },
220
- // additional args with filterExpression
221
- { FilterExpression: "#released BETWEEN :from AND :to" } // allows to override all args
219
+ // additional args with filterExpression for example
220
+ { FilterExpression: "#released BETWEEN :from AND :to" }
222
221
  );
223
222
  ```
224
223
 
@@ -229,6 +228,7 @@ import { itemExists, getAscendingId } from "@moicky/dynamodb";
229
228
 
230
229
  // Check if an item exists using keySchema
231
230
  const exists = await itemExists({ PK: "User/1", SK: "Book/1" });
231
+ console.log(exists); // true / false
232
232
 
233
233
  // Generate ascending ID
234
234
  // Specify keySchemaHash and optionally item to start at using keySchemaRange
@@ -240,18 +240,18 @@ console.log(id1); // "00000010"
240
240
 
241
241
  // Example Structure 2: PK: "User/1", SK: "Book/{{ ASCENDING_ID }}"
242
242
  // Last item: { PK: "User/1", SK: "Book/00000009" }
243
- const id2 = await getAscendingId({ PK: "User/1", SKPrefix: "Book" });
243
+ const id2 = await getAscendingId({ PK: "User/1", SK: "Book" });
244
244
  console.log(id2); // "00000010"
245
245
 
246
246
  // Specify length of ID
247
- const id3 = await getAscendingId({ PK: "User/1", SKPrefix: "Book", length: 4 });
247
+ const id3 = await getAscendingId({ PK: "User/1", SK: "Book", length: 4 });
248
248
  console.log(id3); // "0010"
249
249
 
250
250
  // Example Structure 3: someKeySchemaHash: "User/1", SK: "Book/{{ ASCENDING_ID }}"
251
251
  // Last item: { someKeySchemaHash: "User/1", SK: "Book/00000009" }
252
252
  const id4 = await getAscendingId({
253
253
  someKeySchemaHash: "User/1",
254
- SKPrefix: "Book",
254
+ SK: "Book",
255
255
  });
256
256
  console.log(id4); // "00000010"
257
257
  ```
@@ -376,8 +376,10 @@ const result = await updateItem(
376
376
 
377
377
  ### Setup
378
378
 
379
- Requires `DEFAULT_TABLE` and `SECOND_TABLE` to be present inside the environment (`.env file`)
380
- Can be deployed using the `template.yml` on aws:
379
+ Requires environment variables to be present for the tests to successfully connect to dynamodb tables. You can find a list of required environment variables here:
380
+ [./test/setup/setup-each.js](./test/setup/setup-each.js)
381
+
382
+ They can be obtained using the **template.yml** which can be deployed on aws using:
381
383
 
382
384
  ```bash
383
385
  sam deploy