UNPKG

3.89 kBPlain TextView Raw
1
2/**
3 * A wrapper around the RDS data service client, forming their responses for
4 * easier consumption.
5 */
6export class AuroraDataAPIClient {
7
8 AWS: any
9 RDS: any
10 Params: DataApiParams
11
12 setRDSClient(rdsClient: any) {
13 this.RDS = rdsClient
14 }
15
16 constructor(databaseRegion: string, awsSecretStoreArn: string, dbClusterOrInstanceArn: string, database: string, aws: any) {
17 this.AWS = aws
18 this.AWS.config.update({
19 region: databaseRegion
20 })
21
22 this.RDS = new this.AWS.RDSDataService()
23 this.Params = new DataApiParams()
24
25 this.Params.awsSecretStoreArn = awsSecretStoreArn
26 this.Params.dbClusterOrInstanceArn = dbClusterOrInstanceArn
27 this.Params.database = database
28 }
29
30 /**
31 * Lists all of the tables in the set database.
32 *
33 * @return a list of tables in the database.
34 */
35 public listTables = async () => {
36 this.Params.sqlStatements = 'SHOW TABLES'
37 const response = await this.RDS.executeSql(this.Params).promise()
38
39 let tableList = []
40 const records = response['sqlStatementResults'][0]['resultFrame']['records']
41 for (const record of records) {
42 tableList.push(record['values'][0]['stringValue'])
43 }
44
45 return tableList
46 }
47
48 /**
49 * Describes the table given, by breaking it down into individual column descriptions.
50 *
51 * @param the name of the table to be described.
52 * @return a list of column descriptions.
53 */
54 public describeTable = async (tableName: string) => {
55 this.Params.sqlStatements = `DESCRIBE ${tableName}`
56 const response = await this.RDS.executeSql(this.Params).promise()
57 const listOfColumns = response['sqlStatementResults'][0]['resultFrame']['records']
58 let columnDescriptions = []
59 for (const column of listOfColumns) {
60 let colDescription = new ColumnDescription()
61 const colValues = column['values']
62
63 colDescription.Field = colValues[MYSQL_DESCRIBE_TABLE_ORDER.Field]['stringValue']
64 colDescription.Type = colValues[MYSQL_DESCRIBE_TABLE_ORDER.Type]['stringValue']
65 colDescription.Null = colValues[MYSQL_DESCRIBE_TABLE_ORDER.Null]['stringValue']
66 colDescription.Key = colValues[MYSQL_DESCRIBE_TABLE_ORDER.Key]['stringValue']
67 colDescription.Default = colValues[MYSQL_DESCRIBE_TABLE_ORDER.Default]['stringValue']
68 colDescription.Extra = colValues[MYSQL_DESCRIBE_TABLE_ORDER.Extra]['stringValue']
69
70 columnDescriptions.push(colDescription)
71 }
72
73 return columnDescriptions
74 }
75
76 /**
77 * Gets foreign keys for the given table, if any exist.
78 *
79 * @param tableName the name of the table to be checked.
80 * @return a list of tables referencing the provided table, if any exist.
81 */
82 public getTableForeignKeyReferences = async (tableName: string) => {
83 this.Params.sqlStatements = `SELECT TABLE_NAME FROM information_schema.key_column_usage
84 WHERE referenced_table_name is not null
85 AND REFERENCED_TABLE_NAME = '${tableName}';`
86 const response = await this.RDS.executeSql(this.Params).promise()
87
88 let tableList = []
89 const records = response['sqlStatementResults'][0]['resultFrame']['records']
90 for (const record of records) {
91 tableList.push(record['values'][0]['stringValue'])
92 }
93
94 return tableList
95 }
96
97}
98
99export class DataApiParams {
100 awsSecretStoreArn: string
101 dbClusterOrInstanceArn: string
102 sqlStatements: string
103 database: string
104}
105
106export class ColumnDescription {
107 Field: string
108 Type: string
109 Null: string
110 Key: string
111 Default: string
112 Extra: string
113}
114
115enum MYSQL_DESCRIBE_TABLE_ORDER {
116 Field,
117 Type,
118 Null,
119 Key,
120 Default,
121 Extra
122}
\No newline at end of file