dynamo-bao 0.2.2 → 0.2.4

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 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 ensure mapping tables get both regular and mapping functions
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] = {
@@ -135,21 +135,25 @@ function generateModelClass(
135
135
 
136
136
  logger.debug("fieldResolver customFields", fieldResolver.customFieldsPath);
137
137
 
138
- // get relative path for fieldResolver.customFieldsPath from projectPath
139
- const relativePath = path.relative(outputDir, fieldResolver.customFieldsPath);
140
- logger.debug("relativePath", relativePath);
141
-
142
- // Generate custom field imports
143
- const customFieldImports = Array.from(customFields)
144
- .map((fieldType) => {
145
- // Remove 'Field' suffix before converting to kebab case
146
- const baseName = fieldType.replace(/Field$/, "");
147
- const kebabName = baseName
148
- .replace(/([a-z0-9])([A-Z])/g, "$1-$2")
149
- .toLowerCase();
150
- return `const { ${fieldType} } = require('${relativePath}/${kebabName}-field');`;
151
- })
152
- .join("\n");
138
+ // Generate custom field imports only if we have custom fields and a path
139
+ const customFieldImports =
140
+ customFields.size > 0 && fieldResolver.customFieldsPath
141
+ ? Array.from(customFields)
142
+ .map((fieldType) => {
143
+ // Remove 'Field' suffix before converting to kebab case
144
+ const baseName = fieldType.replace(/Field$/, "");
145
+ const kebabName = baseName
146
+ .replace(/([a-z0-9])([A-Z])/g, "$1-$2")
147
+ .toLowerCase();
148
+ // get relative path for fieldResolver.customFieldsPath from projectPath
149
+ const relativePath = path.relative(
150
+ outputDir,
151
+ fieldResolver.customFieldsPath,
152
+ );
153
+ return `const { ${fieldType} } = require('${relativePath}/${kebabName}-field');`;
154
+ })
155
+ .join("\n")
156
+ : "";
153
157
 
154
158
  // Generate the final code with separated imports
155
159
  return `// 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynamo-bao",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Simple powerful data modeling primitives for DynamoDB in JavaScript.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {