dynamo-bao 0.3.9 → 0.4.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.
- package/README.md +7 -1
- package/bin/bao-create-table.js +47 -0
- package/bin/codegen.js +3 -2
- package/bin/dynamo-bao-create-table.js +23 -63
- package/bin/dynamo-bao-update-table.js +223 -0
- package/bin/generators/model.js +5 -1
- 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 -3
- package/src/constants.js +8 -0
- package/src/dynamodb-client.js +26 -2
- package/src/mixins/mutation-mixin.js +67 -13
- package/src/mixins/validation-mixin.js +4 -0
package/README.md
CHANGED
|
@@ -95,7 +95,7 @@ models:
|
|
|
95
95
|
content: {type: StringField, required: true}
|
|
96
96
|
primaryKey: {partitionKey: postId}
|
|
97
97
|
indexes:
|
|
98
|
-
# Add up to
|
|
98
|
+
# Add up to 5 indexes; make sure each index has a unique id: gsi1, gsi2, gsi3, gsi4, gsi5
|
|
99
99
|
# Enables user.queryPosts() to query posts for a user
|
|
100
100
|
postsForUser: {partitionKey: userId, sortKey: postId, indexId: gsi2}
|
|
101
101
|
```
|
|
@@ -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,10 +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
|
-
|
|
8
|
+
UpdateTimeToLiveCommand,
|
|
9
|
+
} = resolve("src/dynamodb-client.js");
|
|
10
|
+
const { TABLE_PARAMS, waitForTableActive } = require("./lib/create-table-params");
|
|
8
11
|
const readline = require("readline");
|
|
9
12
|
const fs = require("fs");
|
|
10
13
|
const path = require("path");
|
|
@@ -93,65 +96,7 @@ async function createTable() {
|
|
|
93
96
|
(await prompt(`Enter table name [${defaultTableName}]: `)) ||
|
|
94
97
|
defaultTableName;
|
|
95
98
|
|
|
96
|
-
|
|
97
|
-
const params = {
|
|
98
|
-
TableName: tableName,
|
|
99
|
-
BillingMode: "PAY_PER_REQUEST",
|
|
100
|
-
AttributeDefinitions: [
|
|
101
|
-
{ AttributeName: "_pk", AttributeType: "S" },
|
|
102
|
-
{ AttributeName: "_sk", AttributeType: "S" },
|
|
103
|
-
{ AttributeName: "_gsi1_pk", AttributeType: "S" },
|
|
104
|
-
{ AttributeName: "_gsi1_sk", AttributeType: "S" },
|
|
105
|
-
{ AttributeName: "_gsi2_pk", AttributeType: "S" },
|
|
106
|
-
{ AttributeName: "_gsi2_sk", AttributeType: "S" },
|
|
107
|
-
{ AttributeName: "_gsi3_pk", AttributeType: "S" },
|
|
108
|
-
{ AttributeName: "_gsi3_sk", AttributeType: "S" },
|
|
109
|
-
{ AttributeName: "_iter_pk", AttributeType: "S" },
|
|
110
|
-
{ AttributeName: "_iter_sk", AttributeType: "S" },
|
|
111
|
-
],
|
|
112
|
-
KeySchema: [
|
|
113
|
-
{ AttributeName: "_pk", KeyType: "HASH" },
|
|
114
|
-
{ AttributeName: "_sk", KeyType: "RANGE" },
|
|
115
|
-
],
|
|
116
|
-
GlobalSecondaryIndexes: [
|
|
117
|
-
{
|
|
118
|
-
IndexName: "gsi1",
|
|
119
|
-
KeySchema: [
|
|
120
|
-
{ AttributeName: "_gsi1_pk", KeyType: "HASH" },
|
|
121
|
-
{ AttributeName: "_gsi1_sk", KeyType: "RANGE" },
|
|
122
|
-
],
|
|
123
|
-
Projection: { ProjectionType: "ALL" },
|
|
124
|
-
},
|
|
125
|
-
{
|
|
126
|
-
IndexName: "gsi2",
|
|
127
|
-
KeySchema: [
|
|
128
|
-
{ AttributeName: "_gsi2_pk", KeyType: "HASH" },
|
|
129
|
-
{ AttributeName: "_gsi2_sk", KeyType: "RANGE" },
|
|
130
|
-
],
|
|
131
|
-
Projection: { ProjectionType: "ALL" },
|
|
132
|
-
},
|
|
133
|
-
{
|
|
134
|
-
IndexName: "gsi3",
|
|
135
|
-
KeySchema: [
|
|
136
|
-
{ AttributeName: "_gsi3_pk", KeyType: "HASH" },
|
|
137
|
-
{ AttributeName: "_gsi3_sk", KeyType: "RANGE" },
|
|
138
|
-
],
|
|
139
|
-
Projection: { ProjectionType: "ALL" },
|
|
140
|
-
},
|
|
141
|
-
{
|
|
142
|
-
IndexName: "iter_index",
|
|
143
|
-
KeySchema: [
|
|
144
|
-
{ AttributeName: "_iter_pk", KeyType: "HASH" },
|
|
145
|
-
{ AttributeName: "_iter_sk", KeyType: "RANGE" },
|
|
146
|
-
],
|
|
147
|
-
Projection: { ProjectionType: "KEYS_ONLY" },
|
|
148
|
-
},
|
|
149
|
-
],
|
|
150
|
-
TimeToLiveSpecification: {
|
|
151
|
-
AttributeName: "ttl",
|
|
152
|
-
Enabled: true,
|
|
153
|
-
},
|
|
154
|
-
};
|
|
99
|
+
const params = { ...TABLE_PARAMS, TableName: tableName };
|
|
155
100
|
|
|
156
101
|
try {
|
|
157
102
|
// Create table
|
|
@@ -162,7 +107,23 @@ async function createTable() {
|
|
|
162
107
|
response.TableDescription.TableName,
|
|
163
108
|
);
|
|
164
109
|
|
|
165
|
-
//
|
|
110
|
+
// Wait for table to become active before enabling TTL
|
|
111
|
+
console.log("Waiting for table to become active...");
|
|
112
|
+
await waitForTableActive(client, tableName);
|
|
113
|
+
|
|
114
|
+
// Enable TTL (must be done after table creation via UpdateTimeToLive)
|
|
115
|
+
await client.send(
|
|
116
|
+
new UpdateTimeToLiveCommand({
|
|
117
|
+
TableName: tableName,
|
|
118
|
+
TimeToLiveSpecification: {
|
|
119
|
+
AttributeName: "ttl",
|
|
120
|
+
Enabled: true,
|
|
121
|
+
},
|
|
122
|
+
}),
|
|
123
|
+
);
|
|
124
|
+
console.log("TTL enabled on attribute 'ttl'");
|
|
125
|
+
|
|
126
|
+
// Write the config file after table creation
|
|
166
127
|
await writeConfigFile(tableName);
|
|
167
128
|
} catch (error) {
|
|
168
129
|
console.error("Error creating table:", error);
|
|
@@ -184,7 +145,7 @@ async function createDirectoriesAndFiles() {
|
|
|
184
145
|
# Here's a simple example:
|
|
185
146
|
# models:
|
|
186
147
|
# User: {
|
|
187
|
-
#
|
|
148
|
+
#
|
|
188
149
|
# modelPrefix: u
|
|
189
150
|
# fields:
|
|
190
151
|
# userId: {type: UlidField, autoAssign: true, required: true}
|
|
@@ -203,7 +164,6 @@ async function createDirectoriesAndFiles() {
|
|
|
203
164
|
async function main() {
|
|
204
165
|
await checkAwsCredentials();
|
|
205
166
|
await createTable();
|
|
206
|
-
// Add this line after table creation
|
|
207
167
|
await createDirectoriesAndFiles();
|
|
208
168
|
}
|
|
209
169
|
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const resolve = require("./lib/resolve");
|
|
4
|
+
const {
|
|
5
|
+
DynamoDBClient,
|
|
6
|
+
DescribeTableCommand,
|
|
7
|
+
UpdateTableCommand,
|
|
8
|
+
DescribeTimeToLiveCommand,
|
|
9
|
+
UpdateTimeToLiveCommand,
|
|
10
|
+
} = resolve("src/dynamodb-client.js");
|
|
11
|
+
const { initConfig } = resolve("src/config.js");
|
|
12
|
+
|
|
13
|
+
// Expected GSI definitions - the full set that should exist on the table
|
|
14
|
+
const EXPECTED_GSIS = [
|
|
15
|
+
{
|
|
16
|
+
IndexName: "gsi1",
|
|
17
|
+
KeySchema: [
|
|
18
|
+
{ AttributeName: "_gsi1_pk", KeyType: "HASH" },
|
|
19
|
+
{ AttributeName: "_gsi1_sk", KeyType: "RANGE" },
|
|
20
|
+
],
|
|
21
|
+
Projection: { ProjectionType: "ALL" },
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
IndexName: "gsi2",
|
|
25
|
+
KeySchema: [
|
|
26
|
+
{ AttributeName: "_gsi2_pk", KeyType: "HASH" },
|
|
27
|
+
{ AttributeName: "_gsi2_sk", KeyType: "RANGE" },
|
|
28
|
+
],
|
|
29
|
+
Projection: { ProjectionType: "ALL" },
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
IndexName: "gsi3",
|
|
33
|
+
KeySchema: [
|
|
34
|
+
{ AttributeName: "_gsi3_pk", KeyType: "HASH" },
|
|
35
|
+
{ AttributeName: "_gsi3_sk", KeyType: "RANGE" },
|
|
36
|
+
],
|
|
37
|
+
Projection: { ProjectionType: "ALL" },
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
IndexName: "gsi4",
|
|
41
|
+
KeySchema: [
|
|
42
|
+
{ AttributeName: "_gsi4_pk", KeyType: "HASH" },
|
|
43
|
+
{ AttributeName: "_gsi4_sk", KeyType: "RANGE" },
|
|
44
|
+
],
|
|
45
|
+
Projection: { ProjectionType: "ALL" },
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
IndexName: "gsi5",
|
|
49
|
+
KeySchema: [
|
|
50
|
+
{ AttributeName: "_gsi5_pk", KeyType: "HASH" },
|
|
51
|
+
{ AttributeName: "_gsi5_sk", KeyType: "RANGE" },
|
|
52
|
+
],
|
|
53
|
+
Projection: { ProjectionType: "ALL" },
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
IndexName: "iter_index",
|
|
57
|
+
KeySchema: [
|
|
58
|
+
{ AttributeName: "_iter_pk", KeyType: "HASH" },
|
|
59
|
+
{ AttributeName: "_iter_sk", KeyType: "RANGE" },
|
|
60
|
+
],
|
|
61
|
+
Projection: { ProjectionType: "KEYS_ONLY" },
|
|
62
|
+
},
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
// All attribute definitions that GSIs may reference
|
|
66
|
+
const ALL_ATTRIBUTE_DEFINITIONS = [
|
|
67
|
+
{ AttributeName: "_gsi1_pk", AttributeType: "S" },
|
|
68
|
+
{ AttributeName: "_gsi1_sk", AttributeType: "S" },
|
|
69
|
+
{ AttributeName: "_gsi2_pk", AttributeType: "S" },
|
|
70
|
+
{ AttributeName: "_gsi2_sk", AttributeType: "S" },
|
|
71
|
+
{ AttributeName: "_gsi3_pk", AttributeType: "S" },
|
|
72
|
+
{ AttributeName: "_gsi3_sk", AttributeType: "S" },
|
|
73
|
+
{ AttributeName: "_gsi4_pk", AttributeType: "S" },
|
|
74
|
+
{ AttributeName: "_gsi4_sk", AttributeType: "S" },
|
|
75
|
+
{ AttributeName: "_gsi5_pk", AttributeType: "S" },
|
|
76
|
+
{ AttributeName: "_gsi5_sk", AttributeType: "S" },
|
|
77
|
+
{ AttributeName: "_iter_pk", AttributeType: "S" },
|
|
78
|
+
{ AttributeName: "_iter_sk", AttributeType: "S" },
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
const { waitForTableActive } = require("./lib/create-table-params");
|
|
82
|
+
|
|
83
|
+
let client;
|
|
84
|
+
|
|
85
|
+
async function checkAndAddMissingGSIs(tableName) {
|
|
86
|
+
const response = await client.send(
|
|
87
|
+
new DescribeTableCommand({ TableName: tableName }),
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const existingIndexNames = (response.Table.GlobalSecondaryIndexes || []).map(
|
|
91
|
+
(gsi) => gsi.IndexName,
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const missingGSIs = EXPECTED_GSIS.filter(
|
|
95
|
+
(gsi) => !existingIndexNames.includes(gsi.IndexName),
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
if (missingGSIs.length === 0) {
|
|
99
|
+
console.log("All GSIs are present.");
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
console.log(
|
|
104
|
+
`Missing GSIs: ${missingGSIs.map((g) => g.IndexName).join(", ")}`,
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
// DynamoDB only allows adding one GSI per UpdateTable call
|
|
108
|
+
for (const gsi of missingGSIs) {
|
|
109
|
+
console.log(`\nAdding GSI: ${gsi.IndexName}...`);
|
|
110
|
+
|
|
111
|
+
// Collect attribute definitions needed for this GSI
|
|
112
|
+
const neededAttrs = gsi.KeySchema.map((ks) => ks.AttributeName);
|
|
113
|
+
const attrDefs = ALL_ATTRIBUTE_DEFINITIONS.filter((ad) =>
|
|
114
|
+
neededAttrs.includes(ad.AttributeName),
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
await client.send(
|
|
118
|
+
new UpdateTableCommand({
|
|
119
|
+
TableName: tableName,
|
|
120
|
+
AttributeDefinitions: attrDefs,
|
|
121
|
+
GlobalSecondaryIndexUpdates: [
|
|
122
|
+
{
|
|
123
|
+
Create: {
|
|
124
|
+
IndexName: gsi.IndexName,
|
|
125
|
+
KeySchema: gsi.KeySchema,
|
|
126
|
+
Projection: gsi.Projection,
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
}),
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
console.log(
|
|
134
|
+
` GSI ${gsi.IndexName} creation initiated. Waiting for table to become active...`,
|
|
135
|
+
);
|
|
136
|
+
await waitForTableActive(client, tableName, { maxAttempts: 60, intervalMs: 5000 });
|
|
137
|
+
console.log(` GSI ${gsi.IndexName} is ready.`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function checkAndEnableTTL(tableName) {
|
|
142
|
+
const response = await client.send(
|
|
143
|
+
new DescribeTimeToLiveCommand({ TableName: tableName }),
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
const ttlStatus = response.TimeToLiveDescription?.TimeToLiveStatus;
|
|
147
|
+
const ttlAttr = response.TimeToLiveDescription?.AttributeName;
|
|
148
|
+
|
|
149
|
+
if (ttlStatus === "ENABLED" && ttlAttr === "ttl") {
|
|
150
|
+
console.log("TTL is already enabled on attribute 'ttl'.");
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (ttlStatus === "ENABLING") {
|
|
155
|
+
console.log("TTL is currently being enabled. Please wait and try again.");
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (ttlStatus === "DISABLING") {
|
|
160
|
+
console.log(
|
|
161
|
+
"TTL is currently being disabled. Please wait for it to finish, then run this script again.",
|
|
162
|
+
);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
console.log("Enabling TTL on attribute 'ttl'...");
|
|
167
|
+
await client.send(
|
|
168
|
+
new UpdateTimeToLiveCommand({
|
|
169
|
+
TableName: tableName,
|
|
170
|
+
TimeToLiveSpecification: {
|
|
171
|
+
AttributeName: "ttl",
|
|
172
|
+
Enabled: true,
|
|
173
|
+
},
|
|
174
|
+
}),
|
|
175
|
+
);
|
|
176
|
+
console.log("TTL enabled successfully.");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function main() {
|
|
180
|
+
const config = await initConfig();
|
|
181
|
+
const tableName = config.db.tableName;
|
|
182
|
+
|
|
183
|
+
if (!tableName) {
|
|
184
|
+
console.error(
|
|
185
|
+
"No table name found in config. Please run bao-init first or set TABLE_NAME.",
|
|
186
|
+
);
|
|
187
|
+
process.exit(1);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
client = new DynamoDBClient({
|
|
191
|
+
region: config.aws?.region,
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
console.log(`Updating table: ${tableName}\n`);
|
|
195
|
+
|
|
196
|
+
// Verify the table exists
|
|
197
|
+
try {
|
|
198
|
+
await client.send(new DescribeTableCommand({ TableName: tableName }));
|
|
199
|
+
} catch (error) {
|
|
200
|
+
if (error.name === "ResourceNotFoundException") {
|
|
201
|
+
console.error(
|
|
202
|
+
`Table '${tableName}' not found. Please run bao-init to create it first.`,
|
|
203
|
+
);
|
|
204
|
+
process.exit(1);
|
|
205
|
+
}
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Check and add missing GSIs
|
|
210
|
+
console.log("Checking GSIs...");
|
|
211
|
+
await checkAndAddMissingGSIs(tableName);
|
|
212
|
+
|
|
213
|
+
// Check and enable TTL
|
|
214
|
+
console.log("\nChecking TTL...");
|
|
215
|
+
await checkAndEnableTTL(tableName);
|
|
216
|
+
|
|
217
|
+
console.log("\nTable update complete.");
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
main().catch((error) => {
|
|
221
|
+
console.error("Error:", error);
|
|
222
|
+
process.exit(1);
|
|
223
|
+
});
|
package/bin/generators/model.js
CHANGED
|
@@ -100,7 +100,11 @@ function generateModelClass(
|
|
|
100
100
|
// Update logging
|
|
101
101
|
logger.debug(`Generating index ${indexName}:`, indexConfig);
|
|
102
102
|
|
|
103
|
-
const
|
|
103
|
+
const indexNum = indexConfig.indexId.match(/(\d+)$/);
|
|
104
|
+
if (!indexNum) {
|
|
105
|
+
throw new Error(`Invalid indexId format: ${indexConfig.indexId}. Expected format: gsi1, gsi2, etc.`);
|
|
106
|
+
}
|
|
107
|
+
const indexId = `GSI_INDEX_ID${indexNum[1]}`;
|
|
104
108
|
constantImports.add(indexId);
|
|
105
109
|
return ` ${indexName}: IndexConfig('${indexConfig.partitionKey}', '${indexConfig.sortKey}', ${indexId}),`;
|
|
106
110
|
})
|
|
@@ -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.
|
|
3
|
+
"version": "0.4.1",
|
|
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,9 +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-
|
|
60
|
+
"bao-update-table": "./bin/dynamo-bao-update-table.js",
|
|
61
|
+
"bao-delete": "./bin/dynamo-bao-delete-table.js"
|
|
59
62
|
}
|
|
60
63
|
}
|
package/src/constants.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
const GSI_INDEX_ID1 = "gsi1";
|
|
3
3
|
const GSI_INDEX_ID2 = "gsi2";
|
|
4
4
|
const GSI_INDEX_ID3 = "gsi3";
|
|
5
|
+
const GSI_INDEX_ID4 = "gsi4";
|
|
6
|
+
const GSI_INDEX_ID5 = "gsi5";
|
|
5
7
|
|
|
6
8
|
// Constants for iteration index
|
|
7
9
|
const ITERATION_INDEX_NAME = "iter_index";
|
|
@@ -23,6 +25,10 @@ const SYSTEM_FIELDS = [
|
|
|
23
25
|
"_gsi2_sk",
|
|
24
26
|
"_gsi3_pk",
|
|
25
27
|
"_gsi3_sk",
|
|
28
|
+
"_gsi4_pk",
|
|
29
|
+
"_gsi4_sk",
|
|
30
|
+
"_gsi5_pk",
|
|
31
|
+
"_gsi5_sk",
|
|
26
32
|
"_iter_pk",
|
|
27
33
|
"_iter_sk",
|
|
28
34
|
];
|
|
@@ -33,6 +39,8 @@ module.exports = {
|
|
|
33
39
|
GSI_INDEX_ID1,
|
|
34
40
|
GSI_INDEX_ID2,
|
|
35
41
|
GSI_INDEX_ID3,
|
|
42
|
+
GSI_INDEX_ID4,
|
|
43
|
+
GSI_INDEX_ID5,
|
|
36
44
|
ITERATION_INDEX_NAME,
|
|
37
45
|
ITERATION_PK_FIELD,
|
|
38
46
|
ITERATION_SK_FIELD,
|
package/src/dynamodb-client.js
CHANGED
|
@@ -196,7 +196,10 @@ class DynamoDBClient {
|
|
|
196
196
|
'TransactWriteCommand': 'TransactWriteItems',
|
|
197
197
|
'CreateTableCommand': 'CreateTable',
|
|
198
198
|
'ListTablesCommand': 'ListTables',
|
|
199
|
-
'DescribeTableCommand': 'DescribeTable'
|
|
199
|
+
'DescribeTableCommand': 'DescribeTable',
|
|
200
|
+
'UpdateTimeToLiveCommand': 'UpdateTimeToLive',
|
|
201
|
+
'DescribeTimeToLiveCommand': 'DescribeTimeToLive',
|
|
202
|
+
'UpdateTableCommand': 'UpdateTable'
|
|
200
203
|
};
|
|
201
204
|
|
|
202
205
|
return operationMap[commandName] || commandName.replace('Command', '');
|
|
@@ -628,6 +631,24 @@ class DescribeTableCommand {
|
|
|
628
631
|
}
|
|
629
632
|
}
|
|
630
633
|
|
|
634
|
+
class UpdateTimeToLiveCommand {
|
|
635
|
+
constructor(input) {
|
|
636
|
+
this.input = input;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
class DescribeTimeToLiveCommand {
|
|
641
|
+
constructor(input) {
|
|
642
|
+
this.input = input;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
class UpdateTableCommand {
|
|
647
|
+
constructor(input) {
|
|
648
|
+
this.input = input;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
631
652
|
module.exports = {
|
|
632
653
|
DynamoDBClient,
|
|
633
654
|
DynamoDBDocumentClient,
|
|
@@ -640,6 +661,9 @@ module.exports = {
|
|
|
640
661
|
CreateTableCommand,
|
|
641
662
|
ListTablesCommand,
|
|
642
663
|
DescribeTableCommand,
|
|
664
|
+
UpdateTimeToLiveCommand,
|
|
665
|
+
DescribeTimeToLiveCommand,
|
|
666
|
+
UpdateTableCommand,
|
|
643
667
|
marshall,
|
|
644
668
|
unmarshall
|
|
645
|
-
};
|
|
669
|
+
};
|
|
@@ -348,13 +348,46 @@ const MutationMethods = {
|
|
|
348
348
|
}
|
|
349
349
|
}
|
|
350
350
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
351
|
+
// Backfill from currentItem for GSI consistency
|
|
352
|
+
if (!isNew && currentItem) {
|
|
353
|
+
if (forceReindex) {
|
|
354
|
+
// forceReindex: backfill all fields from current item
|
|
355
|
+
Object.keys(this.fields).forEach((fieldName) => {
|
|
356
|
+
const currentValue = currentItem._dyData[fieldName];
|
|
357
|
+
if (
|
|
358
|
+
currentValue !== undefined &&
|
|
359
|
+
indexComputationData[fieldName] === undefined
|
|
360
|
+
) {
|
|
361
|
+
indexComputationData[fieldName] = currentValue;
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
} else {
|
|
365
|
+
// Regular update: backfill GSI counterpart fields so indexes
|
|
366
|
+
// stay consistent when only one field of a key pair is updated
|
|
367
|
+
Object.entries(this.indexes).forEach(([, index]) => {
|
|
368
|
+
const pkField = index.pk;
|
|
369
|
+
const skField = index.sk;
|
|
370
|
+
const pkInUpdate =
|
|
371
|
+
pkField === "modelPrefix" ||
|
|
372
|
+
indexComputationData[pkField] !== undefined;
|
|
373
|
+
const skInUpdate =
|
|
374
|
+
skField === "modelPrefix" ||
|
|
375
|
+
indexComputationData[skField] !== undefined;
|
|
376
|
+
|
|
377
|
+
if (pkInUpdate && !skInUpdate && skField !== "modelPrefix") {
|
|
378
|
+
const currentValue = currentItem._dyData[skField];
|
|
379
|
+
if (currentValue !== undefined) {
|
|
380
|
+
indexComputationData[skField] = currentValue;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if (skInUpdate && !pkInUpdate && pkField !== "modelPrefix") {
|
|
384
|
+
const currentValue = currentItem._dyData[pkField];
|
|
385
|
+
if (currentValue !== undefined) {
|
|
386
|
+
indexComputationData[pkField] = currentValue;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
}
|
|
358
391
|
}
|
|
359
392
|
|
|
360
393
|
if (isNew) {
|
|
@@ -428,12 +461,13 @@ const MutationMethods = {
|
|
|
428
461
|
}
|
|
429
462
|
|
|
430
463
|
// Add GSI keys
|
|
431
|
-
const indexSourceData =
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
: dyUpdatesToSave;
|
|
464
|
+
const indexSourceData = Object.keys(indexComputationData).length
|
|
465
|
+
? indexComputationData
|
|
466
|
+
: dyUpdatesToSave;
|
|
435
467
|
|
|
436
|
-
const indexKeys = this._getIndexKeys(indexSourceData
|
|
468
|
+
const indexKeys = this._getIndexKeys(indexSourceData, {
|
|
469
|
+
isUpdate: !isNew,
|
|
470
|
+
});
|
|
437
471
|
logger.debug("indexKeys", indexKeys);
|
|
438
472
|
Object.assign(dyUpdatesToSave, indexKeys);
|
|
439
473
|
|
|
@@ -711,7 +745,7 @@ const MutationMethods = {
|
|
|
711
745
|
};
|
|
712
746
|
},
|
|
713
747
|
|
|
714
|
-
_getIndexKeys(data) {
|
|
748
|
+
_getIndexKeys(data, { isUpdate = false } = {}) {
|
|
715
749
|
const indexKeys = {};
|
|
716
750
|
|
|
717
751
|
Object.entries(this.indexes).forEach(([indexName, index]) => {
|
|
@@ -739,6 +773,26 @@ const MutationMethods = {
|
|
|
739
773
|
}
|
|
740
774
|
}
|
|
741
775
|
|
|
776
|
+
// Detect partial GSI key updates that would cause index staleness
|
|
777
|
+
if (isUpdate && index.indexId !== undefined) {
|
|
778
|
+
const pkProvided =
|
|
779
|
+
index.pk === "modelPrefix" || pkValue !== undefined;
|
|
780
|
+
const skProvided =
|
|
781
|
+
index.sk === "modelPrefix" || skValue !== undefined;
|
|
782
|
+
if (pkProvided && !skProvided) {
|
|
783
|
+
throw new Error(
|
|
784
|
+
`Update includes GSI partition key "${index.pk}" for index "${indexName}" ` +
|
|
785
|
+
`but is missing sort key "${index.sk}". Include both fields or use forceReindex.`,
|
|
786
|
+
);
|
|
787
|
+
}
|
|
788
|
+
if (skProvided && !pkProvided) {
|
|
789
|
+
throw new Error(
|
|
790
|
+
`Update includes GSI sort key "${index.sk}" for index "${indexName}" ` +
|
|
791
|
+
`but is missing partition key "${index.pk}". Include both fields or use forceReindex.`,
|
|
792
|
+
);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
|
|
742
796
|
if (
|
|
743
797
|
pkValue !== undefined &&
|
|
744
798
|
skValue !== undefined &&
|
|
@@ -6,6 +6,8 @@ const {
|
|
|
6
6
|
GSI_INDEX_ID1,
|
|
7
7
|
GSI_INDEX_ID2,
|
|
8
8
|
GSI_INDEX_ID3,
|
|
9
|
+
GSI_INDEX_ID4,
|
|
10
|
+
GSI_INDEX_ID5,
|
|
9
11
|
UNIQUE_CONSTRAINT_ID1,
|
|
10
12
|
UNIQUE_CONSTRAINT_ID2,
|
|
11
13
|
UNIQUE_CONSTRAINT_ID3,
|
|
@@ -98,6 +100,8 @@ const ValidationMethods = {
|
|
|
98
100
|
GSI_INDEX_ID1,
|
|
99
101
|
GSI_INDEX_ID2,
|
|
100
102
|
GSI_INDEX_ID3,
|
|
103
|
+
GSI_INDEX_ID4,
|
|
104
|
+
GSI_INDEX_ID5,
|
|
101
105
|
undefined,
|
|
102
106
|
]; // undefined for PK-based indexes
|
|
103
107
|
|