-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathenable-global-tables.js
73 lines (58 loc) · 2.23 KB
/
enable-global-tables.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
This code is a little more complicated as in order to enable Global Tables,
DynamoDB Streams must first be enabled on the table. If it is not enabled, this
code example enables it, waits until it is actually enabled (it takes a few seconds),
and then actually enables Global Tables on the table in question.
*/
const { DynamoDB,
DynamoDBClient,
DescribeTableCommand,
UpdateTableCommand,
waitForTableExists,
UpdateGlobalTableCommand,
CreateGlobalTableCommand
} = require('@aws-sdk/client-dynamodb');
const REGION = "us-west-2";
const TableName = "Music";
const dbclient = new DynamoDBClient({ region: REGION });
async function enableStreams() {
const params = {
TableName: TableName,
StreamSpecification: {
StreamEnabled: true,
StreamViewType: "NEW_AND_OLD_IMAGES",
},
};
const response = await dbclient.send( new UpdateTableCommand(params));
//return await waitForTableExists({ maxWaitTime: 20, maxDelay: 5, minDelay: 1 }, { TableName: TableName } );
}
async function isStreamsEnabled() {
const params = { TableName: TableName };
const response = await dbclient.send(new DescribeTableCommand(params));
return response.Table.StreamSpecification.StreamEnabled;
};
isStreamsEnabled()
.then((data) => console.log(data))
/* .then((isEnabled) => {
if (!isEnabled) {
enableStreams();
console.log("woah!");
}
}
.then (() => {
enableGlobalTables();
}) */
.catch((error) => console.log("An error occurred while checking if DynamoDB Streams are enabled:" + ' ' + error.message ));
enableStreams()
.then((data) => console.log(data))
.catch((error) => console.log(error));
/*
enableGlobalTables()
.then((data) => console.log(JSON.stringify(data, null, 2)))
.catch((error) => console.error(JSON.stringify(error, null, 2)));
await dynamodb.waitFor("tableExists", { TableName: "Music" }).promise();
Call function to check to see if streams are enabled on the table.
If true, call enable global tables function. If there is an error, log error and quit.
If false, enable streams and wait until they are enabled. Once enabled, log success and quit.
If error, log an error and quit.
*/