dynamo-bao 0.4.0 → 0.4.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 +6 -0
- package/bin/bao-create-table.js +47 -0
- package/bin/codegen.js +3 -2
- package/bin/dynamo-bao-create-table.js +6 -97
- package/bin/dynamo-bao-update-table.js +6 -20
- package/bin/generators/model.js +2 -5
- package/bin/lib/create-table-params.js +93 -0
- package/bin/lib/resolve.js +7 -0
- package/bin/watch.js +2 -1
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -453,6 +453,12 @@ This creates a `config.js` which contains settings for your project like AWS reg
|
|
|
453
453
|
It also creates a `models.yaml` file with a simple example model, and a `models` directory where
|
|
454
454
|
the generated models will be stored.
|
|
455
455
|
|
|
456
|
+
If you only need to create the table without any project scaffolding (e.g., in CI/CD or scripts), use:
|
|
457
|
+
|
|
458
|
+
```
|
|
459
|
+
npx bao-create-table <table-name>
|
|
460
|
+
```
|
|
461
|
+
|
|
456
462
|
Edit your `models.yaml` file to define these models.
|
|
457
463
|
|
|
458
464
|
```
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const resolve = require("./lib/resolve");
|
|
4
|
+
const {
|
|
5
|
+
DynamoDBClient,
|
|
6
|
+
CreateTableCommand,
|
|
7
|
+
UpdateTimeToLiveCommand,
|
|
8
|
+
} = resolve("src/dynamodb-client.js");
|
|
9
|
+
const { TABLE_PARAMS, waitForTableActive } = require("./lib/create-table-params");
|
|
10
|
+
|
|
11
|
+
const tableName = process.argv[2];
|
|
12
|
+
|
|
13
|
+
if (!tableName) {
|
|
14
|
+
console.error("Usage: bao-create-table <table-name>");
|
|
15
|
+
process.exit(1);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const client = new DynamoDBClient();
|
|
19
|
+
|
|
20
|
+
async function main() {
|
|
21
|
+
const params = { ...TABLE_PARAMS, TableName: tableName };
|
|
22
|
+
|
|
23
|
+
const response = await client.send(new CreateTableCommand(params));
|
|
24
|
+
console.log(
|
|
25
|
+
"Table created successfully:",
|
|
26
|
+
response.TableDescription.TableName,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
console.log("Waiting for table to become active...");
|
|
30
|
+
await waitForTableActive(client, tableName);
|
|
31
|
+
|
|
32
|
+
await client.send(
|
|
33
|
+
new UpdateTimeToLiveCommand({
|
|
34
|
+
TableName: tableName,
|
|
35
|
+
TimeToLiveSpecification: {
|
|
36
|
+
AttributeName: "ttl",
|
|
37
|
+
Enabled: true,
|
|
38
|
+
},
|
|
39
|
+
}),
|
|
40
|
+
);
|
|
41
|
+
console.log("TTL enabled on attribute 'ttl'");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
main().catch((error) => {
|
|
45
|
+
console.error("Error creating table:", error);
|
|
46
|
+
process.exit(1);
|
|
47
|
+
});
|
package/bin/codegen.js
CHANGED
|
@@ -6,8 +6,9 @@ const yaml = require("js-yaml");
|
|
|
6
6
|
const { generateModelFiles } = require("./generators/model");
|
|
7
7
|
const { generateManifestFile } = require("./generators/manifest");
|
|
8
8
|
const { createLogger } = require("./utils/scriptLogger");
|
|
9
|
-
const
|
|
10
|
-
const
|
|
9
|
+
const resolve = require("./lib/resolve");
|
|
10
|
+
const FieldResolver = resolve("src/fieldResolver");
|
|
11
|
+
const { initConfig } = resolve("src/config");
|
|
11
12
|
|
|
12
13
|
// const __dirname = __dirname;
|
|
13
14
|
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
const resolve = require("./lib/resolve");
|
|
3
4
|
const {
|
|
4
5
|
DynamoDBClient,
|
|
5
6
|
CreateTableCommand,
|
|
6
7
|
ListTablesCommand,
|
|
7
|
-
DescribeTableCommand,
|
|
8
8
|
UpdateTimeToLiveCommand,
|
|
9
|
-
} =
|
|
9
|
+
} = resolve("src/dynamodb-client.js");
|
|
10
|
+
const { TABLE_PARAMS, waitForTableActive } = require("./lib/create-table-params");
|
|
10
11
|
const readline = require("readline");
|
|
11
12
|
const fs = require("fs");
|
|
12
13
|
const path = require("path");
|
|
@@ -88,23 +89,6 @@ module.exports = config;
|
|
|
88
89
|
console.log(`Config file written to: ${configPath}`);
|
|
89
90
|
}
|
|
90
91
|
|
|
91
|
-
async function waitForTableActive(tableName, maxAttempts = 30) {
|
|
92
|
-
for (let i = 0; i < maxAttempts; i++) {
|
|
93
|
-
const response = await client.send(
|
|
94
|
-
new DescribeTableCommand({ TableName: tableName }),
|
|
95
|
-
);
|
|
96
|
-
const status = response.Table.TableStatus;
|
|
97
|
-
if (status === "ACTIVE") {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
// Wait 2 seconds between checks
|
|
101
|
-
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
102
|
-
}
|
|
103
|
-
throw new Error(
|
|
104
|
-
`Table ${tableName} did not become active within ${maxAttempts * 2} seconds`,
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
92
|
async function createTable() {
|
|
109
93
|
// Prompt user for table name
|
|
110
94
|
const defaultTableName = "dynamo-bao-dev";
|
|
@@ -112,81 +96,7 @@ async function createTable() {
|
|
|
112
96
|
(await prompt(`Enter table name [${defaultTableName}]: `)) ||
|
|
113
97
|
defaultTableName;
|
|
114
98
|
|
|
115
|
-
|
|
116
|
-
const params = {
|
|
117
|
-
TableName: tableName,
|
|
118
|
-
BillingMode: "PAY_PER_REQUEST",
|
|
119
|
-
AttributeDefinitions: [
|
|
120
|
-
{ AttributeName: "_pk", AttributeType: "S" },
|
|
121
|
-
{ AttributeName: "_sk", AttributeType: "S" },
|
|
122
|
-
{ AttributeName: "_gsi1_pk", AttributeType: "S" },
|
|
123
|
-
{ AttributeName: "_gsi1_sk", AttributeType: "S" },
|
|
124
|
-
{ AttributeName: "_gsi2_pk", AttributeType: "S" },
|
|
125
|
-
{ AttributeName: "_gsi2_sk", AttributeType: "S" },
|
|
126
|
-
{ AttributeName: "_gsi3_pk", AttributeType: "S" },
|
|
127
|
-
{ AttributeName: "_gsi3_sk", AttributeType: "S" },
|
|
128
|
-
{ AttributeName: "_gsi4_pk", AttributeType: "S" },
|
|
129
|
-
{ AttributeName: "_gsi4_sk", AttributeType: "S" },
|
|
130
|
-
{ AttributeName: "_gsi5_pk", AttributeType: "S" },
|
|
131
|
-
{ AttributeName: "_gsi5_sk", AttributeType: "S" },
|
|
132
|
-
{ AttributeName: "_iter_pk", AttributeType: "S" },
|
|
133
|
-
{ AttributeName: "_iter_sk", AttributeType: "S" },
|
|
134
|
-
],
|
|
135
|
-
KeySchema: [
|
|
136
|
-
{ AttributeName: "_pk", KeyType: "HASH" },
|
|
137
|
-
{ AttributeName: "_sk", KeyType: "RANGE" },
|
|
138
|
-
],
|
|
139
|
-
GlobalSecondaryIndexes: [
|
|
140
|
-
{
|
|
141
|
-
IndexName: "gsi1",
|
|
142
|
-
KeySchema: [
|
|
143
|
-
{ AttributeName: "_gsi1_pk", KeyType: "HASH" },
|
|
144
|
-
{ AttributeName: "_gsi1_sk", KeyType: "RANGE" },
|
|
145
|
-
],
|
|
146
|
-
Projection: { ProjectionType: "ALL" },
|
|
147
|
-
},
|
|
148
|
-
{
|
|
149
|
-
IndexName: "gsi2",
|
|
150
|
-
KeySchema: [
|
|
151
|
-
{ AttributeName: "_gsi2_pk", KeyType: "HASH" },
|
|
152
|
-
{ AttributeName: "_gsi2_sk", KeyType: "RANGE" },
|
|
153
|
-
],
|
|
154
|
-
Projection: { ProjectionType: "ALL" },
|
|
155
|
-
},
|
|
156
|
-
{
|
|
157
|
-
IndexName: "gsi3",
|
|
158
|
-
KeySchema: [
|
|
159
|
-
{ AttributeName: "_gsi3_pk", KeyType: "HASH" },
|
|
160
|
-
{ AttributeName: "_gsi3_sk", KeyType: "RANGE" },
|
|
161
|
-
],
|
|
162
|
-
Projection: { ProjectionType: "ALL" },
|
|
163
|
-
},
|
|
164
|
-
{
|
|
165
|
-
IndexName: "gsi4",
|
|
166
|
-
KeySchema: [
|
|
167
|
-
{ AttributeName: "_gsi4_pk", KeyType: "HASH" },
|
|
168
|
-
{ AttributeName: "_gsi4_sk", KeyType: "RANGE" },
|
|
169
|
-
],
|
|
170
|
-
Projection: { ProjectionType: "ALL" },
|
|
171
|
-
},
|
|
172
|
-
{
|
|
173
|
-
IndexName: "gsi5",
|
|
174
|
-
KeySchema: [
|
|
175
|
-
{ AttributeName: "_gsi5_pk", KeyType: "HASH" },
|
|
176
|
-
{ AttributeName: "_gsi5_sk", KeyType: "RANGE" },
|
|
177
|
-
],
|
|
178
|
-
Projection: { ProjectionType: "ALL" },
|
|
179
|
-
},
|
|
180
|
-
{
|
|
181
|
-
IndexName: "iter_index",
|
|
182
|
-
KeySchema: [
|
|
183
|
-
{ AttributeName: "_iter_pk", KeyType: "HASH" },
|
|
184
|
-
{ AttributeName: "_iter_sk", KeyType: "RANGE" },
|
|
185
|
-
],
|
|
186
|
-
Projection: { ProjectionType: "KEYS_ONLY" },
|
|
187
|
-
},
|
|
188
|
-
],
|
|
189
|
-
};
|
|
99
|
+
const params = { ...TABLE_PARAMS, TableName: tableName };
|
|
190
100
|
|
|
191
101
|
try {
|
|
192
102
|
// Create table
|
|
@@ -199,7 +109,7 @@ async function createTable() {
|
|
|
199
109
|
|
|
200
110
|
// Wait for table to become active before enabling TTL
|
|
201
111
|
console.log("Waiting for table to become active...");
|
|
202
|
-
await waitForTableActive(tableName);
|
|
112
|
+
await waitForTableActive(client, tableName);
|
|
203
113
|
|
|
204
114
|
// Enable TTL (must be done after table creation via UpdateTimeToLive)
|
|
205
115
|
await client.send(
|
|
@@ -235,7 +145,7 @@ async function createDirectoriesAndFiles() {
|
|
|
235
145
|
# Here's a simple example:
|
|
236
146
|
# models:
|
|
237
147
|
# User: {
|
|
238
|
-
#
|
|
148
|
+
#
|
|
239
149
|
# modelPrefix: u
|
|
240
150
|
# fields:
|
|
241
151
|
# userId: {type: UlidField, autoAssign: true, required: true}
|
|
@@ -254,7 +164,6 @@ async function createDirectoriesAndFiles() {
|
|
|
254
164
|
async function main() {
|
|
255
165
|
await checkAwsCredentials();
|
|
256
166
|
await createTable();
|
|
257
|
-
// Add this line after table creation
|
|
258
167
|
await createDirectoriesAndFiles();
|
|
259
168
|
}
|
|
260
169
|
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
const resolve = require("./lib/resolve");
|
|
3
4
|
const {
|
|
4
5
|
DynamoDBClient,
|
|
5
6
|
DescribeTableCommand,
|
|
6
7
|
UpdateTableCommand,
|
|
7
8
|
DescribeTimeToLiveCommand,
|
|
8
9
|
UpdateTimeToLiveCommand,
|
|
9
|
-
} =
|
|
10
|
-
const { initConfig } =
|
|
10
|
+
} = resolve("src/dynamodb-client.js");
|
|
11
|
+
const { initConfig } = resolve("src/config.js");
|
|
11
12
|
|
|
12
13
|
// Expected GSI definitions - the full set that should exist on the table
|
|
13
14
|
const EXPECTED_GSIS = [
|
|
@@ -77,24 +78,9 @@ const ALL_ATTRIBUTE_DEFINITIONS = [
|
|
|
77
78
|
{ AttributeName: "_iter_sk", AttributeType: "S" },
|
|
78
79
|
];
|
|
79
80
|
|
|
80
|
-
|
|
81
|
+
const { waitForTableActive } = require("./lib/create-table-params");
|
|
81
82
|
|
|
82
|
-
|
|
83
|
-
for (let i = 0; i < maxAttempts; i++) {
|
|
84
|
-
const response = await client.send(
|
|
85
|
-
new DescribeTableCommand({ TableName: tableName }),
|
|
86
|
-
);
|
|
87
|
-
const status = response.Table.TableStatus;
|
|
88
|
-
if (status === "ACTIVE") {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
console.log(` Table status: ${status}, waiting...`);
|
|
92
|
-
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
93
|
-
}
|
|
94
|
-
throw new Error(
|
|
95
|
-
`Table ${tableName} did not become active within ${maxAttempts * 5} seconds`,
|
|
96
|
-
);
|
|
97
|
-
}
|
|
83
|
+
let client;
|
|
98
84
|
|
|
99
85
|
async function checkAndAddMissingGSIs(tableName) {
|
|
100
86
|
const response = await client.send(
|
|
@@ -147,7 +133,7 @@ async function checkAndAddMissingGSIs(tableName) {
|
|
|
147
133
|
console.log(
|
|
148
134
|
` GSI ${gsi.IndexName} creation initiated. Waiting for table to become active...`,
|
|
149
135
|
);
|
|
150
|
-
await waitForTableActive(tableName);
|
|
136
|
+
await waitForTableActive(client, tableName, { maxAttempts: 60, intervalMs: 5000 });
|
|
151
137
|
console.log(` GSI ${gsi.IndexName} is ready.`);
|
|
152
138
|
}
|
|
153
139
|
}
|
package/bin/generators/model.js
CHANGED
|
@@ -15,11 +15,8 @@ function generateModelClass(
|
|
|
15
15
|
outputDir,
|
|
16
16
|
moduleSystem = 'commonjs',
|
|
17
17
|
) {
|
|
18
|
-
//
|
|
19
|
-
const
|
|
20
|
-
const srcPath = path.join(projectRoot, 'src');
|
|
21
|
-
const relativeSrcPath = path.relative(outputDir, srcPath).replace(/\\/g, '/');
|
|
22
|
-
const srcImportPrefix = relativeSrcPath.startsWith('.') ? relativeSrcPath : './' + relativeSrcPath;
|
|
18
|
+
// Use bare package specifier so imports survive version bumps
|
|
19
|
+
const srcImportPrefix = 'dynamo-bao/src';
|
|
23
20
|
if (!modelConfig || !modelConfig.fields) {
|
|
24
21
|
throw new Error(
|
|
25
22
|
`Invalid model configuration for ${modelName}: missing fields`,
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const TABLE_PARAMS = {
|
|
2
|
+
BillingMode: "PAY_PER_REQUEST",
|
|
3
|
+
AttributeDefinitions: [
|
|
4
|
+
{ AttributeName: "_pk", AttributeType: "S" },
|
|
5
|
+
{ AttributeName: "_sk", AttributeType: "S" },
|
|
6
|
+
{ AttributeName: "_gsi1_pk", AttributeType: "S" },
|
|
7
|
+
{ AttributeName: "_gsi1_sk", AttributeType: "S" },
|
|
8
|
+
{ AttributeName: "_gsi2_pk", AttributeType: "S" },
|
|
9
|
+
{ AttributeName: "_gsi2_sk", AttributeType: "S" },
|
|
10
|
+
{ AttributeName: "_gsi3_pk", AttributeType: "S" },
|
|
11
|
+
{ AttributeName: "_gsi3_sk", AttributeType: "S" },
|
|
12
|
+
{ AttributeName: "_gsi4_pk", AttributeType: "S" },
|
|
13
|
+
{ AttributeName: "_gsi4_sk", AttributeType: "S" },
|
|
14
|
+
{ AttributeName: "_gsi5_pk", AttributeType: "S" },
|
|
15
|
+
{ AttributeName: "_gsi5_sk", AttributeType: "S" },
|
|
16
|
+
{ AttributeName: "_iter_pk", AttributeType: "S" },
|
|
17
|
+
{ AttributeName: "_iter_sk", AttributeType: "S" },
|
|
18
|
+
],
|
|
19
|
+
KeySchema: [
|
|
20
|
+
{ AttributeName: "_pk", KeyType: "HASH" },
|
|
21
|
+
{ AttributeName: "_sk", KeyType: "RANGE" },
|
|
22
|
+
],
|
|
23
|
+
GlobalSecondaryIndexes: [
|
|
24
|
+
{
|
|
25
|
+
IndexName: "gsi1",
|
|
26
|
+
KeySchema: [
|
|
27
|
+
{ AttributeName: "_gsi1_pk", KeyType: "HASH" },
|
|
28
|
+
{ AttributeName: "_gsi1_sk", KeyType: "RANGE" },
|
|
29
|
+
],
|
|
30
|
+
Projection: { ProjectionType: "ALL" },
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
IndexName: "gsi2",
|
|
34
|
+
KeySchema: [
|
|
35
|
+
{ AttributeName: "_gsi2_pk", KeyType: "HASH" },
|
|
36
|
+
{ AttributeName: "_gsi2_sk", KeyType: "RANGE" },
|
|
37
|
+
],
|
|
38
|
+
Projection: { ProjectionType: "ALL" },
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
IndexName: "gsi3",
|
|
42
|
+
KeySchema: [
|
|
43
|
+
{ AttributeName: "_gsi3_pk", KeyType: "HASH" },
|
|
44
|
+
{ AttributeName: "_gsi3_sk", KeyType: "RANGE" },
|
|
45
|
+
],
|
|
46
|
+
Projection: { ProjectionType: "ALL" },
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
IndexName: "gsi4",
|
|
50
|
+
KeySchema: [
|
|
51
|
+
{ AttributeName: "_gsi4_pk", KeyType: "HASH" },
|
|
52
|
+
{ AttributeName: "_gsi4_sk", KeyType: "RANGE" },
|
|
53
|
+
],
|
|
54
|
+
Projection: { ProjectionType: "ALL" },
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
IndexName: "gsi5",
|
|
58
|
+
KeySchema: [
|
|
59
|
+
{ AttributeName: "_gsi5_pk", KeyType: "HASH" },
|
|
60
|
+
{ AttributeName: "_gsi5_sk", KeyType: "RANGE" },
|
|
61
|
+
],
|
|
62
|
+
Projection: { ProjectionType: "ALL" },
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
IndexName: "iter_index",
|
|
66
|
+
KeySchema: [
|
|
67
|
+
{ AttributeName: "_iter_pk", KeyType: "HASH" },
|
|
68
|
+
{ AttributeName: "_iter_sk", KeyType: "RANGE" },
|
|
69
|
+
],
|
|
70
|
+
Projection: { ProjectionType: "KEYS_ONLY" },
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
async function waitForTableActive(client, tableName, { maxAttempts = 30, intervalMs = 2000 } = {}) {
|
|
76
|
+
const resolve = require("./resolve");
|
|
77
|
+
const { DescribeTableCommand } = resolve("src/dynamodb-client.js");
|
|
78
|
+
for (let i = 0; i < maxAttempts; i++) {
|
|
79
|
+
const response = await client.send(
|
|
80
|
+
new DescribeTableCommand({ TableName: tableName }),
|
|
81
|
+
);
|
|
82
|
+
const status = response.Table.TableStatus;
|
|
83
|
+
if (status === "ACTIVE") {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
87
|
+
}
|
|
88
|
+
throw new Error(
|
|
89
|
+
`Table ${tableName} did not become active within ${maxAttempts * (intervalMs / 1000)} seconds`,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = { TABLE_PARAMS, waitForTableActive };
|
package/bin/watch.js
CHANGED
|
@@ -5,7 +5,8 @@ const path = require("path");
|
|
|
5
5
|
const chokidar = require("chokidar");
|
|
6
6
|
const { run: runCodegen } = require("./codegen");
|
|
7
7
|
const { createLogger } = require("./utils/scriptLogger");
|
|
8
|
-
const
|
|
8
|
+
const resolve = require("./lib/resolve");
|
|
9
|
+
const config = resolve("src/config");
|
|
9
10
|
|
|
10
11
|
const logger = createLogger("Watch");
|
|
11
12
|
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dynamo-bao",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "Simple powerful data modeling primitives for DynamoDB in JavaScript.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": "./src/index.js",
|
|
8
|
+
"./src/*": "./src/*",
|
|
8
9
|
"./package.json": "./package.json"
|
|
9
10
|
},
|
|
10
11
|
"scripts": {
|
|
@@ -52,10 +53,11 @@
|
|
|
52
53
|
"eslint-plugin-promise": "^6.1.1"
|
|
53
54
|
},
|
|
54
55
|
"bin": {
|
|
55
|
-
"bao-init": "bin/dynamo-bao-create-table.js",
|
|
56
|
+
"bao-init": "./bin/dynamo-bao-create-table.js",
|
|
57
|
+
"bao-create-table": "./bin/bao-create-table.js",
|
|
56
58
|
"bao-codegen": "./bin/codegen.js",
|
|
57
59
|
"bao-watch": "./bin/watch.js",
|
|
58
|
-
"bao-update-table": "bin/dynamo-bao-update-table.js",
|
|
59
|
-
"bao-delete": "bin/dynamo-bao-delete-table.js"
|
|
60
|
+
"bao-update-table": "./bin/dynamo-bao-update-table.js",
|
|
61
|
+
"bao-delete": "./bin/dynamo-bao-delete-table.js"
|
|
60
62
|
}
|
|
61
63
|
}
|