/
dynamo-tools.ts
55 lines (51 loc) · 1.37 KB
/
dynamo-tools.ts
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
import {
DeleteTableCommand,
DynamoDBClient,
waitUntilTableNotExists,
} from '@aws-sdk/client-dynamodb';
import { StackReference } from './constants.js';
import type { DynamoConfig } from './@types/index.js';
/**
* Toolkit for Dynamo operations
*
* @export
* @class DynamoTools
*/
export class DynamoTools {
public dynamo: DynamoDBClient;
/**
* Creates an instance of DynamoTools.
*
* @param {DynamoConfig} config - Configuration options for the Dynamo toolkit
* @memberof DynamoTools
*/
public constructor(public config: DynamoConfig) {
this.config = config;
this.dynamo = new DynamoDBClient({ region: this.config.awsRegion });
}
/**
* Deletes a dynamo table
*
* @param {StackReference} reference - Reference to a active table
* @returns {Promise<void>}
* @memberof DynamoTools
*/
public async deleteTable(reference: StackReference): Promise<void> {
const TableName = this.getTableName(reference);
await this.dynamo.send(new DeleteTableCommand({ TableName }));
if (this.config.waitForTableDelete) {
await waitUntilTableNotExists(
{
client: this.dynamo,
maxWaitTime: 300,
},
{ TableName }
);
}
}
protected getTableName(ref: StackReference): string {
return ref === StackReference.a
? this.config.tableNameA
: this.config.tableNameB;
}
}