dynamo-bao 0.1.0 → 0.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.
Files changed (2) hide show
  1. package/README.md +68 -67
  2. package/package.json +2 -1
package/README.md CHANGED
@@ -13,8 +13,8 @@ DynamoBao is the tool I wish I had when I started.
13
13
  ## Key features
14
14
 
15
15
  - Model 1:1, 1:N, N:M relationships between objects
16
- - Efficiently load data: load related objects in parallel, cache data using a loading context
17
- - Minimize race conditions: save diffs, optimistic locking, version checks, atomic counters
16
+ - Efficiently load data: load related objects in parallel, cache data using loading contexts
17
+ - Minimize race conditions: save diffs, version checks on save, atomic counters
18
18
  - Enforce unique constraints and use them for lookups
19
19
  - Return total read/write consumed capacity (even when multiple operations were performed)
20
20
 
@@ -25,12 +25,11 @@ Step 1 is to define your models in a yaml file. Here's a simple example.
25
25
  ```
26
26
  models:
27
27
  User: {
28
- modelPrefix: u,
29
- fields: {
30
- userId: {type: UlidField, autoAssign: true, required: true},
31
- name: {type: StringField, required: true},
28
+ modelPrefix: u
29
+ fields:
30
+ userId: {type: UlidField, autoAssign: true, required: true}
31
+ name: {type: StringField, required: true}
32
32
  email: {type: EmailField, required: true}
33
- },
34
33
  primaryKey: {partitionKey: userId}
35
34
  }
36
35
  ```
@@ -64,27 +63,28 @@ testUserModel();
64
63
 
65
64
  ```
66
65
  models:
67
- User: {
68
- modelPrefix: u,
69
- fields: {
70
- userId: {type: UlidField, autoAssign: true, required: true},
71
- name: {type: StringField, required: true},
66
+ User:
67
+ modelPrefix: u # required short string to identify the model
68
+ fields:
69
+ userId: {type: UlidField, autoAssign: true, required: true}
70
+ name: {type: StringField, required: true}
72
71
  email: {type: EmailField, required: true}
73
- },
74
- primaryKey: {partitionKey: userId},
72
+ primaryKey: {partitionKey: userId} # required
73
+ # Add up to 3 unique constraints each with a unique id: uc1, uc2, uc3
75
74
  uniqueConstraints: {uniqueEmail: {field: email, uniqueConstraintId: uc1}}
76
- }
77
- Post: {
78
- modelPrefix: p,
79
- fields: {
80
- postId: {type: UlidField, autoAssign: true},
81
- userId: {type: RelatedField, model: User, required: true},
82
- title: {type: StringField, required: true},
75
+ Post:
76
+ modelPrefix: p
77
+ fields:
78
+ postId: {type: UlidField, autoAssign: true}
79
+ # Enables getUser on a Post object / load related data
80
+ userId: {type: RelatedField, model: User, required: true}
81
+ title: {type: StringField, required: true}
83
82
  content: {type: StringField, required: true}
84
- },
85
- primaryKey: {partitionKey: postId},
86
- indexes: {postsForUser: {partitionKey: userId, sortKey: postId, indexId: gsi2}}
87
- }
83
+ primaryKey: {partitionKey: postId}
84
+ indexes:
85
+ # Add up to 3 indexes; make sure each index has a unique id: gsi1, gsi2, gsi3
86
+ # Enables user.queryPosts() to query posts for a user
87
+ postsForUser: {partitionKey: userId, sortKey: postId, indexId: gsi2}
88
88
  ```
89
89
 
90
90
  ```
@@ -149,23 +149,18 @@ testUserModel();
149
149
 
150
150
  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.
151
151
 
152
- Install DynamoBao globally:
152
+ Create a project and setup some models. You'll also need to install DynamoBao locally in your project.
153
153
 
154
154
  ```
155
- npm install -g dynamo-bao
155
+ mkdir your-project
156
+ cd your-project
157
+ npm install dynamo-bao
156
158
  ```
157
159
 
158
160
  Create a new Dynamo table for your project. You should have one table per project.
159
161
 
160
162
  ```
161
- bao-create-table
162
- ```
163
-
164
- Create a project and setup some models.
165
-
166
- ```
167
- mkdir your-project
168
- cd your-project
163
+ npx bao-create-table
169
164
  ```
170
165
 
171
166
  Create a simple `config.js` file:
@@ -184,6 +179,10 @@ const config = {
184
179
  level: "ERROR",
185
180
  },
186
181
  paths: {
182
+ # Default paths:
183
+ # models/ => generated models
184
+ # models.yaml => model definitions
185
+ # fields/ => custom fields
187
186
  modelsDir: path.resolve(__dirname, "./models"),
188
187
  modelsDefinitionPath: path.resolve(__dirname, "./models.yaml"),
189
188
  fieldsDir: path.resolve(__dirname, "./fields"), // optional for custom fields
@@ -198,41 +197,43 @@ Edit your `models.yaml` file to define your models.
198
197
  ```
199
198
  models:
200
199
  User:
201
- modelPrefix: u
202
- fields: {
203
- userId: {type: UlidField, autoAssign: true, required: true},
204
- name: {type: StringField, required: true},
205
- email: {type: StringField, required: true},
206
- profilePictureUrl: {type: StringField},
207
- createdAt: {type: CreateDateField},
208
- modifiedAt: {type: ModifiedDateField},
209
- role: {type: StringField}
210
- },
211
- primaryKey: {partitionKey: userId},
212
- uniqueConstraints: {
213
- uniqueEmail: {field: email, uniqueConstraintId: uc1}
214
- }
200
+ modelPrefix: u # required short string to identify the model
201
+ fields:
202
+ userId: { type: UlidField, autoAssign: true, required: true }
203
+ name: { type: StringField, required: true }
204
+ email: { type: StringField, required: true }
205
+ profilePictureUrl: { type: StringField }
206
+ createdAt: { type: CreateDateField }
207
+ modifiedAt: { type: ModifiedDateField }
208
+ role: { type: StringField }
209
+ primaryKey:
210
+ partitionKey: userId
211
+ uniqueConstraints:
212
+ # Enforces unique constraint on email field and enables User.findByEmail()
213
+ uniqueEmail: { field: email, uniqueConstraintId: uc1 }
214
+
215
215
  Post:
216
- modelPrefix: p
217
- fields: {
218
- postId: {type: UlidField, autoAssign: true},
219
- userId: {type: RelatedField, model: User, required: true},
220
- title: {type: StringField, required: true},
221
- content: {type: StringField, required: true},
222
- createdAt: {type: CreateDateField},
223
- version: {type: VersionField}
224
- },
225
- primaryKey: {partitionKey: postId},
226
- indexes: {
227
- allPosts: {partitionKey: modelPrefix, sortKey: postId, indexId: gsi1},
228
- postsForUser: {partitionKey: userId, sortKey: createdAt, indexId: gsi2}
229
- }
216
+ modelPrefix: p # required short string to identify the model
217
+ fields:
218
+ postId: { type: UlidField, autoAssign: true }
219
+ userId: { type: RelatedField, model: User, required: true }
220
+ title: { type: StringField, required: true }
221
+ content: { type: StringField, required: true }
222
+ createdAt: { type: CreateDateField }
223
+ version: { type: VersionField }
224
+ primaryKey:
225
+ partitionKey: postId
226
+ indexes:
227
+ # Enables Post.queryAllPosts() to query all posts
228
+ allPosts: { partitionKey: modelPrefix, sortKey: postId, indexId: gsi1 }
229
+ # Enables user.queryPosts() to query posts for a user
230
+ postsForUser: { partitionKey: userId, sortKey: createdAt, indexId: gsi2 }
230
231
  ```
231
232
 
232
- Run the code generator to create the models. You can also run `bao-watch` to automatically regenerate the models when you make changes.
233
+ Run the code generator to create the models. You can also run `npx bao-watch` to automatically regenerate the models when you make changes.
233
234
 
234
235
  ```
235
- bao-codegen
236
+ npx bao-codegen
236
237
  ```
237
238
 
238
239
  You should now have a `models` directory with the generated models.
@@ -240,7 +241,7 @@ You should now have a `models` directory with the generated models.
240
241
  Let's try using the models. Add the following code to a file called `example.js`.
241
242
 
242
243
  ```
243
- # example.js
244
+ // example.js
244
245
  const { User } = require("./models/user");
245
246
  const { Post } = require("./models/post");
246
247
  const userConfig = require("./config");
@@ -251,7 +252,7 @@ async function testUserModel() {
251
252
 
252
253
  // Find user by email
253
254
  const existingUser = await User.findByEmail("test@example.com");
254
- console.log("Found user by email:", existingUser.getPrimaryId());
255
+ console.log("Found user by email:", existingUser.exists());
255
256
  if (existingUser.exists()) {
256
257
  await User.delete(existingUser.getPrimaryId());
257
258
  console.log("Deleted existing user");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynamo-bao",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Simple powerful data modeleling primatives for DynamoDB in JavaScript",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -32,6 +32,7 @@
32
32
  "dependencies": {
33
33
  "@aws-sdk/client-dynamodb": "^3.521.0",
34
34
  "@aws-sdk/lib-dynamodb": "^3.521.0",
35
+ "chokidar": "^4.0.3",
35
36
  "dotenv": "^16.4.7",
36
37
  "js-yaml": "^4.1.0",
37
38
  "ulid": "^2.3.0"