1 |
|
2 |
|
3 |
|
4 |
|
5 | export class AuroraDataAPIClient {
|
6 | AWS: any;
|
7 | RDS: any;
|
8 | Params: DataApiParams;
|
9 |
|
10 | setRDSClient(rdsClient: any) {
|
11 | this.RDS = rdsClient;
|
12 | }
|
13 |
|
14 | constructor(databaseRegion: string, awsSecretStoreArn: string, dbClusterOrInstanceArn: string, database: string, aws: any) {
|
15 | this.AWS = aws;
|
16 | this.AWS.config.update({
|
17 | region: databaseRegion,
|
18 | });
|
19 |
|
20 | this.RDS = new this.AWS.RDSDataService();
|
21 | this.Params = new DataApiParams();
|
22 |
|
23 | this.Params.secretArn = awsSecretStoreArn;
|
24 | this.Params.resourceArn = dbClusterOrInstanceArn;
|
25 | this.Params.database = database;
|
26 | }
|
27 |
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 | public listTables = async () => {
|
34 | this.Params.sql = 'SHOW TABLES';
|
35 | const response = await this.RDS.executeStatement(this.Params).promise();
|
36 |
|
37 | let tableList = [];
|
38 | const records = response['records'];
|
39 | for (const record of records) {
|
40 | tableList.push(record[0]['stringValue']);
|
41 | }
|
42 |
|
43 | return tableList;
|
44 | };
|
45 |
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | public describeTable = async (tableName: string) => {
|
53 | this.Params.sql = `DESCRIBE ${tableName}`;
|
54 | const response = await this.RDS.executeStatement(this.Params).promise();
|
55 | const listOfColumns = response['records'];
|
56 | let columnDescriptions = [];
|
57 | for (const column of listOfColumns) {
|
58 | let colDescription = new ColumnDescription();
|
59 |
|
60 | colDescription.Field = column[MYSQL_DESCRIBE_TABLE_ORDER.Field]['stringValue'];
|
61 | colDescription.Type = column[MYSQL_DESCRIBE_TABLE_ORDER.Type]['stringValue'];
|
62 | colDescription.Null = column[MYSQL_DESCRIBE_TABLE_ORDER.Null]['stringValue'];
|
63 | colDescription.Key = column[MYSQL_DESCRIBE_TABLE_ORDER.Key]['stringValue'];
|
64 | colDescription.Default = column[MYSQL_DESCRIBE_TABLE_ORDER.Default]['stringValue'];
|
65 | colDescription.Extra = column[MYSQL_DESCRIBE_TABLE_ORDER.Extra]['stringValue'];
|
66 |
|
67 | columnDescriptions.push(colDescription);
|
68 | }
|
69 |
|
70 | return columnDescriptions;
|
71 | };
|
72 |
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | public getTableForeignKeyReferences = async (tableName: string) => {
|
80 | this.Params.sql = `SELECT TABLE_NAME FROM information_schema.key_column_usage
|
81 | WHERE referenced_table_name is not null
|
82 | AND REFERENCED_TABLE_NAME = '${tableName}';`;
|
83 | const response = await this.RDS.executeStatement(this.Params).promise();
|
84 |
|
85 | let tableList = [];
|
86 | const records = response['records'];
|
87 | for (const record of records) {
|
88 | tableList.push(record[0]['stringValue']);
|
89 | }
|
90 |
|
91 | return tableList;
|
92 | };
|
93 | }
|
94 |
|
95 | export class DataApiParams {
|
96 | database: string;
|
97 | secretArn: string;
|
98 | resourceArn: string;
|
99 | sql: string;
|
100 | }
|
101 |
|
102 | export class ColumnDescription {
|
103 | Field: string;
|
104 | Type: string;
|
105 | Null: string;
|
106 | Key: string;
|
107 | Default: string;
|
108 | Extra: string;
|
109 | }
|
110 |
|
111 | enum MYSQL_DESCRIBE_TABLE_ORDER {
|
112 | Field,
|
113 | Type,
|
114 | Null,
|
115 | Key,
|
116 | Default,
|
117 | Extra,
|
118 | }
|