@moicky/dynamodb 1.0.2 → 1.1.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/index.js +19 -0
- package/dist/lib/client.d.ts +0 -1
- package/dist/lib/client.js +11 -0
- package/dist/lib/helpers.js +40 -0
- package/dist/operations/delete.d.ts +1 -1
- package/dist/operations/delete.js +52 -0
- package/dist/operations/get.d.ts +1 -1
- package/dist/operations/get.js +77 -0
- package/dist/operations/index.js +22 -0
- package/dist/operations/misc.js +34 -0
- package/dist/operations/put.js +45 -0
- package/dist/operations/query.js +44 -0
- package/dist/operations/update.js +37 -0
- package/package.json +9 -12
- package/readme.md +226 -0
- package/dist/built/commonjs/index.js +0 -1
- package/dist/built/esnext/index.js +0 -1
package/readme.md
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# @moicky/dynamodb
|
|
2
|
+
|
|
3
|
+
## Stats
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+

|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
## Description
|
|
12
|
+
|
|
13
|
+
Contains convenience functions for all major dynamodb operations. Requires very little code to interact with items from aws dynamodb. Uses **aws sdk v3** and fixes several issues:
|
|
14
|
+
|
|
15
|
+
- 🎁 Will **automatically marshall and unmarshall** items
|
|
16
|
+
- 📦 Will **group items into batches** to avoid aws limits and improve performance
|
|
17
|
+
- 🔄 Will **retry** some operations (getItems, deleteItems) **up to 3 times** on unprocessed items
|
|
18
|
+
- 🔒 When specifying an item using its keySchema, all additional attributes (apart from **PK** and **SK**) will be removed to avoid errors
|
|
19
|
+
- 👻 Will **use placeholders** to avoid colliding with [reserved words](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html) if applicable
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm i @moicky/dynamodb
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Setup
|
|
28
|
+
|
|
29
|
+
Add `DYNAMODB_TABLE` as an **environment variable** containing the name of the dynamodb table. Also make sure to setup the required permissions to access the dynamodb table on aws or on your local machine. Also make sure to use `PK` and `SK` as keySchema attribute names in the table.
|
|
30
|
+
|
|
31
|
+
_Support for different keySchemas will follow 😉_
|
|
32
|
+
|
|
33
|
+
## Usage Examples
|
|
34
|
+
|
|
35
|
+
### Put Items
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { putItem, putItems } from "@moicky/dynamodb";
|
|
39
|
+
|
|
40
|
+
// Put single item into dynamodb
|
|
41
|
+
await putItem({
|
|
42
|
+
PK: "User/1",
|
|
43
|
+
SK: "Book/1",
|
|
44
|
+
title: "The Great Gatsby",
|
|
45
|
+
author: "F. Scott Fitzgerald",
|
|
46
|
+
released: 1925,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Put multiple items into dynamodb
|
|
50
|
+
await putItems([
|
|
51
|
+
{
|
|
52
|
+
PK: "User/1",
|
|
53
|
+
SK: "Book/1",
|
|
54
|
+
title: "The Great Gatsby",
|
|
55
|
+
author: "F. Scott Fitzgerald",
|
|
56
|
+
released: 1925,
|
|
57
|
+
},
|
|
58
|
+
// ... infinite more items (will be grouped into batches of 25 due to aws limit)
|
|
59
|
+
]);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Get Items
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { getItem, getItems, getAllItems } from "@moicky/dynamodb";
|
|
66
|
+
|
|
67
|
+
// Passing more than just the key is possible, but will be removed to avoid errors
|
|
68
|
+
|
|
69
|
+
// Get single item
|
|
70
|
+
await getItem({
|
|
71
|
+
PK: "User/1",
|
|
72
|
+
SK: "Book/1",
|
|
73
|
+
title: "The Great Gatsby", // additional fields will be removed before sending
|
|
74
|
+
author: "F. Scott Fitzgerald",
|
|
75
|
+
released: 1925,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Get multiple items
|
|
79
|
+
// Items will be grouped into batches of 100 and will be retried up to 3 times if there are unprocessed items
|
|
80
|
+
// Will also only request each keySchema once, even if it is present multiple times in the array to improve performance
|
|
81
|
+
await getItems([
|
|
82
|
+
{
|
|
83
|
+
PK: "User/1",
|
|
84
|
+
SK: "Book/1",
|
|
85
|
+
title: "The Great Gatsby", // additional fields will be removed before sending
|
|
86
|
+
author: "F. Scott Fitzgerald",
|
|
87
|
+
released: 1925,
|
|
88
|
+
},
|
|
89
|
+
// ... infinite more items (will be grouped into batches of 100 due to aws limit) and retried up to 3 times
|
|
90
|
+
]);
|
|
91
|
+
|
|
92
|
+
// Retrieve all items using ScanCommand
|
|
93
|
+
await getAllItems();
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Delete Items
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { deleteItem, deleteItems } from "@moicky/dynamodb";
|
|
100
|
+
|
|
101
|
+
// Passing more than just the key is possible, but will be removed to avoid errors
|
|
102
|
+
|
|
103
|
+
// Delete single item
|
|
104
|
+
await deleteItem({
|
|
105
|
+
PK: "User/1",
|
|
106
|
+
SK: "Book/1",
|
|
107
|
+
title: "The Great Gatsby", // additional fields will be removed before sending
|
|
108
|
+
author: "F. Scott Fitzgerald",
|
|
109
|
+
released: 1925,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Delete multiple items
|
|
113
|
+
// Keys will be grouped into batches of 25 and will be retried up to 3 times if there are unprocessed items
|
|
114
|
+
// Will also only delete each keySchema once, even if it is present multiple times in the array to improve performance
|
|
115
|
+
await deleteItems([
|
|
116
|
+
{
|
|
117
|
+
PK: "User/1",
|
|
118
|
+
SK: "Book/1",
|
|
119
|
+
title: "The Great Gatsby", // additional fields will be removed before sending
|
|
120
|
+
author: "F. Scott Fitzgerald",
|
|
121
|
+
released: 1925,
|
|
122
|
+
},
|
|
123
|
+
// ... infinite more items (will be grouped into batches of 25 due to aws limit) and retried up to 3 times
|
|
124
|
+
]);
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Update Items
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
import { updateItem, removeAttributes } from "@moicky/dynamodb";
|
|
131
|
+
|
|
132
|
+
// Passing more than just the key is possible, but will be removed to avoid errors
|
|
133
|
+
|
|
134
|
+
// Update the item and overwrite all supplied fields
|
|
135
|
+
await updateItem(
|
|
136
|
+
{
|
|
137
|
+
PK: "User/1",
|
|
138
|
+
SK: "Book/1",
|
|
139
|
+
title: "The Great Gatsby", // additional fields will be removed before sending
|
|
140
|
+
},
|
|
141
|
+
{ description: "A book about a rich guy", author: "F. Scott Fitzgerald" }
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
// Completely remove fields from the item
|
|
145
|
+
await removeAttributes(
|
|
146
|
+
{
|
|
147
|
+
PK: "User/1",
|
|
148
|
+
SK: "Book/1",
|
|
149
|
+
title: "The Great Gatsby", // additional fields will be removed before sending
|
|
150
|
+
},
|
|
151
|
+
["description"]
|
|
152
|
+
);
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Query Items
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
import { query, queryItems, queryAllItems } from "@moicky/dynamodb";
|
|
159
|
+
|
|
160
|
+
// You have to use placeholders for the keyCondition & filterExpression:
|
|
161
|
+
// prefix the attributeNames with a hash (#) and the attributeValues with a colon (:)
|
|
162
|
+
|
|
163
|
+
// Query only using keyCondition and retrieve complete response
|
|
164
|
+
const booksResponse = await query("#PK = :PK and begins_with(#SK, :SK)", {
|
|
165
|
+
PK: "User/1",
|
|
166
|
+
SK: "Book/",
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// Query and retrieve unmarshalled items array
|
|
170
|
+
const books = await queryItems("#PK = :PK and begins_with(#SK, :SK)", {
|
|
171
|
+
PK: "User/1",
|
|
172
|
+
SK: "Book/",
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// Query and retry until all items are retrieved (due to aws limit of 1MB per query)
|
|
176
|
+
const allBooks = await queryAllItems("#PK = :PK and begins_with(#SK, :SK)", {
|
|
177
|
+
PK: "User/1",
|
|
178
|
+
SK: "Book/",
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Query with filterExpression (also specifiy attributes inside the key object)
|
|
182
|
+
const booksWithFilter = await queryAllItems(
|
|
183
|
+
"#PK = :PK and begins_with(#SK, :SK)", // keyCondition
|
|
184
|
+
{
|
|
185
|
+
// definition for all attributes
|
|
186
|
+
PK: "User/1",
|
|
187
|
+
SK: "Book/",
|
|
188
|
+
from: 1950,
|
|
189
|
+
to: 2000,
|
|
190
|
+
},
|
|
191
|
+
// additional args with filterExpression
|
|
192
|
+
{ FilterExpression: "#released BETWEEN :from AND :to" }
|
|
193
|
+
);
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Miscellaneous
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
import { itemExists, getNewId } from "@moicky/dynamodb";
|
|
200
|
+
|
|
201
|
+
// Check if an item exists using keySchema
|
|
202
|
+
const exists = await itemExists({ PK: "User/1", SK: "Book/1" });
|
|
203
|
+
|
|
204
|
+
// Generate ascending ID
|
|
205
|
+
// Example Structure 1: PK: "User/1", SK: "{{ ASCENDING_ID }}"
|
|
206
|
+
// Last item: { PK: "User/1", SK: "00000009" }
|
|
207
|
+
|
|
208
|
+
const id1 = await getNewId({ PK: "User/1" });
|
|
209
|
+
console.log(id1); // "00000010"
|
|
210
|
+
|
|
211
|
+
// Example Structure 2: PK: "User/1", SK: "Book/{{ ASCENDING_ID }}"
|
|
212
|
+
// Last item: { PK: "User/1", SK: "Book/00000009" }
|
|
213
|
+
|
|
214
|
+
const id2 = await getNewId({ PK: "User/1", SK: "Book" });
|
|
215
|
+
console.log(id2); // "00000010"
|
|
216
|
+
|
|
217
|
+
// Specify length of ID
|
|
218
|
+
const id3 = await getNewId({ PK: "User/1", SK: "Book", length: 4 });
|
|
219
|
+
console.log(id3); // "0010"
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Tests
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
npm run test
|
|
226
|
+
```
|