@moicky/dynamodb 2.3.1 → 2.3.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.
- package/package.json +1 -1
- package/readme.md +30 -15
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -15,8 +15,8 @@ Contains convenience functions for all major dynamodb operations. Requires very
|
|
|
15
15
|
- 👻 Will **use placeholders** to avoid colliding with [reserved words](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html) if applicable
|
|
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
|
-
- 📖 Offers a convenient way to use pagination with queries
|
|
19
|
-
-
|
|
18
|
+
- 📖 Offers a convenient way to use pagination with queries ([example](#paginated-items))
|
|
19
|
+
- 🗂️ Supports **transactGetItems** & **transactWriteItems**
|
|
20
20
|
|
|
21
21
|
## Installation
|
|
22
22
|
|
|
@@ -26,14 +26,14 @@ npm i @moicky/dynamodb
|
|
|
26
26
|
|
|
27
27
|
## Setup
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
Automatically grabs `DYNAMODB_TABLE` as an **environment variable** and assumes `PK` and `SK` as it's schema. Can be customized using `initSchema` with one or more tables:
|
|
30
30
|
|
|
31
31
|
```ts
|
|
32
32
|
import { initSchema } from "@moicky/dynamodb";
|
|
33
33
|
|
|
34
34
|
// Should be called once at the start of the runtime before any operation is executed
|
|
35
35
|
initSchema({
|
|
36
|
-
// first one will be used by default if no TableName is specified
|
|
36
|
+
// first one will be used by default if no TableName is specified when calling functions
|
|
37
37
|
[process.env.DEFAULT_TABLE]: {
|
|
38
38
|
hash: "PK",
|
|
39
39
|
range: "SK",
|
|
@@ -74,6 +74,8 @@ await deleteItem(item, { TableName: process.env.SECOND_TABLE });
|
|
|
74
74
|
|
|
75
75
|
### Put Items
|
|
76
76
|
|
|
77
|
+
Every put operation also adds the `createdAt` attribute with the current timestamp on each item.
|
|
78
|
+
|
|
77
79
|
```ts
|
|
78
80
|
import { putItem, putItems } from "@moicky/dynamodb";
|
|
79
81
|
|
|
@@ -157,6 +159,8 @@ await deleteItems([
|
|
|
157
159
|
|
|
158
160
|
### Update Items
|
|
159
161
|
|
|
162
|
+
Every update operation also upserts the `updatedAt` attribute with the current timestamp on each item.
|
|
163
|
+
|
|
160
164
|
```ts
|
|
161
165
|
import { updateItem, removeAttributes } from "@moicky/dynamodb";
|
|
162
166
|
|
|
@@ -221,7 +225,11 @@ const booksWithFilter = await queryAllItems(
|
|
|
221
225
|
// additional args with filterExpression for example
|
|
222
226
|
{ FilterExpression: "#released BETWEEN :from AND :to" }
|
|
223
227
|
);
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
#### Paginated Items
|
|
224
231
|
|
|
232
|
+
```ts
|
|
225
233
|
// Pagination
|
|
226
234
|
const { items, hasNextPage, hasPreviousPage, currentPage } =
|
|
227
235
|
await queryPaginatedItems(
|
|
@@ -235,7 +243,7 @@ const { items, hasNextPage, hasPreviousPage, currentPage } =
|
|
|
235
243
|
const { items: nextItems, currentPage: nextPage } = await queryPaginatedItems(
|
|
236
244
|
"#PK = :PK and begins_with(#SK, :SK)",
|
|
237
245
|
{ PK: "User/1", SK: "Book/" },
|
|
238
|
-
{ pageSize: 100, currentPage }
|
|
246
|
+
{ pageSize: 100, currentPage } // args.direction: 'next' or 'previous'
|
|
239
247
|
);
|
|
240
248
|
// items: The items on the second page.
|
|
241
249
|
// currentPage: { number: 2, firstKey: { ... }, lastKey: { ... } }
|
|
@@ -248,10 +256,10 @@ import { itemExists, getAscendingId } from "@moicky/dynamodb";
|
|
|
248
256
|
|
|
249
257
|
// Check if an item exists using keySchema
|
|
250
258
|
const exists = await itemExists({ PK: "User/1", SK: "Book/1" });
|
|
251
|
-
console.log(exists); // true
|
|
259
|
+
console.log(exists); // true or false
|
|
252
260
|
|
|
253
261
|
// Generate ascending ID
|
|
254
|
-
// Specify
|
|
262
|
+
// Specify Partition-Key and optionally the Sort-Key.
|
|
255
263
|
|
|
256
264
|
// Example Structure 1: PK: "User/1", SK: "{{ ASCENDING_ID }}"
|
|
257
265
|
// Last item: { PK: "User/1", SK: "00000009" }
|
|
@@ -260,18 +268,18 @@ console.log(id1); // "00000010"
|
|
|
260
268
|
|
|
261
269
|
// Example Structure 2: PK: "User/1", SK: "Book/{{ ASCENDING_ID }}"
|
|
262
270
|
// Last item: { PK: "User/1", SK: "Book/00000009" }
|
|
263
|
-
const id2 = await getAscendingId({ PK: "User/1", SK: "Book" });
|
|
271
|
+
const id2 = await getAscendingId({ PK: "User/1", SK: "Book/" });
|
|
264
272
|
console.log(id2); // "00000010"
|
|
265
273
|
|
|
266
274
|
// Specify length of ID
|
|
267
|
-
const id3 = await getAscendingId({ PK: "User/1", SK: "Book", length: 4 });
|
|
275
|
+
const id3 = await getAscendingId({ PK: "User/1", SK: "Book/", length: 4 });
|
|
268
276
|
console.log(id3); // "0010"
|
|
269
277
|
|
|
270
|
-
// Example Structure 3:
|
|
271
|
-
// Last item: {
|
|
278
|
+
// Example Structure 3: somePartitionKey: "User/1", SK: "Book/{{ ASCENDING_ID }}"
|
|
279
|
+
// Last item: { somePartitionKey: "User/1", SK: "Book/00000009" }
|
|
272
280
|
const id4 = await getAscendingId({
|
|
273
|
-
|
|
274
|
-
SK: "Book",
|
|
281
|
+
somePartitionKey: "User/1",
|
|
282
|
+
SK: "Book/",
|
|
275
283
|
});
|
|
276
284
|
console.log(id4); // "00000010"
|
|
277
285
|
```
|
|
@@ -283,9 +291,9 @@ Global defaults can be configured using the `initDefaults` function. This allows
|
|
|
283
291
|
Should be called before any DynamoDB operations are performed.
|
|
284
292
|
|
|
285
293
|
```ts
|
|
286
|
-
import { initDefaultArguments } from "@moicky/dynamodb";
|
|
294
|
+
import { initDefaultArguments, getItem } from "@moicky/dynamodb";
|
|
287
295
|
|
|
288
|
-
//
|
|
296
|
+
// This example enables consistent reads for all DynamoDB operations which support it.
|
|
289
297
|
initDefaultArguments({
|
|
290
298
|
getItem: { ConsistentRead: true },
|
|
291
299
|
getAllItems: { ConsistentRead: true },
|
|
@@ -296,6 +304,12 @@ initDefaultArguments({
|
|
|
296
304
|
queryItems: { ConsistentRead: true },
|
|
297
305
|
queryAllItems: { ConsistentRead: true },
|
|
298
306
|
});
|
|
307
|
+
|
|
308
|
+
// It is still possible to override any arguments when calling a function
|
|
309
|
+
const itemWithoutConsistentRead = await getItem(
|
|
310
|
+
{ PK: "User/1", SK: "Book/001" },
|
|
311
|
+
{ ConsistentRead: false }
|
|
312
|
+
);
|
|
299
313
|
```
|
|
300
314
|
|
|
301
315
|
## Applying fixes
|
|
@@ -325,6 +339,7 @@ initFixes({
|
|
|
325
339
|
enabled: true, // default,
|
|
326
340
|
|
|
327
341
|
// Won't disable ConsistantRead if IndexName is specified here.
|
|
342
|
+
// This works because DynamoDB supports ConsistantRead on LocalSecondaryIndexes
|
|
328
343
|
stillUseOnLocalIndexes: ["localIndexName1", "localIndexName2"],
|
|
329
344
|
},
|
|
330
345
|
});
|