-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmysql-store.js
executable file
·215 lines (157 loc) · 4.75 KB
/
mysql-store.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
const MySQL = require('mysql')
const DefaultConfig = require('./default_config.json')
const Util = require('util')
const { intern } = require('./lib/intern')
const { asyncmethod } = intern
const STORE_NAME = 'mysql-store'
function mysql_store (options) {
const seneca = this
const opts = seneca.util.deepextend(DefaultConfig, options)
const internals = {
name: STORE_NAME,
opts
}
function configure(spec, done) {
const conf = get_config(spec)
const default_conn_opts = {
connectionLimit: conf.poolSize || 5,
host: conf.host,
user: conf.user || conf.username,
password: conf.password,
database: conf.name,
port: conf.port || 3306
}
const conn_opts = conf.conn || default_conn_opts
internals.connectionPool = MySQL.createPool(conn_opts)
internals.spec = spec
return ensure_connected(done)
function get_config(spec) {
if ('string' === typeof spec) {
const urlM = /^mysql:\/\/((.*?):(.*?)@)?(.*?)(:?(\d+))?\/(.*?)$/.exec(spec)
const conf = {
name: urlM[7],
server: urlM[4],
username: urlM[2],
password: urlM[3],
port: urlM[6] ? parseInt(conf.port, 10) : null
}
return conf
}
return spec
}
function ensure_connected(done) {
return internals.connectionPool.getConnection((err, conn) => {
if (err) {
return done(err)
}
conn.release()
return done()
})
}
}
const store = {
name: STORE_NAME,
close: asyncmethod(async function (_msg) {
const { connectionPool: pool = null } = internals
if (pool) {
const end = Util.promisify(pool.end).bind(pool)
try {
await end()
} catch (err) {
return seneca.fail('connection/end', {
store: internals.name,
error: err
})
}
}
seneca.log.debug('Closed the connection to the db')
}),
save: asyncmethod(async function (msg) {
const seneca = this
const ctx = { seneca, db: internals.connectionPool }
if (intern.is_update(msg)) {
return intern.do_update(msg, ctx)
}
return intern.do_create(msg, ctx)
}),
load: asyncmethod(async function (msg) {
const seneca = this
const { qent, q } = msg
const ctx = { seneca, db: internals.connectionPool }
const where = intern.where_of_q(q, ctx)
const out = await intern.loadent({
ent: qent,
where,
limit: 1,
offset: 0 <= q.skip$ ? q.skip$ : null,
order_by: q.sort$ || null
}, ctx)
seneca.log.debug('load', 'ok', q, out)
return out
}),
list: asyncmethod(async function (msg) {
const seneca = this
const { qent, q } = msg
const ctx = { seneca, db: internals.connectionPool }
let out
const nat_query = intern.is_native(msg)
if (null == nat_query) {
const where = intern.where_of_q(q, ctx)
out = await intern.listents({
ent: qent,
where,
limit: 0 <= q.limit$ ? q.limit$ : null,
offset: 0 <= q.skip$ ? q.skip$ : null,
order_by: q.sort$ || null
}, ctx)
} else {
const rows = await intern.execquery(nat_query, ctx)
out = rows.map(row => intern.makeent(qent, row))
}
seneca.log.debug('list', 'ok', q, out.length)
return out
}),
remove: asyncmethod(async function (msg) {
const seneca = this
const { q } = msg
const ctx = { seneca, db: internals.connectionPool }
let op_name
let out
if (q.all$) {
op_name = 'remove/all'
out = await intern.remove_many(msg, ctx)
} else {
op_name = 'remove/one'
out = await intern.remove_one(msg, ctx)
}
seneca.log.debug(op_name, 'ok', q)
return out
}),
native: asyncmethod(async function (_msg) {
return internals.connectionPool
})
}
const meta = seneca.store.init(seneca, opts, store)
internals.desc = meta.desc
seneca.add({ init: store.name, tag: meta.tag }, function (args, done) {
configure(internals.opts, function (err) {
if (err) {
return seneca.fail('entity/configure', {
store: internals.name,
error: err,
desc: internals.desc
})
}
seneca.log.debug('Successfully connected to the database')
return done()
})
})
return { name: store.name, tag: meta.tag }
}
module.exports = mysql_store
module.exports.errors = {
'entity/configure': 'Failed to connect to the database, store "<%=store%>", ' +
'error: "<%=error%>", desc: <%=desc%>',
'connection/end': 'Failed to close the connection, store "<%=store%>, ' +
'error: "<%=error%>"'
}