dynamo-bao 0.3.9 → 0.4.0
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 +1 -1
- package/bin/dynamo-bao-create-table.js +56 -5
- package/bin/dynamo-bao-update-table.js +237 -0
- package/bin/generators/model.js +5 -1
- package/package.json +2 -1
- 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
|
```
|
|
@@ -4,6 +4,8 @@ const {
|
|
|
4
4
|
DynamoDBClient,
|
|
5
5
|
CreateTableCommand,
|
|
6
6
|
ListTablesCommand,
|
|
7
|
+
DescribeTableCommand,
|
|
8
|
+
UpdateTimeToLiveCommand,
|
|
7
9
|
} = require("../src/dynamodb-client.js");
|
|
8
10
|
const readline = require("readline");
|
|
9
11
|
const fs = require("fs");
|
|
@@ -86,6 +88,23 @@ module.exports = config;
|
|
|
86
88
|
console.log(`Config file written to: ${configPath}`);
|
|
87
89
|
}
|
|
88
90
|
|
|
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
|
+
|
|
89
108
|
async function createTable() {
|
|
90
109
|
// Prompt user for table name
|
|
91
110
|
const defaultTableName = "dynamo-bao-dev";
|
|
@@ -106,6 +125,10 @@ async function createTable() {
|
|
|
106
125
|
{ AttributeName: "_gsi2_sk", AttributeType: "S" },
|
|
107
126
|
{ AttributeName: "_gsi3_pk", AttributeType: "S" },
|
|
108
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" },
|
|
109
132
|
{ AttributeName: "_iter_pk", AttributeType: "S" },
|
|
110
133
|
{ AttributeName: "_iter_sk", AttributeType: "S" },
|
|
111
134
|
],
|
|
@@ -138,6 +161,22 @@ async function createTable() {
|
|
|
138
161
|
],
|
|
139
162
|
Projection: { ProjectionType: "ALL" },
|
|
140
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
|
+
},
|
|
141
180
|
{
|
|
142
181
|
IndexName: "iter_index",
|
|
143
182
|
KeySchema: [
|
|
@@ -147,10 +186,6 @@ async function createTable() {
|
|
|
147
186
|
Projection: { ProjectionType: "KEYS_ONLY" },
|
|
148
187
|
},
|
|
149
188
|
],
|
|
150
|
-
TimeToLiveSpecification: {
|
|
151
|
-
AttributeName: "ttl",
|
|
152
|
-
Enabled: true,
|
|
153
|
-
},
|
|
154
189
|
};
|
|
155
190
|
|
|
156
191
|
try {
|
|
@@ -162,7 +197,23 @@ async function createTable() {
|
|
|
162
197
|
response.TableDescription.TableName,
|
|
163
198
|
);
|
|
164
199
|
|
|
165
|
-
//
|
|
200
|
+
// Wait for table to become active before enabling TTL
|
|
201
|
+
console.log("Waiting for table to become active...");
|
|
202
|
+
await waitForTableActive(tableName);
|
|
203
|
+
|
|
204
|
+
// Enable TTL (must be done after table creation via UpdateTimeToLive)
|
|
205
|
+
await client.send(
|
|
206
|
+
new UpdateTimeToLiveCommand({
|
|
207
|
+
TableName: tableName,
|
|
208
|
+
TimeToLiveSpecification: {
|
|
209
|
+
AttributeName: "ttl",
|
|
210
|
+
Enabled: true,
|
|
211
|
+
},
|
|
212
|
+
}),
|
|
213
|
+
);
|
|
214
|
+
console.log("TTL enabled on attribute 'ttl'");
|
|
215
|
+
|
|
216
|
+
// Write the config file after table creation
|
|
166
217
|
await writeConfigFile(tableName);
|
|
167
218
|
} catch (error) {
|
|
168
219
|
console.error("Error creating table:", error);
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
DynamoDBClient,
|
|
5
|
+
DescribeTableCommand,
|
|
6
|
+
UpdateTableCommand,
|
|
7
|
+
DescribeTimeToLiveCommand,
|
|
8
|
+
UpdateTimeToLiveCommand,
|
|
9
|
+
} = require("../src/dynamodb-client.js");
|
|
10
|
+
const { initConfig } = require("../src/config.js");
|
|
11
|
+
|
|
12
|
+
// Expected GSI definitions - the full set that should exist on the table
|
|
13
|
+
const EXPECTED_GSIS = [
|
|
14
|
+
{
|
|
15
|
+
IndexName: "gsi1",
|
|
16
|
+
KeySchema: [
|
|
17
|
+
{ AttributeName: "_gsi1_pk", KeyType: "HASH" },
|
|
18
|
+
{ AttributeName: "_gsi1_sk", KeyType: "RANGE" },
|
|
19
|
+
],
|
|
20
|
+
Projection: { ProjectionType: "ALL" },
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
IndexName: "gsi2",
|
|
24
|
+
KeySchema: [
|
|
25
|
+
{ AttributeName: "_gsi2_pk", KeyType: "HASH" },
|
|
26
|
+
{ AttributeName: "_gsi2_sk", KeyType: "RANGE" },
|
|
27
|
+
],
|
|
28
|
+
Projection: { ProjectionType: "ALL" },
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
IndexName: "gsi3",
|
|
32
|
+
KeySchema: [
|
|
33
|
+
{ AttributeName: "_gsi3_pk", KeyType: "HASH" },
|
|
34
|
+
{ AttributeName: "_gsi3_sk", KeyType: "RANGE" },
|
|
35
|
+
],
|
|
36
|
+
Projection: { ProjectionType: "ALL" },
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
IndexName: "gsi4",
|
|
40
|
+
KeySchema: [
|
|
41
|
+
{ AttributeName: "_gsi4_pk", KeyType: "HASH" },
|
|
42
|
+
{ AttributeName: "_gsi4_sk", KeyType: "RANGE" },
|
|
43
|
+
],
|
|
44
|
+
Projection: { ProjectionType: "ALL" },
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
IndexName: "gsi5",
|
|
48
|
+
KeySchema: [
|
|
49
|
+
{ AttributeName: "_gsi5_pk", KeyType: "HASH" },
|
|
50
|
+
{ AttributeName: "_gsi5_sk", KeyType: "RANGE" },
|
|
51
|
+
],
|
|
52
|
+
Projection: { ProjectionType: "ALL" },
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
IndexName: "iter_index",
|
|
56
|
+
KeySchema: [
|
|
57
|
+
{ AttributeName: "_iter_pk", KeyType: "HASH" },
|
|
58
|
+
{ AttributeName: "_iter_sk", KeyType: "RANGE" },
|
|
59
|
+
],
|
|
60
|
+
Projection: { ProjectionType: "KEYS_ONLY" },
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
// All attribute definitions that GSIs may reference
|
|
65
|
+
const ALL_ATTRIBUTE_DEFINITIONS = [
|
|
66
|
+
{ AttributeName: "_gsi1_pk", AttributeType: "S" },
|
|
67
|
+
{ AttributeName: "_gsi1_sk", AttributeType: "S" },
|
|
68
|
+
{ AttributeName: "_gsi2_pk", AttributeType: "S" },
|
|
69
|
+
{ AttributeName: "_gsi2_sk", AttributeType: "S" },
|
|
70
|
+
{ AttributeName: "_gsi3_pk", AttributeType: "S" },
|
|
71
|
+
{ AttributeName: "_gsi3_sk", AttributeType: "S" },
|
|
72
|
+
{ AttributeName: "_gsi4_pk", AttributeType: "S" },
|
|
73
|
+
{ AttributeName: "_gsi4_sk", AttributeType: "S" },
|
|
74
|
+
{ AttributeName: "_gsi5_pk", AttributeType: "S" },
|
|
75
|
+
{ AttributeName: "_gsi5_sk", AttributeType: "S" },
|
|
76
|
+
{ AttributeName: "_iter_pk", AttributeType: "S" },
|
|
77
|
+
{ AttributeName: "_iter_sk", AttributeType: "S" },
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
let client;
|
|
81
|
+
|
|
82
|
+
async function waitForTableActive(tableName, maxAttempts = 60) {
|
|
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
|
+
}
|
|
98
|
+
|
|
99
|
+
async function checkAndAddMissingGSIs(tableName) {
|
|
100
|
+
const response = await client.send(
|
|
101
|
+
new DescribeTableCommand({ TableName: tableName }),
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
const existingIndexNames = (response.Table.GlobalSecondaryIndexes || []).map(
|
|
105
|
+
(gsi) => gsi.IndexName,
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const missingGSIs = EXPECTED_GSIS.filter(
|
|
109
|
+
(gsi) => !existingIndexNames.includes(gsi.IndexName),
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
if (missingGSIs.length === 0) {
|
|
113
|
+
console.log("All GSIs are present.");
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
console.log(
|
|
118
|
+
`Missing GSIs: ${missingGSIs.map((g) => g.IndexName).join(", ")}`,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
// DynamoDB only allows adding one GSI per UpdateTable call
|
|
122
|
+
for (const gsi of missingGSIs) {
|
|
123
|
+
console.log(`\nAdding GSI: ${gsi.IndexName}...`);
|
|
124
|
+
|
|
125
|
+
// Collect attribute definitions needed for this GSI
|
|
126
|
+
const neededAttrs = gsi.KeySchema.map((ks) => ks.AttributeName);
|
|
127
|
+
const attrDefs = ALL_ATTRIBUTE_DEFINITIONS.filter((ad) =>
|
|
128
|
+
neededAttrs.includes(ad.AttributeName),
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
await client.send(
|
|
132
|
+
new UpdateTableCommand({
|
|
133
|
+
TableName: tableName,
|
|
134
|
+
AttributeDefinitions: attrDefs,
|
|
135
|
+
GlobalSecondaryIndexUpdates: [
|
|
136
|
+
{
|
|
137
|
+
Create: {
|
|
138
|
+
IndexName: gsi.IndexName,
|
|
139
|
+
KeySchema: gsi.KeySchema,
|
|
140
|
+
Projection: gsi.Projection,
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
],
|
|
144
|
+
}),
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
console.log(
|
|
148
|
+
` GSI ${gsi.IndexName} creation initiated. Waiting for table to become active...`,
|
|
149
|
+
);
|
|
150
|
+
await waitForTableActive(tableName);
|
|
151
|
+
console.log(` GSI ${gsi.IndexName} is ready.`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async function checkAndEnableTTL(tableName) {
|
|
156
|
+
const response = await client.send(
|
|
157
|
+
new DescribeTimeToLiveCommand({ TableName: tableName }),
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
const ttlStatus = response.TimeToLiveDescription?.TimeToLiveStatus;
|
|
161
|
+
const ttlAttr = response.TimeToLiveDescription?.AttributeName;
|
|
162
|
+
|
|
163
|
+
if (ttlStatus === "ENABLED" && ttlAttr === "ttl") {
|
|
164
|
+
console.log("TTL is already enabled on attribute 'ttl'.");
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (ttlStatus === "ENABLING") {
|
|
169
|
+
console.log("TTL is currently being enabled. Please wait and try again.");
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (ttlStatus === "DISABLING") {
|
|
174
|
+
console.log(
|
|
175
|
+
"TTL is currently being disabled. Please wait for it to finish, then run this script again.",
|
|
176
|
+
);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
console.log("Enabling TTL on attribute 'ttl'...");
|
|
181
|
+
await client.send(
|
|
182
|
+
new UpdateTimeToLiveCommand({
|
|
183
|
+
TableName: tableName,
|
|
184
|
+
TimeToLiveSpecification: {
|
|
185
|
+
AttributeName: "ttl",
|
|
186
|
+
Enabled: true,
|
|
187
|
+
},
|
|
188
|
+
}),
|
|
189
|
+
);
|
|
190
|
+
console.log("TTL enabled successfully.");
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async function main() {
|
|
194
|
+
const config = await initConfig();
|
|
195
|
+
const tableName = config.db.tableName;
|
|
196
|
+
|
|
197
|
+
if (!tableName) {
|
|
198
|
+
console.error(
|
|
199
|
+
"No table name found in config. Please run bao-init first or set TABLE_NAME.",
|
|
200
|
+
);
|
|
201
|
+
process.exit(1);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
client = new DynamoDBClient({
|
|
205
|
+
region: config.aws?.region,
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
console.log(`Updating table: ${tableName}\n`);
|
|
209
|
+
|
|
210
|
+
// Verify the table exists
|
|
211
|
+
try {
|
|
212
|
+
await client.send(new DescribeTableCommand({ TableName: tableName }));
|
|
213
|
+
} catch (error) {
|
|
214
|
+
if (error.name === "ResourceNotFoundException") {
|
|
215
|
+
console.error(
|
|
216
|
+
`Table '${tableName}' not found. Please run bao-init to create it first.`,
|
|
217
|
+
);
|
|
218
|
+
process.exit(1);
|
|
219
|
+
}
|
|
220
|
+
throw error;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Check and add missing GSIs
|
|
224
|
+
console.log("Checking GSIs...");
|
|
225
|
+
await checkAndAddMissingGSIs(tableName);
|
|
226
|
+
|
|
227
|
+
// Check and enable TTL
|
|
228
|
+
console.log("\nChecking TTL...");
|
|
229
|
+
await checkAndEnableTTL(tableName);
|
|
230
|
+
|
|
231
|
+
console.log("\nTable update complete.");
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
main().catch((error) => {
|
|
235
|
+
console.error("Error:", error);
|
|
236
|
+
process.exit(1);
|
|
237
|
+
});
|
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
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dynamo-bao",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Simple powerful data modeling primitives for DynamoDB in JavaScript.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -55,6 +55,7 @@
|
|
|
55
55
|
"bao-init": "bin/dynamo-bao-create-table.js",
|
|
56
56
|
"bao-codegen": "./bin/codegen.js",
|
|
57
57
|
"bao-watch": "./bin/watch.js",
|
|
58
|
+
"bao-update-table": "bin/dynamo-bao-update-table.js",
|
|
58
59
|
"bao-delete": "bin/dynamo-bao-delete-table.js"
|
|
59
60
|
}
|
|
60
61
|
}
|
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
|
|