forked from sidorares/node-mysql2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.js
115 lines (102 loc) · 2.94 KB
/
promise.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
var core = require('./index.js');
function createConnection(opts) {
var coreConnection = core.createConnection(opts);
var Promise = opts.Promise || global.Promise;
if (!Promise) {
throw new Error('no Promise implementation available.' +
'Use promise-enabled node version or pass userland Promise' +
' implementation as parameter, for example: { Promise: require(\'es6-promise\').Promise }');
}
return new Promise(function(resolve, reject) {
coreConnection.once('connect', function(connectParams) {
resolve(new PromiseConnection(coreConnection, Promise));
});
coreConnection.once('error', reject);
});
}
function PromiseConnection(connection, promiseImpl) {
this.connection = connection;
this.Promise = promiseImpl;
}
PromiseConnection.prototype.release = function() {
this.connection.release();
};
PromiseConnection.prototype.query = function(sql, values) {
var c = this.connection;
return new this.Promise(function(resolve, reject) {
c.query(sql, values, function(err, rows, fields) {
if (err)
reject(err);
else
resolve([rows, fields]);
});
});
};
PromiseConnection.prototype.execute = function(query, params) {
var c = this.connection;
return new this.Promise(function(resolve, reject) {
c.execute(query, params, function(err, rows, fields) {
if (err)
reject(err);
else
resolve([rows, fields]);
});
});
};
PromiseConnection.prototype.end = function() {
var c = this.connection;
return new this.Promise(function(resolve, reject) {
c.end(function() {
resolve();
});
});
};
function createPool(opts) {
var corePool = core.createPool(opts);
var Promise = opts.Promise || global.Promise || require('es6-promise');
var promisePool = {
getConnection: function() {
return new Promise(function(resolve, reject) {
corePool.getConnection(function(err, coreConnection) {
if (err)
reject(err);
else
resolve(new PromiseConnection(coreConnection, Promise));
});
});
},
query: function(sql, args) {
return new Promise(function(resolve, reject) {
corePool.query(sql, args, function(err, rows, fields) {
if (err)
reject(err);
else
resolve([rows, fields]);
});
});
},
execute: function(sql, args) {
return new Promise(function(resolve, reject) {
corePool.execute(sql, args, function(err, rows, fields) {
if (err)
reject(err);
else
resolve([rows, fields]);
});
});
},
end: function() {
return new Promise(function(resolve, reject) {
corePool.end(function(err) {
if (err)
reject(err);
else
resolve();
})
});
}
};
return promisePool;
}
module.exports.createConnection = createConnection;
module.exports.createPool = createPool;