-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmysql.js
48 lines (43 loc) · 1.2 KB
/
mysql.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
var extend = require('extend');
function MysqlAdapter(opts) {
extend(this, opts);
}
MysqlAdapter.prototype.connect = function (cb) {
if (this.knex) return cb();
var connection = {
database: this.dbname || 'template1',
host: this.host || 'localhost',
port: this.port || 3306,
user: this.username || 'root',
password: this.password || '',
charset: this.charset || 'utf8'
};
this.knex = require('knex')({
client: 'mysql',
connection: connection,
debug: false,
useNullAsDefault: true,
pool: {
min: 1,
max: 8
}
});
return cb();
};
MysqlAdapter.prototype.upsert = function (properties, cb) {
var keys = Object.keys(properties);
var values = keys.map(function (k) {
return properties[k];
});
var sql = 'REPLACE INTO ' + this.tableName +
' (' + keys.map(function (key) { return '`' + key + '`'; }).join(',') +
') VALUES (' + values.map(function (x) { return '?'; }).join(',') + ')';
this.knex.raw(sql, values)
.then(function () { cb(); })
.catch(cb);
};
MysqlAdapter.prototype.close = function (cb) {
if (this.knex && this.knex.client) this.knex.client.pool.destroy();
setImmediate(cb);
};
module.exports = MysqlAdapter;