-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
222 lines (178 loc) · 5.17 KB
/
index.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const SQL = require('like-sql')
const mysql = require('mysql2/promise')
// + support for sqlite?
module.exports = function (hostname, user, password, database, opts = {}) {
return new LikePool(hostname, user, password, database, opts)
}
class MySQL extends SQL {
constructor (opts = {}) {
super(opts)
this.charset = opts.charset === undefined ? 'utf8mb4' : opts.charset
this.collate = opts.collate === undefined ? 'utf8mb4_unicode_ci' : opts.collate
this.engine = opts.engine === undefined ? 'InnoDB' : opts.engine
}
async _createDatabase (sql) {
const [res] = await this.execute(sql)
return res.warningStatus === 0
}
async _dropDatabase (sql) {
const [res] = await this.execute(sql)
return res.warningStatus === 0
}
async _createTable (sql) {
const [res] = await this.execute(sql)
return res.warningStatus === 0
}
async _dropTable (sql) {
const [res] = await this.execute(sql)
return res.warningStatus === 0
}
async _insert (sql, values) {
const [res] = await this.execute(sql, values)
return res.insertId
}
async _select (sql, values) {
const [res] = await this.execute(sql, values)
return res
}
async _selectOne (sql, values) {
const [res] = await this.execute(sql, values)
return res.length ? res[0] : undefined
}
async _exists (sql, values) {
const [res, fields] = await this.execute(sql, values)
return !!res[0][fields[0].name]
}
async _count (sql, values) {
const [res, fields] = await this.execute(sql, values)
return res[0][fields[0].name]
}
async _update (sql, values) {
const [res] = await this.execute(sql, values)
return res.changedRows
}
async _delete (sql, values) {
const [res] = await this.execute(sql, values)
return res.affectedRows
}
static parseHostname (host) {
let port
let socketPath
const semicolon = host.indexOf(':')
if (semicolon > -1) { // host:port
port = host.substr(semicolon + 1)
host = host.substr(0, semicolon)
} else { // socket path
socketPath = host
host = ''
}
return { host, port, socketPath }
}
}
class LikePool extends MySQL {
constructor (hostname, user, password, database, opts = {}) {
super(opts)
const { host, port, socketPath } = MySQL.parseHostname(hostname)
this.pool = mysql.createPool({
host: host || '127.0.0.1',
port: port || 3306,
socketPath: socketPath || '',
user: user || 'root',
password: password || '',
database: database || '',
charset: this.collate, // in createPool options, charset is collate value
supportBigNumbers: typeof opts.supportBigNumbers === 'boolean' ? opts.supportBigNumbers : true,
bigNumberStrings: typeof opts.bigNumberStrings === 'boolean' ? opts.bigNumberStrings : false,
decimalNumbers: typeof opts.decimalNumbers === 'boolean' ? opts.decimalNumbers : true,
connectionLimit: opts.connectionLimit || 20,
waitForConnections: typeof opts.waitForConnections === 'boolean' ? opts.waitForConnections : true,
ssl: opts.ssl
})
}
async getConnection () {
const conn = await this.pool.getConnection()
const db = new LikeConnection(conn)
return db
}
async ready (timeout = 15000) {
const started = Date.now()
while (true) {
try {
const conn = await this.pool.getConnection()
conn.release()
break
} catch (error) {
if (error.code === 'ER_ACCESS_DENIED_ERROR') throw error
if (error.code === 'ER_BAD_DB_ERROR') throw error
if (error.message === 'No connections available.') throw error
if (Date.now() - started > timeout) throw error
await sleep(500)
}
}
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
}
query (sql, values) {
return this.pool.query(sql, values)
}
execute (sql, values) {
return this.pool.execute(sql, values)
}
end () {
return this.pool.end()
}
async transaction (callback) {
const conn = await this.getConnection()
let output
await releaseOnError(conn, conn.beginTransaction())
try {
output = await callback(conn)
await conn.commit()
} catch (err) {
await releaseOnError(conn, conn.rollback())
conn.release()
throw err
}
conn.release()
return output
function releaseOnError (conn, promise) {
return promise.catch(err => {
conn.release()
throw err
})
}
}
}
class LikeConnection extends MySQL {
constructor (conn, opts = {}) {
super(opts)
this.connection = conn
}
query (sql, values) {
return this.connection.query(sql, values)
}
execute (sql, values) {
return this.connection.execute(sql, values)
}
end () {
return this.connection.end()
}
destroy () {
return this.connection.destroy()
}
beginTransaction () {
return this.connection.beginTransaction()
}
commit () {
return this.connection.commit()
}
rollback () {
return this.connection.rollback()
}
release () {
return this.connection.release()
}
}
// mysqldump -h 127.0.0.1 --port=3307 -u root -p meli categories > categories.sql
// db.dump()