| 5 | export type MysqlConnectionDetails = { host: string, port: number, username: string, password: string, database: string } |
| 6 | |
| 7 | export class MysqlEngine implements DatabaseEngine { |
| 8 | |
| 9 | public connection: knexlib.Knex | null = null; |
| 10 | |
| 11 | constructor(connector: knexlib.Knex) { |
| 12 | this.connection = connector; |
| 13 | } |
| 14 | |
| 15 | getType(): KnexClient { |
| 16 | return 'mysql2'; |
| 17 | } |
| 18 | |
| 19 | getConnection(): knexlib.Knex | null { |
| 20 | return this.connection |
| 21 | } |
| 22 | |
| 23 | async isOkay(): Promise<boolean> { |
| 24 | if (!this.connection) return false; |
| 25 | |
| 26 | try { |
| 27 | await this.connection.raw('SELECT VERSION()'); |
| 28 | return true; |
| 29 | } catch { |
| 30 | return false; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | async disconnect() { |
| 35 | if (this.connection) this.connection.destroy(() => null); |
| 36 | } |
| 37 | |
| 38 | async getTableCreationSql(table: string): Promise<string> { |
| 39 | if (!this.connection) return ''; |
| 40 | |
| 41 | const creationSql = (await this.connection.raw(`SHOW CREATE TABLE ??`, [table]))[0]; |
| 42 | |
| 43 | const sql = (creationSql[0] as any)['Create Table']; |
| 44 | |
| 45 | /** |
| 46 | * Comes formatted, and any attempt to use format(...) |
| 47 | * from the sql-formatter package causes an issue whereby |
| 48 | * newline is added between "CHARACTER SET", which is still |
| 49 | * readable but largely odd. |
| 50 | */ |
| 51 | return sql |
| 52 | } |
| 53 | |
| 54 | async getTables(): Promise<string[]> { |
| 55 | if (!this.connection) return []; |
| 56 | |
| 57 | const tables = ((await this.connection.raw('SHOW TABLES'))[0]).map((entry: Record<string, string>) => Object.values(entry)[0]); |
| 58 | |
| 59 | return tables; |
| 60 | } |
| 61 | |
| 62 | async getColumns(table: string): Promise<Column[]> { |
| 63 | if (!this.connection) return []; |
| 64 |
nothing calls this directly
no outgoing calls
no test coverage detected