UNPKG

3.38 kBPlain TextView Raw
1/**
2 * A wrapper around the RDS data service client, forming their responses for
3 * easier consumption.
4 */
5export 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 * Lists all of the tables in the set database.
30 *
31 * @return a list of tables in the database.
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 * Describes the table given, by breaking it down into individual column descriptions.
48 *
49 * @param the name of the table to be described.
50 * @return a list of column descriptions.
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 * Gets foreign keys for the given table, if any exist.
75 *
76 * @param tableName the name of the table to be checked.
77 * @return a list of tables referencing the provided table, if any exist.
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
95export class DataApiParams {
96 database: string;
97 secretArn: string;
98 resourceArn: string;
99 sql: string;
100}
101
102export class ColumnDescription {
103 Field: string;
104 Type: string;
105 Null: string;
106 Key: string;
107 Default: string;
108 Extra: string;
109}
110
111enum MYSQL_DESCRIBE_TABLE_ORDER {
112 Field,
113 Type,
114 Null,
115 Key,
116 Default,
117 Extra,
118}