dynamo-bao 0.1.1 → 0.1.2

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
@@ -34,26 +34,24 @@ models:
34
34
  }
35
35
  ```
36
36
 
37
- Based on this definition, the code generator will create a `User` model (in models/user) that you can use like this:
37
+ Based on this definition, the code generator will create a `User` model in the models directory that you can use like this:
38
38
 
39
39
  ```
40
40
  const { User } = require("./models/user");
41
41
  const userConfig = require("./config");
42
42
  const dynamoBao = require("dynamo-bao");
43
43
  async function testUserModel() {
44
- try {
45
- dynamoBao.initModels(userConfig);
44
+ dynamoBao.initModels(userConfig);
46
45
 
47
- // Create a new user
48
- const user = new User({
49
- name: "Test User",
50
- email: "test@example.com",
51
- });
46
+ // Create a new user
47
+ const user = new User({
48
+ name: "Test User",
49
+ email: "test@example.com",
50
+ });
52
51
 
53
- await user.save();
54
- console.log("Created user:", user);
55
- console.log("Consumed capacity:", user.getNumericConsumedCapacity());
56
- }
52
+ await user.save();
53
+ console.log("Created user:", user);
54
+ console.log("Consumed capacity:", user.getNumericConsumedCapacity());
57
55
  }
58
56
 
59
57
  testUserModel();
@@ -93,53 +91,50 @@ const { Post } = require("./models/post");
93
91
  const userConfig = require("./config");
94
92
  const dynamoBao = require("dynamo-bao");
95
93
  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
- }
94
+ dynamoBao.initModels(userConfig);
95
+
96
+ // Create a new user
97
+ const user = new User({
98
+ name: "Test User",
99
+ email: "test@example.com",
100
+ });
101
+
102
+ await user.save();
103
+ console.log("Created user:", user.userId);
104
+
105
+ // Find user by unique constraint
106
+ const foundUser = await User.findByEmail("test@example.com");
107
+ console.log("Found user by email:", foundUser);
108
+
109
+
110
+ // Create some test posts for the user
111
+ const post1 = new Post({
112
+ userId: user.userId,
113
+ title: "Test Post 1",
114
+ content: "This is a test post",
115
+ });
116
+
117
+ const post2 = new Post({
118
+ userId: user.userId,
119
+ title: "Test Post 2",
120
+ content: "This is another test post",
121
+ });
122
+
123
+ await Promise.all([post1.save(), post2.save()]);
124
+
125
+ // User now has a queryPosts method (via the postsForUser index)
126
+ const userPosts = await user.queryPosts();
127
+ console.log("User posts:", userPosts.items.length);
128
+
129
+ // Or add a filter condition to the query
130
+ const filteredPosts = await user.queryPosts(null, {
131
+ filter: {
132
+ content: {
133
+ $contains: "another"
138
134
  }
139
- });
140
- console.log("User posts matching filter:", filteredPosts.items.length);
141
-
142
- }
135
+ }
136
+ });
137
+ console.log("User posts matching filter:", filteredPosts.items.length);
143
138
  }
144
139
 
145
140
  testUserModel();
@@ -163,36 +158,12 @@ Create a new Dynamo table for your project. You should have one table per projec
163
158
  npx bao-create-table
164
159
  ```
165
160
 
166
- Create a simple `config.js` file:
161
+ This creates a `config.js` which contains settings for your project like AWS region and the table name.
167
162
 
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
- ```
163
+ It also creates a `models.yaml` file with a simple example model, and a `models` directory where
164
+ the generated models will be stored.
194
165
 
195
- Edit your `models.yaml` file to define your models.
166
+ Edit your `models.yaml` file to define these models.
196
167
 
197
168
  ```
198
169
  models:
@@ -236,7 +207,7 @@ Run the code generator to create the models. You can also run `npx bao-watch` to
236
207
  npx bao-codegen
237
208
  ```
238
209
 
239
- You should now have a `models` directory with the generated models.
210
+ You should now have generated models in the `models` directory.
240
211
 
241
212
  Let's try using the models. Add the following code to a file called `example.js`.
242
213
 
@@ -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: adriangraham.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.2",
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",
@@ -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
  }