dynamo-bao 0.1.1 → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Adrian Graham
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # DynamoBao
2
2
 
3
+ [![GitHub stars](https://img.shields.io/github/stars/aag1024/dynamo-bao.svg?style=social&label=Star)](https://github.com/aag1024/dynamo-bao)
4
+ [![npm version](https://badge.fury.io/js/dynamo-bao.svg)](https://badge.fury.io/js/dynamo-bao)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+
3
7
  DynamoBao is a simple lightweight library for building DynamoDB models in JavaScript. I've used
4
8
  DynamoDB for years and generally love it, but find getting started difficult and repetitive.
5
9
  DynamoBao is the tool I wish I had when I started.
@@ -34,26 +38,24 @@ models:
34
38
  }
35
39
  ```
36
40
 
37
- Based on this definition, the code generator will create a `User` model (in models/user) that you can use like this:
41
+ Based on this definition, the code generator will create a `User` model in the models directory that you can use like this:
38
42
 
39
43
  ```
40
44
  const { User } = require("./models/user");
41
45
  const userConfig = require("./config");
42
46
  const dynamoBao = require("dynamo-bao");
43
47
  async function testUserModel() {
44
- try {
45
- dynamoBao.initModels(userConfig);
46
-
47
- // Create a new user
48
- const user = new User({
49
- name: "Test User",
50
- email: "test@example.com",
51
- });
52
-
53
- await user.save();
54
- console.log("Created user:", user);
55
- console.log("Consumed capacity:", user.getNumericConsumedCapacity());
56
- }
48
+ dynamoBao.initModels(userConfig);
49
+
50
+ // Create a new user
51
+ const user = new User({
52
+ name: "Test User",
53
+ email: "test@example.com",
54
+ });
55
+
56
+ await user.save();
57
+ console.log("Created user:", user);
58
+ console.log("Consumed capacity:", user.getNumericConsumedCapacity());
57
59
  }
58
60
 
59
61
  testUserModel();
@@ -93,53 +95,50 @@ const { Post } = require("./models/post");
93
95
  const userConfig = require("./config");
94
96
  const dynamoBao = require("dynamo-bao");
95
97
  async function testUserModel() {
96
- try {
97
- dynamoBao.initModels(userConfig);
98
-
99
- // Create a new user
100
- const user = new User({
101
- name: "Test User",
102
- email: "test@example.com",
103
- });
104
-
105
- await user.save();
106
- console.log("Created user:", user.userId);
107
-
108
- // Find user by unique constraint
109
- const foundUser = await User.findByEmail("test@example.com");
110
- console.log("Found user by email:", foundUser);
111
-
112
-
113
- // Create some test posts for the user
114
- const post1 = new Post({
115
- userId: user.userId,
116
- title: "Test Post 1",
117
- content: "This is a test post",
118
- });
119
-
120
- const post2 = new Post({
121
- userId: user.userId,
122
- title: "Test Post 2",
123
- content: "This is another test post",
124
- });
125
-
126
- await Promise.all([post1.save(), post2.save()]);
127
-
128
- // User now has a queryPosts method (via the postsForUser index)
129
- const userPosts = await user.queryPosts();
130
- console.log("User posts:", userPosts.items.length);
131
-
132
- // Or add a filter condition to the query
133
- const filteredPosts = await user.queryPosts(null, {
134
- filter: {
135
- content: {
136
- $contains: "another"
137
- }
98
+ dynamoBao.initModels(userConfig);
99
+
100
+ // Create a new user
101
+ const user = new User({
102
+ name: "Test User",
103
+ email: "test@example.com",
104
+ });
105
+
106
+ await user.save();
107
+ console.log("Created user:", user.userId);
108
+
109
+ // Find user by unique constraint
110
+ const foundUser = await User.findByEmail("test@example.com");
111
+ console.log("Found user by email:", foundUser);
112
+
113
+
114
+ // Create some test posts for the user
115
+ const post1 = new Post({
116
+ userId: user.userId,
117
+ title: "Test Post 1",
118
+ content: "This is a test post",
119
+ });
120
+
121
+ const post2 = new Post({
122
+ userId: user.userId,
123
+ title: "Test Post 2",
124
+ content: "This is another test post",
125
+ });
126
+
127
+ await Promise.all([post1.save(), post2.save()]);
128
+
129
+ // User now has a queryPosts method (via the postsForUser index)
130
+ const userPosts = await user.queryPosts();
131
+ console.log("User posts:", userPosts.items.length);
132
+
133
+ // Or add a filter condition to the query
134
+ const filteredPosts = await user.queryPosts(null, {
135
+ filter: {
136
+ content: {
137
+ $contains: "another"
138
138
  }
139
- });
140
- console.log("User posts matching filter:", filteredPosts.items.length);
141
-
142
- }
139
+ }
140
+ });
141
+ console.log("User posts matching filter:", filteredPosts.items.length);
143
142
  }
144
143
 
145
144
  testUserModel();
@@ -160,39 +159,15 @@ npm install dynamo-bao
160
159
  Create a new Dynamo table for your project. You should have one table per project.
161
160
 
162
161
  ```
163
- npx bao-create-table
162
+ npx bao-init
164
163
  ```
165
164
 
166
- Create a simple `config.js` file:
165
+ This creates a `config.js` which contains settings for your project like AWS region and the table name.
167
166
 
168
- ```
169
- const path = require("path");
170
-
171
- const config = {
172
- aws: {
173
- region: "us-west-2",
174
- },
175
- db: {
176
- tableName: "dynamo-bao-dev", // make sure this matches what you created with bao-create-table
177
- },
178
- logging: {
179
- level: "ERROR",
180
- },
181
- paths: {
182
- # Default paths:
183
- # models/ => generated models
184
- # models.yaml => model definitions
185
- # fields/ => custom fields
186
- modelsDir: path.resolve(__dirname, "./models"),
187
- modelsDefinitionPath: path.resolve(__dirname, "./models.yaml"),
188
- fieldsDir: path.resolve(__dirname, "./fields"), // optional for custom fields
189
- },
190
- };
191
-
192
- module.exports = config;
193
- ```
167
+ It also creates a `models.yaml` file with a simple example model, and a `models` directory where
168
+ the generated models will be stored.
194
169
 
195
- Edit your `models.yaml` file to define your models.
170
+ Edit your `models.yaml` file to define these models.
196
171
 
197
172
  ```
198
173
  models:
@@ -236,7 +211,7 @@ Run the code generator to create the models. You can also run `npx bao-watch` to
236
211
  npx bao-codegen
237
212
  ```
238
213
 
239
- You should now have a `models` directory with the generated models.
214
+ You should now have generated models in the `models` directory.
240
215
 
241
216
  Let's try using the models. Add the following code to a file called `example.js`.
242
217
 
@@ -247,60 +222,56 @@ const { Post } = require("./models/post");
247
222
  const userConfig = require("./config");
248
223
  const dynamoBao = require("dynamo-bao");
249
224
  async function testUserModel() {
250
- try {
251
- dynamoBao.initModels(userConfig);
252
-
253
- // Find user by email
254
- const existingUser = await User.findByEmail("test@example.com");
255
- console.log("Found user by email:", existingUser.exists());
256
- if (existingUser.exists()) {
257
- await User.delete(existingUser.getPrimaryId());
258
- console.log("Deleted existing user");
259
- await new Promise((resolve) => setTimeout(resolve, 100)); // 100ms
260
- }
261
-
262
- // Create a new user
263
- const user = new User({
264
- name: "Test User",
265
- email: "test@example.com",
266
- role: "user",
267
- profilePictureUrl: "https://example.com/profile.jpg",
268
- });
269
-
270
- await user.save();
271
- console.log("Created user:", user.getPrimaryId());
272
-
273
- // Find user by email
274
- const foundUser = await User.findByEmail("test@example.com");
275
- console.log("Found user by email:", foundUser.getPrimaryId());
276
-
277
- // Create some test posts for the user
278
- const post1 = new Post({
279
- userId: user.userId,
280
- title: "Test Post 1",
281
- content: "This is a test post",
282
- });
283
-
284
- const post2 = new Post({
285
- userId: user.userId,
286
- title: "Test Post 2",
287
- content: "This is another test post",
288
- });
289
-
290
- await Promise.all([post1.save(), post2.save()]);
291
-
292
- // Query user posts
293
- const userPosts = await user.queryPosts();
294
- console.log("User posts:", userPosts.items.length);
295
-
296
- // Or add a filter condition to the query
297
- const filteredPosts = await user.queryPosts(null, {
298
- filter: { content: { $contains: "another" } },
299
- });
300
- console.log("User posts matching filter:", filteredPosts.items.length);
301
- } catch (error) {
302
- console.error("Error:", error);
225
+ dynamoBao.initModels(userConfig);
226
+
227
+ // Find user by email
228
+ const existingUser = await User.findByEmail("test@example.com");
229
+ console.log("Found user by email:", existingUser.exists());
230
+ if (existingUser.exists()) {
231
+ await User.delete(existingUser.getPrimaryId());
232
+ console.log("Deleted existing user");
233
+ await new Promise((resolve) => setTimeout(resolve, 100)); // 100ms
303
234
  }
235
+
236
+ // Create a new user
237
+ const user = new User({
238
+ name: "Test User",
239
+ email: "test@example.com",
240
+ role: "user",
241
+ profilePictureUrl: "https://example.com/profile.jpg",
242
+ });
243
+
244
+ await user.save();
245
+ console.log("Created user:", user.getPrimaryId());
246
+
247
+ // Find user by email
248
+ const foundUser = await User.findByEmail("test@example.com");
249
+ console.log("Found user by email:", foundUser.getPrimaryId());
250
+
251
+ // Create some test posts for the user
252
+ const post1 = new Post({
253
+ userId: user.userId,
254
+ title: "Test Post 1",
255
+ content: "This is a test post",
256
+ });
257
+
258
+ const post2 = new Post({
259
+ userId: user.userId,
260
+ title: "Test Post 2",
261
+ content: "This is another test post",
262
+ });
263
+
264
+ await Promise.all([post1.save(), post2.save()]);
265
+
266
+ // Query user posts
267
+ const userPosts = await user.queryPosts();
268
+ console.log("User posts:", userPosts.items.length);
269
+
270
+ // Or add a filter condition to the query
271
+ const filteredPosts = await user.queryPosts(null, {
272
+ filter: { content: { $contains: "another" } },
273
+ });
274
+ console.log("User posts matching filter:", filteredPosts.items.length);
304
275
  }
305
276
 
306
277
  // Run the test
@@ -6,6 +6,8 @@ const {
6
6
  ListTablesCommand,
7
7
  } = require("@aws-sdk/client-dynamodb");
8
8
  const readline = require("readline");
9
+ const fs = require("fs");
10
+ const path = require("path");
9
11
 
10
12
  // Create DynamoDB client at the top level
11
13
  const client = new DynamoDBClient();
@@ -48,6 +50,39 @@ const prompt = (query) => {
48
50
  );
49
51
  };
50
52
 
53
+ async function writeConfigFile(tableName) {
54
+ const region = await client.config.region();
55
+ const configContent = `const path = require("path");
56
+
57
+ const config = {
58
+ aws: {
59
+ region: "${region}",
60
+ },
61
+ db: {
62
+ tableName: "${tableName}",
63
+ },
64
+ logging: {
65
+ level: "ERROR",
66
+ },
67
+ paths: {
68
+ // Default paths:
69
+ // models/ => generated models
70
+ // models.yaml => model definitions
71
+ // fields/ => custom fields
72
+ modelsDir: path.resolve(__dirname, "./models"),
73
+ modelsDefinitionPath: path.resolve(__dirname, "./models.yaml"),
74
+ fieldsDir: path.resolve(__dirname, "./fields"), // optional for custom fields
75
+ },
76
+ };
77
+
78
+ module.exports = config;
79
+ `;
80
+
81
+ const configPath = path.resolve(process.cwd(), "config.js");
82
+ fs.writeFileSync(configPath, configContent);
83
+ console.log(`Config file written to: ${configPath}`);
84
+ }
85
+
51
86
  async function createTable() {
52
87
  // Prompt user for table name
53
88
  const defaultTableName = "dynamo-bao-dev";
@@ -119,14 +154,50 @@ async function createTable() {
119
154
  "Table created successfully:",
120
155
  response.TableDescription.TableName,
121
156
  );
157
+
158
+ // Add this line to write the config file after table creation
159
+ await writeConfigFile(tableName);
122
160
  } catch (error) {
123
161
  console.error("Error creating table:", error);
124
162
  }
125
163
  }
126
164
 
165
+ async function createDirectoriesAndFiles() {
166
+ // Create models directory
167
+ const modelsDir = path.resolve(process.cwd(), "models");
168
+ if (!fs.existsSync(modelsDir)) {
169
+ fs.mkdirSync(modelsDir);
170
+ console.log(`Created models directory at: ${modelsDir}`);
171
+ }
172
+
173
+ // Create models.yaml with default content
174
+ const modelsYamlPath = path.resolve(process.cwd(), "models.yaml");
175
+ const yamlContent = `# Edit this file to create your models. For more information see: aag1024.github.io/dynamo-bao/
176
+ #
177
+ # Here's a simple example:
178
+ # models:
179
+ # User: {
180
+ #
181
+ # modelPrefix: u
182
+ # fields:
183
+ # userId: {type: UlidField, autoAssign: true, required: true}
184
+ # name: {type: StringField, required: true}
185
+ # email: {type: EmailField, required: true}
186
+ # primaryKey: {partitionKey: userId}
187
+ # }
188
+ `;
189
+
190
+ if (!fs.existsSync(modelsYamlPath)) {
191
+ fs.writeFileSync(modelsYamlPath, yamlContent);
192
+ console.log(`Created models.yaml at: ${modelsYamlPath}`);
193
+ }
194
+ }
195
+
127
196
  async function main() {
128
197
  await checkAwsCredentials();
129
198
  await createTable();
199
+ // Add this line after table creation
200
+ await createDirectoriesAndFiles();
130
201
  }
131
202
 
132
203
  // Run the main function
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env node
2
+
3
+ console.log(`
4
+ 🧨🧨🧨
5
+ Are you sure you want to PERMANENTLY delete this table?
6
+
7
+ This will delete all the data in the table and cannot be undone.
8
+
9
+ If you want to do this, please use the following AWS cli command:
10
+
11
+ aws dynamodb delete-table --table-name {YOUR_TABLE_NAME}
12
+
13
+ If you don't know your table's name, look in config.js.
14
+ 🧨🧨🧨
15
+ `);
16
+
17
+ process.exit(1);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dynamo-bao",
3
- "version": "0.1.1",
4
- "description": "Simple powerful data modeleling primatives for DynamoDB in JavaScript",
3
+ "version": "0.1.3",
4
+ "description": "Simple powerful data modeling primitives for DynamoDB in JavaScript.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
7
7
  "test": "LOG_LEVEL=OFF jest",
@@ -27,7 +27,7 @@
27
27
  "license": "MIT",
28
28
  "repository": {
29
29
  "type": "git",
30
- "url": "git+https://github.com/adriangraham/dynamo-bao.git"
30
+ "url": "https://github.com/aag1024/dynamo-bao"
31
31
  },
32
32
  "dependencies": {
33
33
  "@aws-sdk/client-dynamodb": "^3.521.0",
@@ -48,8 +48,9 @@
48
48
  "serverless": "^3.38.0"
49
49
  },
50
50
  "bin": {
51
- "bao-create-table": "bin/dynamo-bao-create-table.js",
51
+ "bao-init": "bin/dynamo-bao-create-table.js",
52
52
  "bao-codegen": "bin/codegen.js",
53
- "bao-watch": "bin/watch.js"
53
+ "bao-watch": "bin/watch.js",
54
+ "bao-delete": "bin/dynamo-bao-delete-table.js"
54
55
  }
55
56
  }