dynamo-bao 0.2.2 → 0.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.
- package/README.md +35 -2
- package/bin/codegen.js +31 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,6 +22,7 @@ DynamoBao is the tool I wish I had when I started.
|
|
|
22
22
|
- Enforce unique constraints and use them for lookups
|
|
23
23
|
- Built-in multi-tenancy support with complete data isolation and concurrency safety
|
|
24
24
|
- Return total read/write consumed capacity (even when multiple operations were performed)
|
|
25
|
+
- Easily iterate over all items in a model for batch processing or migrations
|
|
25
26
|
|
|
26
27
|
## Requirements
|
|
27
28
|
|
|
@@ -150,6 +151,40 @@ async function testUserModel() {
|
|
|
150
151
|
testUserModel();
|
|
151
152
|
```
|
|
152
153
|
|
|
154
|
+
### Iterating over all items
|
|
155
|
+
|
|
156
|
+
DynamoBao makes it easy to iterate over all items in a model, which is useful for tasks like data migration, backfills, or reporting.
|
|
157
|
+
|
|
158
|
+
By default, all models are created with `iterable: true`. This automatically sets up a dedicated index that allows you to use the `iterateAll()` method on the model class.
|
|
159
|
+
|
|
160
|
+
```javascript
|
|
161
|
+
// Iterate over all posts, 100 at a time
|
|
162
|
+
for await (const batch of Post.iterateAll({ batchSize: 100 })) {
|
|
163
|
+
for (const post of batch) {
|
|
164
|
+
console.log(post.title);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
**Cost and Performance**
|
|
170
|
+
|
|
171
|
+
This convenience comes at a cost: every `create`, `update`, or `delete` operation on an iterable model requires a second write to the database to maintain the iteration index. This doubles the write cost for every item.
|
|
172
|
+
|
|
173
|
+
To prevent "hot partitions" on large models, the iteration index is automatically split into 10 "buckets" or partitions. The `iterateAll()` method handles fetching from all buckets seamlessly.
|
|
174
|
+
|
|
175
|
+
**Opting Out**
|
|
176
|
+
|
|
177
|
+
For high-volume, write-heavy models where you know you will never need to iterate (e.g., logging tables), you can disable this feature to save costs:
|
|
178
|
+
|
|
179
|
+
```yaml
|
|
180
|
+
models:
|
|
181
|
+
AnalyticsEvent:
|
|
182
|
+
modelPrefix: "ae"
|
|
183
|
+
iterable: false # Disabling iteration for this write-heavy model
|
|
184
|
+
fields:
|
|
185
|
+
# ...
|
|
186
|
+
```
|
|
187
|
+
|
|
153
188
|
## Installation / Quick Start
|
|
154
189
|
|
|
155
190
|
Make sure you have [AWS credentials setup in your environment](https://medium.com/@simonazhangzy/installing-and-configuring-the-aws-cli-7d33796e4a7c). You'll also need [node and npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) installed.
|
|
@@ -205,8 +240,6 @@ models:
|
|
|
205
240
|
primaryKey:
|
|
206
241
|
partitionKey: postId
|
|
207
242
|
indexes:
|
|
208
|
-
# Enables Post.queryAllPosts() to query all posts
|
|
209
|
-
allPosts: { partitionKey: modelPrefix, sortKey: postId, indexId: gsi1 }
|
|
210
243
|
# Enables user.queryPosts() to query posts for a user
|
|
211
244
|
postsForUser: { partitionKey: userId, sortKey: createdAt, indexId: gsi2 }
|
|
212
245
|
```
|
package/bin/codegen.js
CHANGED
|
@@ -45,8 +45,38 @@ function loadModelDefinitions(definitionsPath, fieldResolver) {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
// Process models to
|
|
48
|
+
// Process models to set defaults and handle mapping tables
|
|
49
49
|
for (const [modelName, modelDef] of Object.entries(models)) {
|
|
50
|
+
// Default 'iterable' based on tableType
|
|
51
|
+
if (modelDef.iterable === undefined) {
|
|
52
|
+
if (modelDef.tableType === "mapping") {
|
|
53
|
+
logger.debug(
|
|
54
|
+
`Model "${modelName}" is a mapping table, defaulting 'iterable' to false.`,
|
|
55
|
+
);
|
|
56
|
+
modelDef.iterable = false;
|
|
57
|
+
} else {
|
|
58
|
+
logger.debug(
|
|
59
|
+
`Model "${modelName}" is a standard table, defaulting 'iterable' to true.`,
|
|
60
|
+
);
|
|
61
|
+
modelDef.iterable = true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Default 'iterationBuckets'
|
|
66
|
+
if (modelDef.iterationBuckets === undefined) {
|
|
67
|
+
if (modelDef.iterable) {
|
|
68
|
+
logger.debug(
|
|
69
|
+
`Model "${modelName}" is iterable, defaulting 'iterationBuckets' to 10.`,
|
|
70
|
+
);
|
|
71
|
+
modelDef.iterationBuckets = 10;
|
|
72
|
+
} else {
|
|
73
|
+
logger.debug(
|
|
74
|
+
`Model "${modelName}" is not iterable, defaulting 'iterationBuckets' to 0.`,
|
|
75
|
+
);
|
|
76
|
+
modelDef.iterationBuckets = 0;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
50
80
|
if (modelDef.mapping) {
|
|
51
81
|
// For mapping tables, merge mapping properties with regular model properties
|
|
52
82
|
models[modelName] = {
|