@aws/nx-plugin 1.0.0-rc.3 → 1.0.0-rc.5
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-THIRD-PARTY +1437 -41
- package/executors.json +9 -0
- package/generators.json +34 -0
- package/package.json +5 -1
- package/sdk/license.d.ts +8 -0
- package/sdk/license.js +13 -0
- package/sdk/license.js.map +1 -0
- package/src/connection/generator.d.ts +1 -1
- package/src/connection/generator.js +19 -0
- package/src/connection/generator.js.map +1 -1
- package/src/license/__snapshots__/config.spec.ts.snap +23 -0
- package/src/license/config-types.d.ts +17 -0
- package/src/license/config.d.ts +63 -5
- package/src/license/config.js +315 -7
- package/src/license/config.js.map +1 -1
- package/src/license/dependency-check/check.d.ts +12 -0
- package/src/license/dependency-check/check.js +111 -0
- package/src/license/dependency-check/check.js.map +1 -0
- package/src/license/dependency-check/collectors/collector.d.ts +21 -0
- package/src/license/dependency-check/collectors/collector.js +76 -0
- package/src/license/dependency-check/collectors/collector.js.map +1 -0
- package/src/license/dependency-check/collectors/npm-collector.d.ts +10 -0
- package/src/license/dependency-check/collectors/npm-collector.js +75 -0
- package/src/license/dependency-check/collectors/npm-collector.js.map +1 -0
- package/src/license/dependency-check/collectors/python-collector.d.ts +19 -0
- package/src/license/dependency-check/collectors/python-collector.js +121 -0
- package/src/license/dependency-check/collectors/python-collector.js.map +1 -0
- package/src/license/dependency-check/evaluator.d.ts +9 -0
- package/src/license/dependency-check/evaluator.js +122 -0
- package/src/license/dependency-check/evaluator.js.map +1 -0
- package/src/license/dependency-check/executor-schema.json +6 -0
- package/src/license/dependency-check/executor.d.ts +9 -0
- package/src/license/dependency-check/executor.js +43 -0
- package/src/license/dependency-check/executor.js.map +1 -0
- package/src/license/dependency-check/pre-approved.d.ts +6 -0
- package/src/license/dependency-check/pre-approved.js +1280 -0
- package/src/license/dependency-check/pre-approved.js.map +1 -0
- package/src/license/dependency-check/types.d.ts +45 -0
- package/src/license/dependency-check/types.js +7 -0
- package/src/license/dependency-check/types.js.map +1 -0
- package/src/license/generator.js +110 -11
- package/src/license/generator.js.map +1 -1
- package/src/license/index.d.ts +8 -0
- package/src/license/index.js +13 -0
- package/src/license/index.js.map +1 -0
- package/src/license/known-exceptions.d.ts +7 -0
- package/src/license/known-exceptions.js +33 -0
- package/src/license/known-exceptions.js.map +1 -0
- package/src/license/schema.d.ts +5 -0
- package/src/license/schema.json +6 -0
- package/src/license/sync/generator.js +10 -10
- package/src/license/sync/generator.js.map +1 -1
- package/src/license/sync/project-file-sync.d.ts +2 -2
- package/src/license/sync/project-file-sync.js.map +1 -1
- package/src/preset/__snapshots__/generator.spec.ts.snap +70 -0
- package/src/preset/generator.d.ts +1 -1
- package/src/preset/generator.js +5 -1
- package/src/preset/generator.js.map +1 -1
- package/src/preset/schema.d.ts +1 -0
- package/src/preset/schema.json +5 -0
- package/src/py/agent/generator.js +5 -0
- package/src/py/agent/generator.js.map +1 -1
- package/src/py/mcp-server/generator.js +3 -0
- package/src/py/mcp-server/generator.js.map +1 -1
- package/src/py/project/generator.js +7 -0
- package/src/py/project/generator.js.map +1 -1
- package/src/ts/dynamodb/__snapshots__/generator.spec.ts.snap +1502 -0
- package/src/ts/dynamodb/agent-connection/__snapshots__/generator.spec.ts.snap +45 -0
- package/src/ts/dynamodb/agent-connection/generator.d.ts +10 -0
- package/src/ts/dynamodb/agent-connection/generator.js +32 -0
- package/src/ts/dynamodb/agent-connection/generator.js.map +1 -0
- package/src/ts/dynamodb/agent-connection/schema.d.ts +14 -0
- package/src/ts/dynamodb/agent-connection/schema.json +22 -0
- package/src/ts/dynamodb/files/scripts/create-local-table.ts.template +161 -0
- package/src/ts/{rdb/files/scripts/docker-pull.ts.template → dynamodb/files/scripts/pull-image.ts.template} +1 -1
- package/src/ts/dynamodb/files/scripts/start-container.ts.template +81 -0
- package/src/ts/dynamodb/files/src/client.ts.template +45 -0
- package/src/ts/dynamodb/files/src/constants.ts.template +9 -0
- package/src/ts/dynamodb/files/src/entities/example.ts.template +47 -0
- package/src/ts/dynamodb/files/src/entities/index.ts.template +1 -0
- package/src/ts/dynamodb/files/src/gsi.ts.template +14 -0
- package/src/ts/dynamodb/files/src/index.ts.template +11 -0
- package/src/ts/dynamodb/generator.d.ts +10 -0
- package/src/ts/dynamodb/generator.js +108 -0
- package/src/ts/dynamodb/generator.js.map +1 -0
- package/src/ts/dynamodb/mcp-server-connection/__snapshots__/generator.spec.ts.snap +45 -0
- package/src/ts/dynamodb/mcp-server-connection/generator.d.ts +10 -0
- package/src/ts/dynamodb/mcp-server-connection/generator.js +32 -0
- package/src/ts/dynamodb/mcp-server-connection/generator.js.map +1 -0
- package/src/ts/dynamodb/mcp-server-connection/schema.d.ts +14 -0
- package/src/ts/dynamodb/mcp-server-connection/schema.json +22 -0
- package/src/ts/dynamodb/schema.d.ts +14 -0
- package/src/ts/dynamodb/schema.json +52 -0
- package/src/ts/dynamodb/smithy-connection/__snapshots__/generator.spec.ts.snap +23 -0
- package/src/ts/dynamodb/smithy-connection/generator.d.ts +10 -0
- package/src/ts/dynamodb/smithy-connection/generator.js +30 -0
- package/src/ts/dynamodb/smithy-connection/generator.js.map +1 -0
- package/src/ts/dynamodb/smithy-connection/schema.d.ts +12 -0
- package/src/ts/dynamodb/smithy-connection/schema.json +18 -0
- package/src/ts/dynamodb/trpc-connection/__snapshots__/generator.spec.ts.snap +23 -0
- package/src/ts/dynamodb/trpc-connection/generator.d.ts +10 -0
- package/src/ts/dynamodb/trpc-connection/generator.js +30 -0
- package/src/ts/dynamodb/trpc-connection/generator.js.map +1 -0
- package/src/ts/dynamodb/trpc-connection/schema.d.ts +12 -0
- package/src/ts/dynamodb/trpc-connection/schema.json +18 -0
- package/src/ts/lib/ts-project-utils.js +6 -0
- package/src/ts/lib/ts-project-utils.js.map +1 -1
- package/src/ts/mcp-server/generator.js +3 -0
- package/src/ts/mcp-server/generator.js.map +1 -1
- package/src/ts/rdb/__snapshots__/generator.spec.ts.snap +22 -38
- package/src/ts/rdb/files/scripts/pull-image.ts.template +15 -0
- package/src/ts/rdb/files/scripts/{docker-start.ts.template → start-container.ts.template} +23 -23
- package/src/ts/rdb/generator.js +8 -8
- package/src/ts/rdb/generator.js.map +1 -1
- package/src/utils/ast.d.ts +28 -0
- package/src/utils/ast.js +59 -1
- package/src/utils/ast.js.map +1 -1
- package/src/utils/config/utils.d.ts +11 -1
- package/src/utils/config/utils.js +37 -5
- package/src/utils/config/utils.js.map +1 -1
- package/src/utils/dynamodb-constructs/dynamodb-constructs.d.ts +18 -0
- package/src/utils/dynamodb-constructs/dynamodb-constructs.js +57 -0
- package/src/utils/dynamodb-constructs/dynamodb-constructs.js.map +1 -0
- package/src/utils/dynamodb-constructs/files/cdk/app/dynamodb/__nameKebabCase__.ts.template +30 -0
- package/src/utils/dynamodb-constructs/files/cdk/core/dynamodb.ts.template +105 -0
- package/src/utils/dynamodb-constructs/files/terraform/app/dynamodb/__nameKebabCase__/__nameKebabCase__.tf.template +65 -0
- package/src/utils/dynamodb-constructs/files/terraform/core/dynamodb/dynamodb.tf.template +155 -0
- package/src/utils/mcp.d.ts +25 -0
- package/src/utils/mcp.js +81 -0
- package/src/utils/mcp.js.map +1 -0
- package/src/utils/port.d.ts +6 -0
- package/src/utils/port.js +23 -2
- package/src/utils/port.js.map +1 -1
- package/src/utils/rdb-constructs/files/terraform/app/dbs/__nameKebabCase__/__nameKebabCase__.tf.template +4 -4
- package/src/utils/rdb-constructs/rdb-constructs.d.ts +1 -1
- package/src/utils/versions.d.ts +2 -1
- package/src/utils/versions.js +1 -0
- package/src/utils/versions.js.map +1 -1
|
@@ -0,0 +1,1502 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`ts#dynamodb generator > should generate scripts for finch engine > packages/my-table/scripts/create-local-table.ts 1`] = `
|
|
4
|
+
"import {
|
|
5
|
+
CreateTableCommand,
|
|
6
|
+
DescribeTableCommand,
|
|
7
|
+
DynamoDBClient,
|
|
8
|
+
GlobalSecondaryIndexDescription,
|
|
9
|
+
ResourceInUseException,
|
|
10
|
+
UpdateTableCommand,
|
|
11
|
+
} from '@aws-sdk/client-dynamodb';
|
|
12
|
+
import { GLOBAL_SECONDARY_INDEXES } from '../src/gsi.js';
|
|
13
|
+
|
|
14
|
+
const [tableName, port] = process.argv.slice(2);
|
|
15
|
+
const endpoint = \`http://localhost:\${port}\`;
|
|
16
|
+
|
|
17
|
+
const REQUEST_TIMEOUT_MS = 10_000;
|
|
18
|
+
const timeoutSignal = () => AbortSignal.timeout(REQUEST_TIMEOUT_MS);
|
|
19
|
+
|
|
20
|
+
const client = new DynamoDBClient({
|
|
21
|
+
endpoint,
|
|
22
|
+
region: 'us-east-1',
|
|
23
|
+
credentials: {
|
|
24
|
+
accessKeyId: 'accessKeyId',
|
|
25
|
+
secretAccessKey: 'secretAccessKey',
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const waitForTableActive = async (
|
|
30
|
+
waitClient: DynamoDBClient,
|
|
31
|
+
waitTableName: string,
|
|
32
|
+
): Promise<void> => {
|
|
33
|
+
for (;;) {
|
|
34
|
+
const { Table } = await waitClient.send(
|
|
35
|
+
new DescribeTableCommand({ TableName: waitTableName }),
|
|
36
|
+
{ abortSignal: timeoutSignal() },
|
|
37
|
+
);
|
|
38
|
+
const allActive =
|
|
39
|
+
Table?.TableStatus === 'ACTIVE' &&
|
|
40
|
+
(Table?.GlobalSecondaryIndexes ?? []).every(
|
|
41
|
+
(g: GlobalSecondaryIndexDescription) => g.IndexStatus === 'ACTIVE',
|
|
42
|
+
);
|
|
43
|
+
if (allActive) return;
|
|
44
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const ensureTable = async (
|
|
49
|
+
tblClient: DynamoDBClient,
|
|
50
|
+
tblName: string,
|
|
51
|
+
gsIs: { indexName: string; partitionKey: string; sortKey: string }[],
|
|
52
|
+
): Promise<void> => {
|
|
53
|
+
const gsiAttributeDefinitions = gsIs.flatMap((gsi) => [
|
|
54
|
+
{ AttributeName: gsi.partitionKey, AttributeType: 'S' as const },
|
|
55
|
+
{ AttributeName: gsi.sortKey, AttributeType: 'S' as const },
|
|
56
|
+
]);
|
|
57
|
+
const attributeDefinitions = [
|
|
58
|
+
{ AttributeName: 'pk', AttributeType: 'S' as const },
|
|
59
|
+
{ AttributeName: 'sk', AttributeType: 'S' as const },
|
|
60
|
+
...gsiAttributeDefinitions.filter(
|
|
61
|
+
(a, i, arr) =>
|
|
62
|
+
arr.findIndex((b) => b.AttributeName === a.AttributeName) === i,
|
|
63
|
+
),
|
|
64
|
+
];
|
|
65
|
+
const globalSecondaryIndexes = gsIs.map((gsi) => ({
|
|
66
|
+
IndexName: gsi.indexName,
|
|
67
|
+
KeySchema: [
|
|
68
|
+
{ AttributeName: gsi.partitionKey, KeyType: 'HASH' as const },
|
|
69
|
+
{ AttributeName: gsi.sortKey, KeyType: 'RANGE' as const },
|
|
70
|
+
],
|
|
71
|
+
Projection: { ProjectionType: 'ALL' as const },
|
|
72
|
+
}));
|
|
73
|
+
|
|
74
|
+
for (let attempt = 0; attempt < 30; attempt++) {
|
|
75
|
+
try {
|
|
76
|
+
// New table: create with all GSIs in one shot.
|
|
77
|
+
await tblClient.send(
|
|
78
|
+
new CreateTableCommand({
|
|
79
|
+
TableName: tblName,
|
|
80
|
+
AttributeDefinitions: attributeDefinitions,
|
|
81
|
+
KeySchema: [
|
|
82
|
+
{ AttributeName: 'pk', KeyType: 'HASH' },
|
|
83
|
+
{ AttributeName: 'sk', KeyType: 'RANGE' },
|
|
84
|
+
],
|
|
85
|
+
BillingMode: 'PAY_PER_REQUEST',
|
|
86
|
+
...(globalSecondaryIndexes.length > 0 && {
|
|
87
|
+
GlobalSecondaryIndexes: globalSecondaryIndexes,
|
|
88
|
+
}),
|
|
89
|
+
}),
|
|
90
|
+
{ abortSignal: timeoutSignal() },
|
|
91
|
+
);
|
|
92
|
+
console.log(\`Created table: \${tblName}\`);
|
|
93
|
+
for (const gsi of globalSecondaryIndexes) {
|
|
94
|
+
console.log(\`Created GSI: \${gsi.IndexName}\`);
|
|
95
|
+
}
|
|
96
|
+
return;
|
|
97
|
+
} catch (e) {
|
|
98
|
+
// Table already exists: sync GSIs to match desired state.
|
|
99
|
+
if (e instanceof ResourceInUseException) {
|
|
100
|
+
const { Table } = await tblClient.send(
|
|
101
|
+
new DescribeTableCommand({ TableName: tblName }),
|
|
102
|
+
{ abortSignal: timeoutSignal() },
|
|
103
|
+
);
|
|
104
|
+
const existingGSIs = Table?.GlobalSecondaryIndexes ?? [];
|
|
105
|
+
const existingNames = new Set(
|
|
106
|
+
existingGSIs.map(
|
|
107
|
+
(g: GlobalSecondaryIndexDescription) => g.IndexName ?? '',
|
|
108
|
+
),
|
|
109
|
+
);
|
|
110
|
+
const desiredNames = new Set(gsIs.map((g) => g.indexName));
|
|
111
|
+
|
|
112
|
+
// DynamoDB Local only allows one GSI to be created or deleted at a time.
|
|
113
|
+
for (const existing of existingGSIs) {
|
|
114
|
+
if (!existing.IndexName || desiredNames.has(existing.IndexName))
|
|
115
|
+
continue;
|
|
116
|
+
await tblClient.send(
|
|
117
|
+
new UpdateTableCommand({
|
|
118
|
+
TableName: tblName,
|
|
119
|
+
GlobalSecondaryIndexUpdates: [
|
|
120
|
+
{ Delete: { IndexName: existing.IndexName } },
|
|
121
|
+
],
|
|
122
|
+
}),
|
|
123
|
+
{ abortSignal: timeoutSignal() },
|
|
124
|
+
);
|
|
125
|
+
console.log(\`Deleted GSI: \${existing.IndexName}\`);
|
|
126
|
+
await waitForTableActive(tblClient, tblName);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
for (const gsi of gsIs) {
|
|
130
|
+
if (existingNames.has(gsi.indexName)) continue;
|
|
131
|
+
await tblClient.send(
|
|
132
|
+
new UpdateTableCommand({
|
|
133
|
+
TableName: tblName,
|
|
134
|
+
AttributeDefinitions: [
|
|
135
|
+
{ AttributeName: gsi.partitionKey, AttributeType: 'S' },
|
|
136
|
+
{ AttributeName: gsi.sortKey, AttributeType: 'S' },
|
|
137
|
+
],
|
|
138
|
+
GlobalSecondaryIndexUpdates: [
|
|
139
|
+
{
|
|
140
|
+
Create: {
|
|
141
|
+
IndexName: gsi.indexName,
|
|
142
|
+
KeySchema: [
|
|
143
|
+
{ AttributeName: gsi.partitionKey, KeyType: 'HASH' },
|
|
144
|
+
{ AttributeName: gsi.sortKey, KeyType: 'RANGE' },
|
|
145
|
+
],
|
|
146
|
+
Projection: { ProjectionType: 'ALL' },
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
}),
|
|
151
|
+
{ abortSignal: timeoutSignal() },
|
|
152
|
+
);
|
|
153
|
+
console.log(\`Created GSI: \${gsi.indexName}\`);
|
|
154
|
+
await waitForTableActive(tblClient, tblName);
|
|
155
|
+
}
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (attempt === 29) throw e;
|
|
159
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
await ensureTable(client, tableName, GLOBAL_SECONDARY_INDEXES);
|
|
165
|
+
"
|
|
166
|
+
`;
|
|
167
|
+
|
|
168
|
+
exports[`ts#dynamodb generator > should generate scripts for finch engine > packages/my-table/scripts/pull-image.ts 1`] = `
|
|
169
|
+
"import { spawnSync } from 'child_process';
|
|
170
|
+
|
|
171
|
+
const ENGINE = 'finch';
|
|
172
|
+
const image = process.argv[2];
|
|
173
|
+
|
|
174
|
+
const inspect = spawnSync(ENGINE, ['image', 'inspect', image], {
|
|
175
|
+
stdio: 'ignore',
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
if (inspect.status !== 0) {
|
|
179
|
+
const pull = spawnSync(ENGINE, ['pull', image], { stdio: 'inherit' });
|
|
180
|
+
if (pull.status !== 0) {
|
|
181
|
+
process.exit(pull.status ?? 1);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
"
|
|
185
|
+
`;
|
|
186
|
+
|
|
187
|
+
exports[`ts#dynamodb generator > should generate scripts for finch engine > packages/my-table/scripts/start-container.ts 1`] = `
|
|
188
|
+
"import { spawn, spawnSync } from 'child_process';
|
|
189
|
+
|
|
190
|
+
const ENGINE = 'finch';
|
|
191
|
+
|
|
192
|
+
const [containerName, image, hostPort] = process.argv.slice(2);
|
|
193
|
+
|
|
194
|
+
const containerExists = (): boolean =>
|
|
195
|
+
spawnSync(ENGINE, ['container', 'inspect', containerName], {
|
|
196
|
+
stdio: 'ignore',
|
|
197
|
+
}).status === 0;
|
|
198
|
+
|
|
199
|
+
const isRunning = (): boolean => {
|
|
200
|
+
const result = spawnSync(
|
|
201
|
+
ENGINE,
|
|
202
|
+
['container', 'inspect', '-f', '{{.State.Running}}', containerName],
|
|
203
|
+
{ encoding: 'utf-8' },
|
|
204
|
+
);
|
|
205
|
+
return result.status === 0 && result.stdout.trim() === 'true';
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// Multiple dynamo table packages run serve-local in parallel sharing the same
|
|
209
|
+
// container. All processes stream logs, but only the one that created the
|
|
210
|
+
// container stops it on exit — others just detach.
|
|
211
|
+
let created = false;
|
|
212
|
+
let runRetries = 0;
|
|
213
|
+
|
|
214
|
+
for (;;) {
|
|
215
|
+
if (containerExists()) {
|
|
216
|
+
if (!isRunning()) {
|
|
217
|
+
// Another process may have started it between our inspect and start — container engine start
|
|
218
|
+
// exits 0 even if the container is already running, so no special-casing needed.
|
|
219
|
+
const start = spawnSync(ENGINE, ['start', containerName], {
|
|
220
|
+
stdio: 'inherit',
|
|
221
|
+
});
|
|
222
|
+
if (start.status !== 0) process.exit(start.status ?? 1);
|
|
223
|
+
}
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
const runArgs = (
|
|
227
|
+
\`run --name \${containerName} -u root -w /home/dynamodblocal\` +
|
|
228
|
+
\` -p \${hostPort}:\${hostPort} -v \${containerName}-data:/home/dynamodblocal/data\` +
|
|
229
|
+
\` -d \${image} -jar DynamoDBLocal.jar -sharedDb -dbPath ./data -port \${hostPort} -optimizeDbBeforeStartup\`
|
|
230
|
+
).split(' ');
|
|
231
|
+
const create = spawnSync(ENGINE, runArgs, { stdio: 'pipe' });
|
|
232
|
+
if (create.status === 0) {
|
|
233
|
+
created = true;
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
if (!containerExists() && ++runRetries > 3) {
|
|
237
|
+
process.stderr.write(create.stderr);
|
|
238
|
+
process.exit(create.status ?? 1);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const logs = spawn(ENGINE, ['logs', '-f', containerName], {
|
|
243
|
+
stdio: ['ignore', 'inherit', 'inherit'],
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
let exiting = false;
|
|
247
|
+
|
|
248
|
+
const cleanup = () => {
|
|
249
|
+
if (exiting) return;
|
|
250
|
+
exiting = true;
|
|
251
|
+
if (created) {
|
|
252
|
+
spawnSync(ENGINE, ['rm', '-f', containerName], { stdio: 'ignore' });
|
|
253
|
+
}
|
|
254
|
+
logs.kill();
|
|
255
|
+
process.exit(0);
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
process.on('SIGTERM', cleanup);
|
|
259
|
+
process.on('SIGINT', cleanup);
|
|
260
|
+
process.on('SIGHUP', cleanup);
|
|
261
|
+
|
|
262
|
+
logs.on('exit', (code) => {
|
|
263
|
+
if (!exiting) process.exit(code ?? 0);
|
|
264
|
+
});
|
|
265
|
+
"
|
|
266
|
+
`;
|
|
267
|
+
|
|
268
|
+
exports[`ts#dynamodb generator > should generate terraform modules when iac is terraform 1`] = `
|
|
269
|
+
"terraform {
|
|
270
|
+
required_version = ">= 1.0"
|
|
271
|
+
|
|
272
|
+
required_providers {
|
|
273
|
+
aws = {
|
|
274
|
+
source = "hashicorp/aws"
|
|
275
|
+
version = "~> 6.0"
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
variable "name" {
|
|
281
|
+
description = "Name for the DynamoDB table."
|
|
282
|
+
type = string
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
variable "billing_mode" {
|
|
286
|
+
description = "Controls how you are charged for read and write throughput."
|
|
287
|
+
type = string
|
|
288
|
+
default = "PAY_PER_REQUEST"
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
variable "point_in_time_recovery_enabled" {
|
|
292
|
+
description = "Whether to enable point-in-time recovery for the table."
|
|
293
|
+
type = bool
|
|
294
|
+
default = true
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
variable "deletion_protection_enabled" {
|
|
298
|
+
description = "Whether to enable deletion protection on the table."
|
|
299
|
+
type = bool
|
|
300
|
+
default = true
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
variable "enable_key_rotation" {
|
|
304
|
+
description = "Whether to enable automatic key rotation on the KMS key used to encrypt the table."
|
|
305
|
+
type = bool
|
|
306
|
+
default = true
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
variable "global_secondary_indexes" {
|
|
310
|
+
description = "Global secondary indexes to create on the table. All key attributes must be of type string."
|
|
311
|
+
type = list(object({
|
|
312
|
+
name = string
|
|
313
|
+
hash_key = string
|
|
314
|
+
range_key = string
|
|
315
|
+
projection_type = string
|
|
316
|
+
}))
|
|
317
|
+
default = []
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
locals {
|
|
321
|
+
gsi_attribute_names = distinct(flatten([
|
|
322
|
+
for gsi in var.global_secondary_indexes : [gsi.hash_key, gsi.range_key]
|
|
323
|
+
]))
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
data "aws_caller_identity" "current" {}
|
|
327
|
+
data "aws_partition" "current" {}
|
|
328
|
+
|
|
329
|
+
resource "aws_kms_key" "table" {
|
|
330
|
+
description = "KMS key for DynamoDB table \${var.name}"
|
|
331
|
+
enable_key_rotation = var.enable_key_rotation
|
|
332
|
+
|
|
333
|
+
policy = jsonencode({
|
|
334
|
+
Version = "2012-10-17"
|
|
335
|
+
Statement = [
|
|
336
|
+
{
|
|
337
|
+
Sid = "EnableRootAccess"
|
|
338
|
+
Effect = "Allow"
|
|
339
|
+
Principal = {
|
|
340
|
+
AWS = "arn:\${data.aws_partition.current.partition}:iam::\${data.aws_caller_identity.current.account_id}:root"
|
|
341
|
+
}
|
|
342
|
+
Action = "kms:*"
|
|
343
|
+
Resource = "*"
|
|
344
|
+
},
|
|
345
|
+
{
|
|
346
|
+
Sid = "AllowDynamoDBService"
|
|
347
|
+
Effect = "Allow"
|
|
348
|
+
Principal = {
|
|
349
|
+
Service = "dynamodb.amazonaws.com"
|
|
350
|
+
}
|
|
351
|
+
Action = [
|
|
352
|
+
"kms:Encrypt",
|
|
353
|
+
"kms:Decrypt",
|
|
354
|
+
"kms:ReEncrypt*",
|
|
355
|
+
"kms:GenerateDataKey*",
|
|
356
|
+
"kms:DescribeKey",
|
|
357
|
+
"kms:CreateGrant"
|
|
358
|
+
]
|
|
359
|
+
Resource = "*"
|
|
360
|
+
}
|
|
361
|
+
]
|
|
362
|
+
})
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
resource "aws_dynamodb_table" "table" {
|
|
366
|
+
name = var.name
|
|
367
|
+
billing_mode = var.billing_mode
|
|
368
|
+
hash_key = "pk"
|
|
369
|
+
range_key = "sk"
|
|
370
|
+
deletion_protection_enabled = var.deletion_protection_enabled
|
|
371
|
+
|
|
372
|
+
attribute {
|
|
373
|
+
name = "pk"
|
|
374
|
+
type = "S"
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
attribute {
|
|
378
|
+
name = "sk"
|
|
379
|
+
type = "S"
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
dynamic "attribute" {
|
|
383
|
+
for_each = toset(local.gsi_attribute_names)
|
|
384
|
+
content {
|
|
385
|
+
name = attribute.value
|
|
386
|
+
type = "S"
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
dynamic "global_secondary_index" {
|
|
391
|
+
for_each = var.global_secondary_indexes
|
|
392
|
+
content {
|
|
393
|
+
name = global_secondary_index.value.name
|
|
394
|
+
hash_key = global_secondary_index.value.hash_key
|
|
395
|
+
range_key = global_secondary_index.value.range_key
|
|
396
|
+
projection_type = global_secondary_index.value.projection_type
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
point_in_time_recovery {
|
|
401
|
+
enabled = var.point_in_time_recovery_enabled
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
server_side_encryption {
|
|
405
|
+
enabled = true
|
|
406
|
+
kms_key_arn = aws_kms_key.table.arn
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
output "table_name" {
|
|
411
|
+
description = "Name of the DynamoDB table."
|
|
412
|
+
value = aws_dynamodb_table.table.name
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
output "table_arn" {
|
|
416
|
+
description = "ARN of the DynamoDB table."
|
|
417
|
+
value = aws_dynamodb_table.table.arn
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
output "kms_key_arn" {
|
|
421
|
+
description = "ARN of the KMS key used to encrypt the DynamoDB table."
|
|
422
|
+
value = aws_kms_key.table.arn
|
|
423
|
+
}
|
|
424
|
+
"
|
|
425
|
+
`;
|
|
426
|
+
|
|
427
|
+
exports[`ts#dynamodb generator > should generate terraform modules when iac is terraform 2`] = `
|
|
428
|
+
"terraform {
|
|
429
|
+
required_providers {
|
|
430
|
+
aws = {
|
|
431
|
+
source = "hashicorp/aws"
|
|
432
|
+
version = "~> 6.0"
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
variable "billing_mode" {
|
|
438
|
+
description = "Controls how you are charged for read and write throughput."
|
|
439
|
+
type = string
|
|
440
|
+
default = "PAY_PER_REQUEST"
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
variable "point_in_time_recovery_enabled" {
|
|
444
|
+
description = "Whether to enable point-in-time recovery for the table."
|
|
445
|
+
type = bool
|
|
446
|
+
default = true
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
variable "deletion_protection_enabled" {
|
|
450
|
+
description = "Whether to enable deletion protection on the table."
|
|
451
|
+
type = bool
|
|
452
|
+
default = true
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
variable "enable_key_rotation" {
|
|
456
|
+
description = "Whether to enable automatic key rotation on the KMS key used to encrypt the table."
|
|
457
|
+
type = bool
|
|
458
|
+
default = true
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
module "dynamodb_table" {
|
|
462
|
+
source = "../../../core/dynamodb"
|
|
463
|
+
name = "proj-my-table"
|
|
464
|
+
billing_mode = var.billing_mode
|
|
465
|
+
point_in_time_recovery_enabled = var.point_in_time_recovery_enabled
|
|
466
|
+
deletion_protection_enabled = var.deletion_protection_enabled
|
|
467
|
+
enable_key_rotation = var.enable_key_rotation
|
|
468
|
+
global_secondary_indexes = [
|
|
469
|
+
{
|
|
470
|
+
name = "gsi1pk-gsi1sk-index"
|
|
471
|
+
hash_key = "gsi1pk"
|
|
472
|
+
range_key = "gsi1sk"
|
|
473
|
+
projection_type = "ALL"
|
|
474
|
+
},
|
|
475
|
+
{
|
|
476
|
+
name = "gsi2pk-gsi2sk-index"
|
|
477
|
+
hash_key = "gsi2pk"
|
|
478
|
+
range_key = "gsi2sk"
|
|
479
|
+
projection_type = "ALL"
|
|
480
|
+
},
|
|
481
|
+
]
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
module "add_to_runtime_config" {
|
|
485
|
+
source = "../../../core/runtime-config/entry"
|
|
486
|
+
|
|
487
|
+
namespace = "dynamodb"
|
|
488
|
+
key = "MyTable"
|
|
489
|
+
value = {
|
|
490
|
+
tableName = module.dynamodb_table.table_name
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
"
|
|
494
|
+
`;
|
|
495
|
+
|
|
496
|
+
exports[`ts#dynamodb generator > should generate the dynamodb project > packages/my-table/scripts/create-local-table.ts 1`] = `
|
|
497
|
+
"import {
|
|
498
|
+
CreateTableCommand,
|
|
499
|
+
DescribeTableCommand,
|
|
500
|
+
DynamoDBClient,
|
|
501
|
+
GlobalSecondaryIndexDescription,
|
|
502
|
+
ResourceInUseException,
|
|
503
|
+
UpdateTableCommand,
|
|
504
|
+
} from '@aws-sdk/client-dynamodb';
|
|
505
|
+
import { GLOBAL_SECONDARY_INDEXES } from '../src/gsi.js';
|
|
506
|
+
|
|
507
|
+
const [tableName, port] = process.argv.slice(2);
|
|
508
|
+
const endpoint = \`http://localhost:\${port}\`;
|
|
509
|
+
|
|
510
|
+
const REQUEST_TIMEOUT_MS = 10_000;
|
|
511
|
+
const timeoutSignal = () => AbortSignal.timeout(REQUEST_TIMEOUT_MS);
|
|
512
|
+
|
|
513
|
+
const client = new DynamoDBClient({
|
|
514
|
+
endpoint,
|
|
515
|
+
region: 'us-east-1',
|
|
516
|
+
credentials: {
|
|
517
|
+
accessKeyId: 'accessKeyId',
|
|
518
|
+
secretAccessKey: 'secretAccessKey',
|
|
519
|
+
},
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
const waitForTableActive = async (
|
|
523
|
+
waitClient: DynamoDBClient,
|
|
524
|
+
waitTableName: string,
|
|
525
|
+
): Promise<void> => {
|
|
526
|
+
for (;;) {
|
|
527
|
+
const { Table } = await waitClient.send(
|
|
528
|
+
new DescribeTableCommand({ TableName: waitTableName }),
|
|
529
|
+
{ abortSignal: timeoutSignal() },
|
|
530
|
+
);
|
|
531
|
+
const allActive =
|
|
532
|
+
Table?.TableStatus === 'ACTIVE' &&
|
|
533
|
+
(Table?.GlobalSecondaryIndexes ?? []).every(
|
|
534
|
+
(g: GlobalSecondaryIndexDescription) => g.IndexStatus === 'ACTIVE',
|
|
535
|
+
);
|
|
536
|
+
if (allActive) return;
|
|
537
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
538
|
+
}
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
const ensureTable = async (
|
|
542
|
+
tblClient: DynamoDBClient,
|
|
543
|
+
tblName: string,
|
|
544
|
+
gsIs: { indexName: string; partitionKey: string; sortKey: string }[],
|
|
545
|
+
): Promise<void> => {
|
|
546
|
+
const gsiAttributeDefinitions = gsIs.flatMap((gsi) => [
|
|
547
|
+
{ AttributeName: gsi.partitionKey, AttributeType: 'S' as const },
|
|
548
|
+
{ AttributeName: gsi.sortKey, AttributeType: 'S' as const },
|
|
549
|
+
]);
|
|
550
|
+
const attributeDefinitions = [
|
|
551
|
+
{ AttributeName: 'pk', AttributeType: 'S' as const },
|
|
552
|
+
{ AttributeName: 'sk', AttributeType: 'S' as const },
|
|
553
|
+
...gsiAttributeDefinitions.filter(
|
|
554
|
+
(a, i, arr) =>
|
|
555
|
+
arr.findIndex((b) => b.AttributeName === a.AttributeName) === i,
|
|
556
|
+
),
|
|
557
|
+
];
|
|
558
|
+
const globalSecondaryIndexes = gsIs.map((gsi) => ({
|
|
559
|
+
IndexName: gsi.indexName,
|
|
560
|
+
KeySchema: [
|
|
561
|
+
{ AttributeName: gsi.partitionKey, KeyType: 'HASH' as const },
|
|
562
|
+
{ AttributeName: gsi.sortKey, KeyType: 'RANGE' as const },
|
|
563
|
+
],
|
|
564
|
+
Projection: { ProjectionType: 'ALL' as const },
|
|
565
|
+
}));
|
|
566
|
+
|
|
567
|
+
for (let attempt = 0; attempt < 30; attempt++) {
|
|
568
|
+
try {
|
|
569
|
+
// New table: create with all GSIs in one shot.
|
|
570
|
+
await tblClient.send(
|
|
571
|
+
new CreateTableCommand({
|
|
572
|
+
TableName: tblName,
|
|
573
|
+
AttributeDefinitions: attributeDefinitions,
|
|
574
|
+
KeySchema: [
|
|
575
|
+
{ AttributeName: 'pk', KeyType: 'HASH' },
|
|
576
|
+
{ AttributeName: 'sk', KeyType: 'RANGE' },
|
|
577
|
+
],
|
|
578
|
+
BillingMode: 'PAY_PER_REQUEST',
|
|
579
|
+
...(globalSecondaryIndexes.length > 0 && {
|
|
580
|
+
GlobalSecondaryIndexes: globalSecondaryIndexes,
|
|
581
|
+
}),
|
|
582
|
+
}),
|
|
583
|
+
{ abortSignal: timeoutSignal() },
|
|
584
|
+
);
|
|
585
|
+
console.log(\`Created table: \${tblName}\`);
|
|
586
|
+
for (const gsi of globalSecondaryIndexes) {
|
|
587
|
+
console.log(\`Created GSI: \${gsi.IndexName}\`);
|
|
588
|
+
}
|
|
589
|
+
return;
|
|
590
|
+
} catch (e) {
|
|
591
|
+
// Table already exists: sync GSIs to match desired state.
|
|
592
|
+
if (e instanceof ResourceInUseException) {
|
|
593
|
+
const { Table } = await tblClient.send(
|
|
594
|
+
new DescribeTableCommand({ TableName: tblName }),
|
|
595
|
+
{ abortSignal: timeoutSignal() },
|
|
596
|
+
);
|
|
597
|
+
const existingGSIs = Table?.GlobalSecondaryIndexes ?? [];
|
|
598
|
+
const existingNames = new Set(
|
|
599
|
+
existingGSIs.map(
|
|
600
|
+
(g: GlobalSecondaryIndexDescription) => g.IndexName ?? '',
|
|
601
|
+
),
|
|
602
|
+
);
|
|
603
|
+
const desiredNames = new Set(gsIs.map((g) => g.indexName));
|
|
604
|
+
|
|
605
|
+
// DynamoDB Local only allows one GSI to be created or deleted at a time.
|
|
606
|
+
for (const existing of existingGSIs) {
|
|
607
|
+
if (!existing.IndexName || desiredNames.has(existing.IndexName))
|
|
608
|
+
continue;
|
|
609
|
+
await tblClient.send(
|
|
610
|
+
new UpdateTableCommand({
|
|
611
|
+
TableName: tblName,
|
|
612
|
+
GlobalSecondaryIndexUpdates: [
|
|
613
|
+
{ Delete: { IndexName: existing.IndexName } },
|
|
614
|
+
],
|
|
615
|
+
}),
|
|
616
|
+
{ abortSignal: timeoutSignal() },
|
|
617
|
+
);
|
|
618
|
+
console.log(\`Deleted GSI: \${existing.IndexName}\`);
|
|
619
|
+
await waitForTableActive(tblClient, tblName);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
for (const gsi of gsIs) {
|
|
623
|
+
if (existingNames.has(gsi.indexName)) continue;
|
|
624
|
+
await tblClient.send(
|
|
625
|
+
new UpdateTableCommand({
|
|
626
|
+
TableName: tblName,
|
|
627
|
+
AttributeDefinitions: [
|
|
628
|
+
{ AttributeName: gsi.partitionKey, AttributeType: 'S' },
|
|
629
|
+
{ AttributeName: gsi.sortKey, AttributeType: 'S' },
|
|
630
|
+
],
|
|
631
|
+
GlobalSecondaryIndexUpdates: [
|
|
632
|
+
{
|
|
633
|
+
Create: {
|
|
634
|
+
IndexName: gsi.indexName,
|
|
635
|
+
KeySchema: [
|
|
636
|
+
{ AttributeName: gsi.partitionKey, KeyType: 'HASH' },
|
|
637
|
+
{ AttributeName: gsi.sortKey, KeyType: 'RANGE' },
|
|
638
|
+
],
|
|
639
|
+
Projection: { ProjectionType: 'ALL' },
|
|
640
|
+
},
|
|
641
|
+
},
|
|
642
|
+
],
|
|
643
|
+
}),
|
|
644
|
+
{ abortSignal: timeoutSignal() },
|
|
645
|
+
);
|
|
646
|
+
console.log(\`Created GSI: \${gsi.indexName}\`);
|
|
647
|
+
await waitForTableActive(tblClient, tblName);
|
|
648
|
+
}
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
if (attempt === 29) throw e;
|
|
652
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
|
|
657
|
+
await ensureTable(client, tableName, GLOBAL_SECONDARY_INDEXES);
|
|
658
|
+
"
|
|
659
|
+
`;
|
|
660
|
+
|
|
661
|
+
exports[`ts#dynamodb generator > should generate the dynamodb project > packages/my-table/scripts/pull-image.ts 1`] = `
|
|
662
|
+
"import { spawnSync } from 'child_process';
|
|
663
|
+
|
|
664
|
+
const ENGINE = 'docker';
|
|
665
|
+
const image = process.argv[2];
|
|
666
|
+
|
|
667
|
+
const inspect = spawnSync(ENGINE, ['image', 'inspect', image], {
|
|
668
|
+
stdio: 'ignore',
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
if (inspect.status !== 0) {
|
|
672
|
+
const pull = spawnSync(ENGINE, ['pull', image], { stdio: 'inherit' });
|
|
673
|
+
if (pull.status !== 0) {
|
|
674
|
+
process.exit(pull.status ?? 1);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
"
|
|
678
|
+
`;
|
|
679
|
+
|
|
680
|
+
exports[`ts#dynamodb generator > should generate the dynamodb project > packages/my-table/scripts/start-container.ts 1`] = `
|
|
681
|
+
"import { spawn, spawnSync } from 'child_process';
|
|
682
|
+
|
|
683
|
+
const ENGINE = 'docker';
|
|
684
|
+
|
|
685
|
+
const [containerName, image, hostPort] = process.argv.slice(2);
|
|
686
|
+
|
|
687
|
+
const containerExists = (): boolean =>
|
|
688
|
+
spawnSync(ENGINE, ['container', 'inspect', containerName], {
|
|
689
|
+
stdio: 'ignore',
|
|
690
|
+
}).status === 0;
|
|
691
|
+
|
|
692
|
+
const isRunning = (): boolean => {
|
|
693
|
+
const result = spawnSync(
|
|
694
|
+
ENGINE,
|
|
695
|
+
['container', 'inspect', '-f', '{{.State.Running}}', containerName],
|
|
696
|
+
{ encoding: 'utf-8' },
|
|
697
|
+
);
|
|
698
|
+
return result.status === 0 && result.stdout.trim() === 'true';
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
// Multiple dynamo table packages run serve-local in parallel sharing the same
|
|
702
|
+
// container. All processes stream logs, but only the one that created the
|
|
703
|
+
// container stops it on exit — others just detach.
|
|
704
|
+
let created = false;
|
|
705
|
+
let runRetries = 0;
|
|
706
|
+
|
|
707
|
+
for (;;) {
|
|
708
|
+
if (containerExists()) {
|
|
709
|
+
if (!isRunning()) {
|
|
710
|
+
// Another process may have started it between our inspect and start — container engine start
|
|
711
|
+
// exits 0 even if the container is already running, so no special-casing needed.
|
|
712
|
+
const start = spawnSync(ENGINE, ['start', containerName], {
|
|
713
|
+
stdio: 'inherit',
|
|
714
|
+
});
|
|
715
|
+
if (start.status !== 0) process.exit(start.status ?? 1);
|
|
716
|
+
}
|
|
717
|
+
break;
|
|
718
|
+
}
|
|
719
|
+
const runArgs = (
|
|
720
|
+
\`run --rm --name \${containerName} -u root -w /home/dynamodblocal\` +
|
|
721
|
+
\` -p \${hostPort}:\${hostPort} -v \${containerName}-data:/home/dynamodblocal/data\` +
|
|
722
|
+
\` -d \${image} -jar DynamoDBLocal.jar -sharedDb -dbPath ./data -port \${hostPort} -optimizeDbBeforeStartup\`
|
|
723
|
+
).split(' ');
|
|
724
|
+
const create = spawnSync(ENGINE, runArgs, { stdio: 'pipe' });
|
|
725
|
+
if (create.status === 0) {
|
|
726
|
+
created = true;
|
|
727
|
+
break;
|
|
728
|
+
}
|
|
729
|
+
if (!containerExists() && ++runRetries > 3) {
|
|
730
|
+
process.stderr.write(create.stderr);
|
|
731
|
+
process.exit(create.status ?? 1);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
const logs = spawn(ENGINE, ['logs', '-f', containerName], {
|
|
736
|
+
stdio: ['ignore', 'inherit', 'inherit'],
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
let exiting = false;
|
|
740
|
+
|
|
741
|
+
const cleanup = () => {
|
|
742
|
+
if (exiting) return;
|
|
743
|
+
exiting = true;
|
|
744
|
+
if (created) {
|
|
745
|
+
spawnSync(ENGINE, ['stop', containerName], { stdio: 'ignore' });
|
|
746
|
+
}
|
|
747
|
+
logs.kill();
|
|
748
|
+
process.exit(0);
|
|
749
|
+
};
|
|
750
|
+
|
|
751
|
+
process.on('SIGTERM', cleanup);
|
|
752
|
+
process.on('SIGINT', cleanup);
|
|
753
|
+
process.on('SIGHUP', cleanup);
|
|
754
|
+
|
|
755
|
+
logs.on('exit', (code) => {
|
|
756
|
+
if (!exiting) process.exit(code ?? 0);
|
|
757
|
+
});
|
|
758
|
+
"
|
|
759
|
+
`;
|
|
760
|
+
|
|
761
|
+
exports[`ts#dynamodb generator > should generate the dynamodb project > packages/my-table/src/client.ts 1`] = `
|
|
762
|
+
"import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
|
|
763
|
+
import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
|
|
764
|
+
import {
|
|
765
|
+
PACKAGE_NAME,
|
|
766
|
+
LOCAL_ACCESS_KEY_ID,
|
|
767
|
+
LOCAL_ENDPOINT,
|
|
768
|
+
LOCAL_REGION,
|
|
769
|
+
LOCAL_SECRET_ACCESS_KEY,
|
|
770
|
+
LOCAL_TABLE_NAME,
|
|
771
|
+
} from './constants.js';
|
|
772
|
+
|
|
773
|
+
let _client: DynamoDBClient | undefined;
|
|
774
|
+
|
|
775
|
+
export const getDynamoDBClient = (): DynamoDBClient => {
|
|
776
|
+
_client ??=
|
|
777
|
+
process.env.SERVE_LOCAL === 'true'
|
|
778
|
+
? new DynamoDBClient({
|
|
779
|
+
endpoint: LOCAL_ENDPOINT,
|
|
780
|
+
region: LOCAL_REGION,
|
|
781
|
+
credentials: {
|
|
782
|
+
accessKeyId: LOCAL_ACCESS_KEY_ID,
|
|
783
|
+
secretAccessKey: LOCAL_SECRET_ACCESS_KEY,
|
|
784
|
+
},
|
|
785
|
+
})
|
|
786
|
+
: new DynamoDBClient({});
|
|
787
|
+
return _client;
|
|
788
|
+
};
|
|
789
|
+
|
|
790
|
+
type DynamoDBConfig = {
|
|
791
|
+
tableName: string;
|
|
792
|
+
};
|
|
793
|
+
|
|
794
|
+
let _tableName: string | undefined;
|
|
795
|
+
|
|
796
|
+
export const resolveTableName = async (): Promise<string> => {
|
|
797
|
+
if (process.env.SERVE_LOCAL === 'true') {
|
|
798
|
+
return LOCAL_TABLE_NAME;
|
|
799
|
+
}
|
|
800
|
+
if (_tableName === undefined) {
|
|
801
|
+
const appId = process.env.RUNTIME_CONFIG_APP_ID;
|
|
802
|
+
if (!appId) {
|
|
803
|
+
throw new Error('RUNTIME_CONFIG_APP_ID environment variable is not set');
|
|
804
|
+
}
|
|
805
|
+
const config = await getAppConfig<{
|
|
806
|
+
[key: string]: DynamoDBConfig | undefined;
|
|
807
|
+
}>('dynamodb', {
|
|
808
|
+
application: appId,
|
|
809
|
+
environment: 'default',
|
|
810
|
+
transform: 'json',
|
|
811
|
+
});
|
|
812
|
+
const tableName = config?.[PACKAGE_NAME]?.tableName;
|
|
813
|
+
if (!tableName) {
|
|
814
|
+
throw new Error('Could not resolve table name from runtime config');
|
|
815
|
+
}
|
|
816
|
+
_tableName = tableName;
|
|
817
|
+
}
|
|
818
|
+
return _tableName!;
|
|
819
|
+
};
|
|
820
|
+
"
|
|
821
|
+
`;
|
|
822
|
+
|
|
823
|
+
exports[`ts#dynamodb generator > should generate the dynamodb project > packages/my-table/src/constants.ts 1`] = `
|
|
824
|
+
"export const PACKAGE_NAME = 'MyTable';
|
|
825
|
+
|
|
826
|
+
// Local development connection details (used when SERVE_LOCAL=true, see serve-local Nx target)
|
|
827
|
+
export const LOCAL_PORT = 8000;
|
|
828
|
+
export const LOCAL_REGION = 'us-east-1';
|
|
829
|
+
export const LOCAL_ACCESS_KEY_ID = 'accessKeyId';
|
|
830
|
+
export const LOCAL_SECRET_ACCESS_KEY = 'secretAccessKey';
|
|
831
|
+
export const LOCAL_ENDPOINT = \`http://localhost:\${LOCAL_PORT}\`;
|
|
832
|
+
export const LOCAL_TABLE_NAME = 'proj-my-table';
|
|
833
|
+
"
|
|
834
|
+
`;
|
|
835
|
+
|
|
836
|
+
exports[`ts#dynamodb generator > should generate the dynamodb project > packages/my-table/src/entities/example.ts 1`] = `
|
|
837
|
+
"import { Entity } from 'electrodb';
|
|
838
|
+
import { getDynamoDBClient, resolveTableName } from '../client.js';
|
|
839
|
+
|
|
840
|
+
// Example entity - replace with your own entities using ElectroDB
|
|
841
|
+
// See https://electrodb.dev/en/modeling/entities/ for documentation
|
|
842
|
+
export const createExampleEntity = async () =>
|
|
843
|
+
new Entity(
|
|
844
|
+
{
|
|
845
|
+
model: {
|
|
846
|
+
entity: 'example',
|
|
847
|
+
version: '1',
|
|
848
|
+
service: 'MyTable',
|
|
849
|
+
},
|
|
850
|
+
attributes: {
|
|
851
|
+
id: {
|
|
852
|
+
type: 'string',
|
|
853
|
+
required: true,
|
|
854
|
+
},
|
|
855
|
+
createdAt: {
|
|
856
|
+
type: 'string',
|
|
857
|
+
required: true,
|
|
858
|
+
default: () => new Date().toISOString(),
|
|
859
|
+
readOnly: true,
|
|
860
|
+
},
|
|
861
|
+
updatedAt: {
|
|
862
|
+
type: 'string',
|
|
863
|
+
required: true,
|
|
864
|
+
default: () => new Date().toISOString(),
|
|
865
|
+
watch: '*',
|
|
866
|
+
set: () => new Date().toISOString(),
|
|
867
|
+
},
|
|
868
|
+
},
|
|
869
|
+
indexes: {
|
|
870
|
+
primary: {
|
|
871
|
+
pk: {
|
|
872
|
+
field: 'pk',
|
|
873
|
+
composite: ['id'],
|
|
874
|
+
},
|
|
875
|
+
sk: {
|
|
876
|
+
field: 'sk',
|
|
877
|
+
composite: [],
|
|
878
|
+
},
|
|
879
|
+
},
|
|
880
|
+
},
|
|
881
|
+
},
|
|
882
|
+
{ client: getDynamoDBClient(), table: await resolveTableName() },
|
|
883
|
+
);
|
|
884
|
+
"
|
|
885
|
+
`;
|
|
886
|
+
|
|
887
|
+
exports[`ts#dynamodb generator > should generate the dynamodb project > packages/my-table/src/entities/index.ts 1`] = `
|
|
888
|
+
"export { createExampleEntity } from './example.js';
|
|
889
|
+
"
|
|
890
|
+
`;
|
|
891
|
+
|
|
892
|
+
exports[`ts#dynamodb generator > should generate the dynamodb project > packages/my-table/src/gsi.ts 1`] = `
|
|
893
|
+
"// GSIs to synchronize on the local table at startup and provision in infrastructure.
|
|
894
|
+
// ElectroDB names GSI keys gsi{n}pk / gsi{n}sk — add one entry per index defined in your entities.
|
|
895
|
+
export const GLOBAL_SECONDARY_INDEXES = [
|
|
896
|
+
{
|
|
897
|
+
indexName: 'gsi1pk-gsi1sk-index',
|
|
898
|
+
partitionKey: 'gsi1pk',
|
|
899
|
+
sortKey: 'gsi1sk',
|
|
900
|
+
},
|
|
901
|
+
{
|
|
902
|
+
indexName: 'gsi2pk-gsi2sk-index',
|
|
903
|
+
partitionKey: 'gsi2pk',
|
|
904
|
+
sortKey: 'gsi2sk',
|
|
905
|
+
},
|
|
906
|
+
];
|
|
907
|
+
"
|
|
908
|
+
`;
|
|
909
|
+
|
|
910
|
+
exports[`ts#dynamodb generator > should generate the dynamodb project > packages/my-table/src/index.ts 1`] = `
|
|
911
|
+
"export {
|
|
912
|
+
PACKAGE_NAME,
|
|
913
|
+
LOCAL_ACCESS_KEY_ID,
|
|
914
|
+
LOCAL_ENDPOINT,
|
|
915
|
+
LOCAL_REGION,
|
|
916
|
+
LOCAL_SECRET_ACCESS_KEY,
|
|
917
|
+
LOCAL_TABLE_NAME,
|
|
918
|
+
} from './constants.js';
|
|
919
|
+
export { getDynamoDBClient, resolveTableName } from './client.js';
|
|
920
|
+
export { GLOBAL_SECONDARY_INDEXES } from './gsi.js';
|
|
921
|
+
export * from './entities/index.js';
|
|
922
|
+
"
|
|
923
|
+
`;
|
|
924
|
+
|
|
925
|
+
exports[`ts#dynamodb generator > should generate the dynamodb project 1`] = `
|
|
926
|
+
"import { Construct } from 'constructs';
|
|
927
|
+
import {
|
|
928
|
+
AttributeType,
|
|
929
|
+
BillingMode,
|
|
930
|
+
Table,
|
|
931
|
+
TableEncryption,
|
|
932
|
+
TableProps,
|
|
933
|
+
} from 'aws-cdk-lib/aws-dynamodb';
|
|
934
|
+
import { RemovalPolicy } from 'aws-cdk-lib';
|
|
935
|
+
import { Grant, IGrantable } from 'aws-cdk-lib/aws-iam';
|
|
936
|
+
import { Key } from 'aws-cdk-lib/aws-kms';
|
|
937
|
+
import { RuntimeConfig } from './runtime-config.js';
|
|
938
|
+
|
|
939
|
+
type _DynamoDBTableProps = Omit<
|
|
940
|
+
TableProps,
|
|
941
|
+
'tableName' | 'partitionKey' | 'sortKey' | 'encryption' | 'encryptionKey'
|
|
942
|
+
>;
|
|
943
|
+
|
|
944
|
+
export interface DynamoDBTableProps extends _DynamoDBTableProps {
|
|
945
|
+
/**
|
|
946
|
+
* The DynamoDB table name. If omitted, CDK auto-generates a unique name
|
|
947
|
+
* from the stack name and construct path.
|
|
948
|
+
*/
|
|
949
|
+
readonly tableName?: string;
|
|
950
|
+
|
|
951
|
+
/**
|
|
952
|
+
* RuntimeConfig key used under the \`dynamodb\` namespace.
|
|
953
|
+
*/
|
|
954
|
+
readonly runtimeConfigKey: string;
|
|
955
|
+
|
|
956
|
+
/**
|
|
957
|
+
* Whether to enable automatic key rotation on the KMS key used to encrypt the table.
|
|
958
|
+
*
|
|
959
|
+
* @default true
|
|
960
|
+
*/
|
|
961
|
+
readonly enableKeyRotation?: boolean;
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
export abstract class DynamoDBTable extends Construct {
|
|
965
|
+
public readonly table: Table;
|
|
966
|
+
|
|
967
|
+
constructor(
|
|
968
|
+
scope: Construct,
|
|
969
|
+
id: string,
|
|
970
|
+
{
|
|
971
|
+
tableName,
|
|
972
|
+
runtimeConfigKey,
|
|
973
|
+
billingMode = BillingMode.PAY_PER_REQUEST,
|
|
974
|
+
pointInTimeRecoverySpecification = { pointInTimeRecoveryEnabled: true },
|
|
975
|
+
deletionProtection = true,
|
|
976
|
+
removalPolicy = RemovalPolicy.RETAIN,
|
|
977
|
+
enableKeyRotation = true,
|
|
978
|
+
...rest
|
|
979
|
+
}: DynamoDBTableProps,
|
|
980
|
+
) {
|
|
981
|
+
super(scope, id);
|
|
982
|
+
|
|
983
|
+
const key = new Key(this, 'EncryptionKey', { enableKeyRotation });
|
|
984
|
+
|
|
985
|
+
this.table = new Table(this, runtimeConfigKey, {
|
|
986
|
+
...rest,
|
|
987
|
+
...(tableName !== undefined && { tableName }),
|
|
988
|
+
partitionKey: { name: 'pk', type: AttributeType.STRING },
|
|
989
|
+
sortKey: { name: 'sk', type: AttributeType.STRING },
|
|
990
|
+
billingMode,
|
|
991
|
+
pointInTimeRecoverySpecification,
|
|
992
|
+
deletionProtection,
|
|
993
|
+
encryptionKey: key,
|
|
994
|
+
encryption: TableEncryption.CUSTOMER_MANAGED,
|
|
995
|
+
removalPolicy,
|
|
996
|
+
});
|
|
997
|
+
|
|
998
|
+
const rc = RuntimeConfig.ensure(this);
|
|
999
|
+
rc.set('dynamodb', runtimeConfigKey, { tableName: this.table.tableName });
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
public grantReadData(grantee: IGrantable): Grant {
|
|
1003
|
+
return this.table.grantReadData(grantee);
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
public grantWriteData(grantee: IGrantable): Grant {
|
|
1007
|
+
return this.table.grantWriteData(grantee);
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
public grantReadWriteData(grantee: IGrantable): Grant {
|
|
1011
|
+
return this.table.grantReadWriteData(grantee);
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
public grantFullAccess(grantee: IGrantable): Grant {
|
|
1015
|
+
return this.table.grantFullAccess(grantee);
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
public grantStreamRead(grantee: IGrantable): Grant {
|
|
1019
|
+
return this.table.grantStreamRead(grantee);
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
public grantTableListStreams(grantee: IGrantable): Grant {
|
|
1023
|
+
return this.table.grantTableListStreams(grantee);
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
public grant(grantee: IGrantable, ...actions: string[]): Grant {
|
|
1027
|
+
return this.table.grant(grantee, ...actions);
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
public grantStream(grantee: IGrantable, ...actions: string[]): Grant {
|
|
1031
|
+
return this.table.grantStream(grantee, ...actions);
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
"
|
|
1035
|
+
`;
|
|
1036
|
+
|
|
1037
|
+
exports[`ts#dynamodb generator > should generate the dynamodb project 2`] = `
|
|
1038
|
+
"import { Construct } from 'constructs';
|
|
1039
|
+
import { PACKAGE_NAME, GLOBAL_SECONDARY_INDEXES } from ':proj/my-table';
|
|
1040
|
+
import { AttributeType, ProjectionType } from 'aws-cdk-lib/aws-dynamodb';
|
|
1041
|
+
import { DynamoDBTable, DynamoDBTableProps } from '../../core/dynamodb.js';
|
|
1042
|
+
|
|
1043
|
+
export type MyTableProps = Omit<DynamoDBTableProps, 'runtimeConfigKey'>;
|
|
1044
|
+
|
|
1045
|
+
export class MyTable extends DynamoDBTable {
|
|
1046
|
+
constructor(scope: Construct, id: string, props?: MyTableProps) {
|
|
1047
|
+
super(scope, id, {
|
|
1048
|
+
...props,
|
|
1049
|
+
runtimeConfigKey: PACKAGE_NAME,
|
|
1050
|
+
});
|
|
1051
|
+
|
|
1052
|
+
for (const gsi of GLOBAL_SECONDARY_INDEXES) {
|
|
1053
|
+
this.table.addGlobalSecondaryIndex({
|
|
1054
|
+
indexName: gsi.indexName,
|
|
1055
|
+
partitionKey: { name: gsi.partitionKey, type: AttributeType.STRING },
|
|
1056
|
+
sortKey: { name: gsi.sortKey, type: AttributeType.STRING },
|
|
1057
|
+
projectionType: ProjectionType.ALL,
|
|
1058
|
+
});
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
"
|
|
1063
|
+
`;
|
|
1064
|
+
|
|
1065
|
+
exports[`ts#dynamodb generator > should generate the dynamodb project 3`] = `
|
|
1066
|
+
"export * from './my-table.js';
|
|
1067
|
+
"
|
|
1068
|
+
`;
|
|
1069
|
+
|
|
1070
|
+
exports[`ts#dynamodb generator > should generate the dynamodb project 4`] = `
|
|
1071
|
+
"export * from './dynamodb/index.js';
|
|
1072
|
+
"
|
|
1073
|
+
`;
|
|
1074
|
+
|
|
1075
|
+
exports[`ts#dynamodb generator > should generate with infra=none then upgrade to infra=dynamodb > packages/my-table/scripts/create-local-table.ts 1`] = `
|
|
1076
|
+
"import {
|
|
1077
|
+
CreateTableCommand,
|
|
1078
|
+
DescribeTableCommand,
|
|
1079
|
+
DynamoDBClient,
|
|
1080
|
+
GlobalSecondaryIndexDescription,
|
|
1081
|
+
ResourceInUseException,
|
|
1082
|
+
UpdateTableCommand,
|
|
1083
|
+
} from '@aws-sdk/client-dynamodb';
|
|
1084
|
+
import { GLOBAL_SECONDARY_INDEXES } from '../src/gsi.js';
|
|
1085
|
+
|
|
1086
|
+
const [tableName, port] = process.argv.slice(2);
|
|
1087
|
+
const endpoint = \`http://localhost:\${port}\`;
|
|
1088
|
+
|
|
1089
|
+
const REQUEST_TIMEOUT_MS = 10_000;
|
|
1090
|
+
const timeoutSignal = () => AbortSignal.timeout(REQUEST_TIMEOUT_MS);
|
|
1091
|
+
|
|
1092
|
+
const client = new DynamoDBClient({
|
|
1093
|
+
endpoint,
|
|
1094
|
+
region: 'us-east-1',
|
|
1095
|
+
credentials: {
|
|
1096
|
+
accessKeyId: 'accessKeyId',
|
|
1097
|
+
secretAccessKey: 'secretAccessKey',
|
|
1098
|
+
},
|
|
1099
|
+
});
|
|
1100
|
+
|
|
1101
|
+
const waitForTableActive = async (
|
|
1102
|
+
waitClient: DynamoDBClient,
|
|
1103
|
+
waitTableName: string,
|
|
1104
|
+
): Promise<void> => {
|
|
1105
|
+
for (;;) {
|
|
1106
|
+
const { Table } = await waitClient.send(
|
|
1107
|
+
new DescribeTableCommand({ TableName: waitTableName }),
|
|
1108
|
+
{ abortSignal: timeoutSignal() },
|
|
1109
|
+
);
|
|
1110
|
+
const allActive =
|
|
1111
|
+
Table?.TableStatus === 'ACTIVE' &&
|
|
1112
|
+
(Table?.GlobalSecondaryIndexes ?? []).every(
|
|
1113
|
+
(g: GlobalSecondaryIndexDescription) => g.IndexStatus === 'ACTIVE',
|
|
1114
|
+
);
|
|
1115
|
+
if (allActive) return;
|
|
1116
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
1117
|
+
}
|
|
1118
|
+
};
|
|
1119
|
+
|
|
1120
|
+
const ensureTable = async (
|
|
1121
|
+
tblClient: DynamoDBClient,
|
|
1122
|
+
tblName: string,
|
|
1123
|
+
gsIs: { indexName: string; partitionKey: string; sortKey: string }[],
|
|
1124
|
+
): Promise<void> => {
|
|
1125
|
+
const gsiAttributeDefinitions = gsIs.flatMap((gsi) => [
|
|
1126
|
+
{ AttributeName: gsi.partitionKey, AttributeType: 'S' as const },
|
|
1127
|
+
{ AttributeName: gsi.sortKey, AttributeType: 'S' as const },
|
|
1128
|
+
]);
|
|
1129
|
+
const attributeDefinitions = [
|
|
1130
|
+
{ AttributeName: 'pk', AttributeType: 'S' as const },
|
|
1131
|
+
{ AttributeName: 'sk', AttributeType: 'S' as const },
|
|
1132
|
+
...gsiAttributeDefinitions.filter(
|
|
1133
|
+
(a, i, arr) =>
|
|
1134
|
+
arr.findIndex((b) => b.AttributeName === a.AttributeName) === i,
|
|
1135
|
+
),
|
|
1136
|
+
];
|
|
1137
|
+
const globalSecondaryIndexes = gsIs.map((gsi) => ({
|
|
1138
|
+
IndexName: gsi.indexName,
|
|
1139
|
+
KeySchema: [
|
|
1140
|
+
{ AttributeName: gsi.partitionKey, KeyType: 'HASH' as const },
|
|
1141
|
+
{ AttributeName: gsi.sortKey, KeyType: 'RANGE' as const },
|
|
1142
|
+
],
|
|
1143
|
+
Projection: { ProjectionType: 'ALL' as const },
|
|
1144
|
+
}));
|
|
1145
|
+
|
|
1146
|
+
for (let attempt = 0; attempt < 30; attempt++) {
|
|
1147
|
+
try {
|
|
1148
|
+
// New table: create with all GSIs in one shot.
|
|
1149
|
+
await tblClient.send(
|
|
1150
|
+
new CreateTableCommand({
|
|
1151
|
+
TableName: tblName,
|
|
1152
|
+
AttributeDefinitions: attributeDefinitions,
|
|
1153
|
+
KeySchema: [
|
|
1154
|
+
{ AttributeName: 'pk', KeyType: 'HASH' },
|
|
1155
|
+
{ AttributeName: 'sk', KeyType: 'RANGE' },
|
|
1156
|
+
],
|
|
1157
|
+
BillingMode: 'PAY_PER_REQUEST',
|
|
1158
|
+
...(globalSecondaryIndexes.length > 0 && {
|
|
1159
|
+
GlobalSecondaryIndexes: globalSecondaryIndexes,
|
|
1160
|
+
}),
|
|
1161
|
+
}),
|
|
1162
|
+
{ abortSignal: timeoutSignal() },
|
|
1163
|
+
);
|
|
1164
|
+
console.log(\`Created table: \${tblName}\`);
|
|
1165
|
+
for (const gsi of globalSecondaryIndexes) {
|
|
1166
|
+
console.log(\`Created GSI: \${gsi.IndexName}\`);
|
|
1167
|
+
}
|
|
1168
|
+
return;
|
|
1169
|
+
} catch (e) {
|
|
1170
|
+
// Table already exists: sync GSIs to match desired state.
|
|
1171
|
+
if (e instanceof ResourceInUseException) {
|
|
1172
|
+
const { Table } = await tblClient.send(
|
|
1173
|
+
new DescribeTableCommand({ TableName: tblName }),
|
|
1174
|
+
{ abortSignal: timeoutSignal() },
|
|
1175
|
+
);
|
|
1176
|
+
const existingGSIs = Table?.GlobalSecondaryIndexes ?? [];
|
|
1177
|
+
const existingNames = new Set(
|
|
1178
|
+
existingGSIs.map(
|
|
1179
|
+
(g: GlobalSecondaryIndexDescription) => g.IndexName ?? '',
|
|
1180
|
+
),
|
|
1181
|
+
);
|
|
1182
|
+
const desiredNames = new Set(gsIs.map((g) => g.indexName));
|
|
1183
|
+
|
|
1184
|
+
// DynamoDB Local only allows one GSI to be created or deleted at a time.
|
|
1185
|
+
for (const existing of existingGSIs) {
|
|
1186
|
+
if (!existing.IndexName || desiredNames.has(existing.IndexName))
|
|
1187
|
+
continue;
|
|
1188
|
+
await tblClient.send(
|
|
1189
|
+
new UpdateTableCommand({
|
|
1190
|
+
TableName: tblName,
|
|
1191
|
+
GlobalSecondaryIndexUpdates: [
|
|
1192
|
+
{ Delete: { IndexName: existing.IndexName } },
|
|
1193
|
+
],
|
|
1194
|
+
}),
|
|
1195
|
+
{ abortSignal: timeoutSignal() },
|
|
1196
|
+
);
|
|
1197
|
+
console.log(\`Deleted GSI: \${existing.IndexName}\`);
|
|
1198
|
+
await waitForTableActive(tblClient, tblName);
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
for (const gsi of gsIs) {
|
|
1202
|
+
if (existingNames.has(gsi.indexName)) continue;
|
|
1203
|
+
await tblClient.send(
|
|
1204
|
+
new UpdateTableCommand({
|
|
1205
|
+
TableName: tblName,
|
|
1206
|
+
AttributeDefinitions: [
|
|
1207
|
+
{ AttributeName: gsi.partitionKey, AttributeType: 'S' },
|
|
1208
|
+
{ AttributeName: gsi.sortKey, AttributeType: 'S' },
|
|
1209
|
+
],
|
|
1210
|
+
GlobalSecondaryIndexUpdates: [
|
|
1211
|
+
{
|
|
1212
|
+
Create: {
|
|
1213
|
+
IndexName: gsi.indexName,
|
|
1214
|
+
KeySchema: [
|
|
1215
|
+
{ AttributeName: gsi.partitionKey, KeyType: 'HASH' },
|
|
1216
|
+
{ AttributeName: gsi.sortKey, KeyType: 'RANGE' },
|
|
1217
|
+
],
|
|
1218
|
+
Projection: { ProjectionType: 'ALL' },
|
|
1219
|
+
},
|
|
1220
|
+
},
|
|
1221
|
+
],
|
|
1222
|
+
}),
|
|
1223
|
+
{ abortSignal: timeoutSignal() },
|
|
1224
|
+
);
|
|
1225
|
+
console.log(\`Created GSI: \${gsi.indexName}\`);
|
|
1226
|
+
await waitForTableActive(tblClient, tblName);
|
|
1227
|
+
}
|
|
1228
|
+
return;
|
|
1229
|
+
}
|
|
1230
|
+
if (attempt === 29) throw e;
|
|
1231
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
};
|
|
1235
|
+
|
|
1236
|
+
await ensureTable(client, tableName, GLOBAL_SECONDARY_INDEXES);
|
|
1237
|
+
"
|
|
1238
|
+
`;
|
|
1239
|
+
|
|
1240
|
+
exports[`ts#dynamodb generator > should generate with infra=none then upgrade to infra=dynamodb > packages/my-table/scripts/pull-image.ts 1`] = `
|
|
1241
|
+
"import { spawnSync } from 'child_process';
|
|
1242
|
+
|
|
1243
|
+
const ENGINE = 'docker';
|
|
1244
|
+
const image = process.argv[2];
|
|
1245
|
+
|
|
1246
|
+
const inspect = spawnSync(ENGINE, ['image', 'inspect', image], {
|
|
1247
|
+
stdio: 'ignore',
|
|
1248
|
+
});
|
|
1249
|
+
|
|
1250
|
+
if (inspect.status !== 0) {
|
|
1251
|
+
const pull = spawnSync(ENGINE, ['pull', image], { stdio: 'inherit' });
|
|
1252
|
+
if (pull.status !== 0) {
|
|
1253
|
+
process.exit(pull.status ?? 1);
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
"
|
|
1257
|
+
`;
|
|
1258
|
+
|
|
1259
|
+
exports[`ts#dynamodb generator > should generate with infra=none then upgrade to infra=dynamodb > packages/my-table/scripts/start-container.ts 1`] = `
|
|
1260
|
+
"import { spawn, spawnSync } from 'child_process';
|
|
1261
|
+
|
|
1262
|
+
const ENGINE = 'docker';
|
|
1263
|
+
|
|
1264
|
+
const [containerName, image, hostPort] = process.argv.slice(2);
|
|
1265
|
+
|
|
1266
|
+
const containerExists = (): boolean =>
|
|
1267
|
+
spawnSync(ENGINE, ['container', 'inspect', containerName], {
|
|
1268
|
+
stdio: 'ignore',
|
|
1269
|
+
}).status === 0;
|
|
1270
|
+
|
|
1271
|
+
const isRunning = (): boolean => {
|
|
1272
|
+
const result = spawnSync(
|
|
1273
|
+
ENGINE,
|
|
1274
|
+
['container', 'inspect', '-f', '{{.State.Running}}', containerName],
|
|
1275
|
+
{ encoding: 'utf-8' },
|
|
1276
|
+
);
|
|
1277
|
+
return result.status === 0 && result.stdout.trim() === 'true';
|
|
1278
|
+
};
|
|
1279
|
+
|
|
1280
|
+
// Multiple dynamo table packages run serve-local in parallel sharing the same
|
|
1281
|
+
// container. All processes stream logs, but only the one that created the
|
|
1282
|
+
// container stops it on exit — others just detach.
|
|
1283
|
+
let created = false;
|
|
1284
|
+
let runRetries = 0;
|
|
1285
|
+
|
|
1286
|
+
for (;;) {
|
|
1287
|
+
if (containerExists()) {
|
|
1288
|
+
if (!isRunning()) {
|
|
1289
|
+
// Another process may have started it between our inspect and start — container engine start
|
|
1290
|
+
// exits 0 even if the container is already running, so no special-casing needed.
|
|
1291
|
+
const start = spawnSync(ENGINE, ['start', containerName], {
|
|
1292
|
+
stdio: 'inherit',
|
|
1293
|
+
});
|
|
1294
|
+
if (start.status !== 0) process.exit(start.status ?? 1);
|
|
1295
|
+
}
|
|
1296
|
+
break;
|
|
1297
|
+
}
|
|
1298
|
+
const runArgs = (
|
|
1299
|
+
\`run --rm --name \${containerName} -u root -w /home/dynamodblocal\` +
|
|
1300
|
+
\` -p \${hostPort}:\${hostPort} -v \${containerName}-data:/home/dynamodblocal/data\` +
|
|
1301
|
+
\` -d \${image} -jar DynamoDBLocal.jar -sharedDb -dbPath ./data -port \${hostPort} -optimizeDbBeforeStartup\`
|
|
1302
|
+
).split(' ');
|
|
1303
|
+
const create = spawnSync(ENGINE, runArgs, { stdio: 'pipe' });
|
|
1304
|
+
if (create.status === 0) {
|
|
1305
|
+
created = true;
|
|
1306
|
+
break;
|
|
1307
|
+
}
|
|
1308
|
+
if (!containerExists() && ++runRetries > 3) {
|
|
1309
|
+
process.stderr.write(create.stderr);
|
|
1310
|
+
process.exit(create.status ?? 1);
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
const logs = spawn(ENGINE, ['logs', '-f', containerName], {
|
|
1315
|
+
stdio: ['ignore', 'inherit', 'inherit'],
|
|
1316
|
+
});
|
|
1317
|
+
|
|
1318
|
+
let exiting = false;
|
|
1319
|
+
|
|
1320
|
+
const cleanup = () => {
|
|
1321
|
+
if (exiting) return;
|
|
1322
|
+
exiting = true;
|
|
1323
|
+
if (created) {
|
|
1324
|
+
spawnSync(ENGINE, ['stop', containerName], { stdio: 'ignore' });
|
|
1325
|
+
}
|
|
1326
|
+
logs.kill();
|
|
1327
|
+
process.exit(0);
|
|
1328
|
+
};
|
|
1329
|
+
|
|
1330
|
+
process.on('SIGTERM', cleanup);
|
|
1331
|
+
process.on('SIGINT', cleanup);
|
|
1332
|
+
process.on('SIGHUP', cleanup);
|
|
1333
|
+
|
|
1334
|
+
logs.on('exit', (code) => {
|
|
1335
|
+
if (!exiting) process.exit(code ?? 0);
|
|
1336
|
+
});
|
|
1337
|
+
"
|
|
1338
|
+
`;
|
|
1339
|
+
|
|
1340
|
+
exports[`ts#dynamodb generator > should generate with infra=none then upgrade to infra=dynamodb > packages/my-table/src/client.ts 1`] = `
|
|
1341
|
+
"import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
|
|
1342
|
+
import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
|
|
1343
|
+
import {
|
|
1344
|
+
PACKAGE_NAME,
|
|
1345
|
+
LOCAL_ACCESS_KEY_ID,
|
|
1346
|
+
LOCAL_ENDPOINT,
|
|
1347
|
+
LOCAL_REGION,
|
|
1348
|
+
LOCAL_SECRET_ACCESS_KEY,
|
|
1349
|
+
LOCAL_TABLE_NAME,
|
|
1350
|
+
} from './constants.js';
|
|
1351
|
+
|
|
1352
|
+
let _client: DynamoDBClient | undefined;
|
|
1353
|
+
|
|
1354
|
+
export const getDynamoDBClient = (): DynamoDBClient => {
|
|
1355
|
+
_client ??=
|
|
1356
|
+
process.env.SERVE_LOCAL === 'true'
|
|
1357
|
+
? new DynamoDBClient({
|
|
1358
|
+
endpoint: LOCAL_ENDPOINT,
|
|
1359
|
+
region: LOCAL_REGION,
|
|
1360
|
+
credentials: {
|
|
1361
|
+
accessKeyId: LOCAL_ACCESS_KEY_ID,
|
|
1362
|
+
secretAccessKey: LOCAL_SECRET_ACCESS_KEY,
|
|
1363
|
+
},
|
|
1364
|
+
})
|
|
1365
|
+
: new DynamoDBClient({});
|
|
1366
|
+
return _client;
|
|
1367
|
+
};
|
|
1368
|
+
|
|
1369
|
+
type DynamoDBConfig = {
|
|
1370
|
+
tableName: string;
|
|
1371
|
+
};
|
|
1372
|
+
|
|
1373
|
+
let _tableName: string | undefined;
|
|
1374
|
+
|
|
1375
|
+
export const resolveTableName = async (): Promise<string> => {
|
|
1376
|
+
if (process.env.SERVE_LOCAL === 'true') {
|
|
1377
|
+
return LOCAL_TABLE_NAME;
|
|
1378
|
+
}
|
|
1379
|
+
if (_tableName === undefined) {
|
|
1380
|
+
const appId = process.env.RUNTIME_CONFIG_APP_ID;
|
|
1381
|
+
if (!appId) {
|
|
1382
|
+
throw new Error('RUNTIME_CONFIG_APP_ID environment variable is not set');
|
|
1383
|
+
}
|
|
1384
|
+
const config = await getAppConfig<{
|
|
1385
|
+
[key: string]: DynamoDBConfig | undefined;
|
|
1386
|
+
}>('dynamodb', {
|
|
1387
|
+
application: appId,
|
|
1388
|
+
environment: 'default',
|
|
1389
|
+
transform: 'json',
|
|
1390
|
+
});
|
|
1391
|
+
const tableName = config?.[PACKAGE_NAME]?.tableName;
|
|
1392
|
+
if (!tableName) {
|
|
1393
|
+
throw new Error('Could not resolve table name from runtime config');
|
|
1394
|
+
}
|
|
1395
|
+
_tableName = tableName;
|
|
1396
|
+
}
|
|
1397
|
+
return _tableName!;
|
|
1398
|
+
};
|
|
1399
|
+
"
|
|
1400
|
+
`;
|
|
1401
|
+
|
|
1402
|
+
exports[`ts#dynamodb generator > should generate with infra=none then upgrade to infra=dynamodb > packages/my-table/src/constants.ts 1`] = `
|
|
1403
|
+
"export const PACKAGE_NAME = 'MyTable';
|
|
1404
|
+
|
|
1405
|
+
// Local development connection details (used when SERVE_LOCAL=true, see serve-local Nx target)
|
|
1406
|
+
export const LOCAL_PORT = 8000;
|
|
1407
|
+
export const LOCAL_REGION = 'us-east-1';
|
|
1408
|
+
export const LOCAL_ACCESS_KEY_ID = 'accessKeyId';
|
|
1409
|
+
export const LOCAL_SECRET_ACCESS_KEY = 'secretAccessKey';
|
|
1410
|
+
export const LOCAL_ENDPOINT = \`http://localhost:\${LOCAL_PORT}\`;
|
|
1411
|
+
export const LOCAL_TABLE_NAME = 'proj-my-table';
|
|
1412
|
+
"
|
|
1413
|
+
`;
|
|
1414
|
+
|
|
1415
|
+
exports[`ts#dynamodb generator > should generate with infra=none then upgrade to infra=dynamodb > packages/my-table/src/entities/example.ts 1`] = `
|
|
1416
|
+
"import { Entity } from 'electrodb';
|
|
1417
|
+
import { getDynamoDBClient, resolveTableName } from '../client.js';
|
|
1418
|
+
|
|
1419
|
+
// Example entity - replace with your own entities using ElectroDB
|
|
1420
|
+
// See https://electrodb.dev/en/modeling/entities/ for documentation
|
|
1421
|
+
export const createExampleEntity = async () =>
|
|
1422
|
+
new Entity(
|
|
1423
|
+
{
|
|
1424
|
+
model: {
|
|
1425
|
+
entity: 'example',
|
|
1426
|
+
version: '1',
|
|
1427
|
+
service: 'MyTable',
|
|
1428
|
+
},
|
|
1429
|
+
attributes: {
|
|
1430
|
+
id: {
|
|
1431
|
+
type: 'string',
|
|
1432
|
+
required: true,
|
|
1433
|
+
},
|
|
1434
|
+
createdAt: {
|
|
1435
|
+
type: 'string',
|
|
1436
|
+
required: true,
|
|
1437
|
+
default: () => new Date().toISOString(),
|
|
1438
|
+
readOnly: true,
|
|
1439
|
+
},
|
|
1440
|
+
updatedAt: {
|
|
1441
|
+
type: 'string',
|
|
1442
|
+
required: true,
|
|
1443
|
+
default: () => new Date().toISOString(),
|
|
1444
|
+
watch: '*',
|
|
1445
|
+
set: () => new Date().toISOString(),
|
|
1446
|
+
},
|
|
1447
|
+
},
|
|
1448
|
+
indexes: {
|
|
1449
|
+
primary: {
|
|
1450
|
+
pk: {
|
|
1451
|
+
field: 'pk',
|
|
1452
|
+
composite: ['id'],
|
|
1453
|
+
},
|
|
1454
|
+
sk: {
|
|
1455
|
+
field: 'sk',
|
|
1456
|
+
composite: [],
|
|
1457
|
+
},
|
|
1458
|
+
},
|
|
1459
|
+
},
|
|
1460
|
+
},
|
|
1461
|
+
{ client: getDynamoDBClient(), table: await resolveTableName() },
|
|
1462
|
+
);
|
|
1463
|
+
"
|
|
1464
|
+
`;
|
|
1465
|
+
|
|
1466
|
+
exports[`ts#dynamodb generator > should generate with infra=none then upgrade to infra=dynamodb > packages/my-table/src/entities/index.ts 1`] = `
|
|
1467
|
+
"export { createExampleEntity } from './example.js';
|
|
1468
|
+
"
|
|
1469
|
+
`;
|
|
1470
|
+
|
|
1471
|
+
exports[`ts#dynamodb generator > should generate with infra=none then upgrade to infra=dynamodb > packages/my-table/src/gsi.ts 1`] = `
|
|
1472
|
+
"// GSIs to synchronize on the local table at startup and provision in infrastructure.
|
|
1473
|
+
// ElectroDB names GSI keys gsi{n}pk / gsi{n}sk — add one entry per index defined in your entities.
|
|
1474
|
+
export const GLOBAL_SECONDARY_INDEXES = [
|
|
1475
|
+
{
|
|
1476
|
+
indexName: 'gsi1pk-gsi1sk-index',
|
|
1477
|
+
partitionKey: 'gsi1pk',
|
|
1478
|
+
sortKey: 'gsi1sk',
|
|
1479
|
+
},
|
|
1480
|
+
{
|
|
1481
|
+
indexName: 'gsi2pk-gsi2sk-index',
|
|
1482
|
+
partitionKey: 'gsi2pk',
|
|
1483
|
+
sortKey: 'gsi2sk',
|
|
1484
|
+
},
|
|
1485
|
+
];
|
|
1486
|
+
"
|
|
1487
|
+
`;
|
|
1488
|
+
|
|
1489
|
+
exports[`ts#dynamodb generator > should generate with infra=none then upgrade to infra=dynamodb > packages/my-table/src/index.ts 1`] = `
|
|
1490
|
+
"export {
|
|
1491
|
+
PACKAGE_NAME,
|
|
1492
|
+
LOCAL_ACCESS_KEY_ID,
|
|
1493
|
+
LOCAL_ENDPOINT,
|
|
1494
|
+
LOCAL_REGION,
|
|
1495
|
+
LOCAL_SECRET_ACCESS_KEY,
|
|
1496
|
+
LOCAL_TABLE_NAME,
|
|
1497
|
+
} from './constants.js';
|
|
1498
|
+
export { getDynamoDBClient, resolveTableName } from './client.js';
|
|
1499
|
+
export { GLOBAL_SECONDARY_INDEXES } from './gsi.js';
|
|
1500
|
+
export * from './entities/index.js';
|
|
1501
|
+
"
|
|
1502
|
+
`;
|