-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathMysqlClient.ts
91 lines (84 loc) · 2.37 KB
/
MysqlClient.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import type { Connection as MySqlConnection } from 'mysql2'
import { Connection } from '../SettingStore'
import AbstractClient, { RawField } from './AbstractClient'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const mysql = require('mysql2')
export default class MysqlClient extends AbstractClient {
connection: MySqlConnection | null = null
constructor(settings: Connection) {
super(settings)
}
get DefaultPort() {
return 3306
}
get DefaultHost() {
return '127.0.0.1'
}
get DefaultUser() {
return 'root'
}
async connect() {
this.connection = mysql.createConnection({
host: this.settings.host || this.DefaultHost,
password: this.settings.password || '',
user: this.settings.user || this.DefaultUser,
port: this.settings.port || this.DefaultPort,
database: this.settings.database || '',
})
return true
}
disconnect() {
if (this.connection) {
this.connection.end()
}
this.connection = null
}
getTables(): Promise<string[]> {
const sql = `
SELECT table_name
FROM information_schema.tables
WHERE table_schema = '${this.settings.database}'
`
return new Promise((resolve, reject) => {
if (!this.connection) {
reject(new Error("Don't have database connection."))
return
}
this.connection.query(
sql,
(err, results: { [key: string]: string }[]) => {
if (err) {
reject(new Error(err.message))
return
}
const tables = results.map((v) => v['table_name'] || v['TABLE_NAME'])
resolve(tables)
}
)
})
}
getColumns(tableName: string): Promise<RawField[]> {
const sql = `SHOW FULL FIELDS FROM ${tableName}`
return new Promise((resolve, reject) => {
if (!this.connection) {
reject(new Error("Don't have database connection."))
return
}
this.connection.query(sql, (err, results) => {
if (err) {
reject(new Error(err.message))
return
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const columns: RawField[] = (results as any).map((v: any) => ({
field: v.Field,
type: v.Type,
null: v.Null,
default: v.Default,
comment: v.Comment,
}))
resolve(columns)
})
})
}
}