Skip to content

Commit e4aed30

Browse files
authored
feat: promiseify (ali-sdk#20)
1 parent 60c3f18 commit e4aed30

File tree

14 files changed

+1140
-262
lines changed

14 files changed

+1140
-262
lines changed

.eslintrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "eslint-config-egg",
3+
"parserOptions": {
4+
"ecmaVersion": 2017
5+
},
6+
"rules": {
7+
"no-multi-str": 0,
8+
},
9+
}

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ language: node_js
33
node_js:
44
- '4'
55
- '6'
6+
- '7'
67
install:
78
- npm i npminstall && npminstall
89
before_script:

lib/client.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const wrap = require('co-wrap-all');
66
const Operator = require('./operator');
77
const RDSConnection = require('./connection');
88
const RDSTransaction = require('./transaction');
9+
const promisify = require('pify');
910

1011
module.exports = RDSClient;
1112
module.exports.literals = require('./literals');
@@ -17,35 +18,33 @@ function RDSClient(options) {
1718
Operator.call(this);
1819

1920
this.pool = mysql.createPool(options);
21+
[
22+
'query',
23+
'getConnection',
24+
].forEach(method => {
25+
this.pool[method] = promisify(this.pool[method]);
26+
});
2027
}
2128

2229
util.inherits(RDSClient, Operator);
2330

2431
const proto = RDSClient.prototype;
2532

26-
proto._query = function (sql) {
27-
let pool = this.pool;
28-
return function (callback) {
29-
pool.query(sql, function (err, rows) {
30-
callback(err, rows);
31-
});
32-
};
33+
proto._query = function(sql) {
34+
return this.pool.query(sql);
3335
};
3436

35-
proto.getConnection = function () {
36-
let pool = this.pool;
37-
return function (callback) {
38-
pool.getConnection(function (err, connection) {
39-
if (err) {
40-
if (err.name === 'Error') {
41-
err.name = 'RDSClientGetConnectionError';
42-
}
43-
return callback(err);
44-
}
45-
let conn = new RDSConnection(connection);
46-
callback(null, conn);
47-
});
48-
};
37+
proto.getConnection = function() {
38+
return this.pool.getConnection().then(onConnection, onError);
39+
function onConnection(conn) {
40+
return new RDSConnection(conn);
41+
}
42+
function onError(err) {
43+
if (err.name === 'Error') {
44+
err.name = 'RDSClientGetConnectionError';
45+
}
46+
throw err;
47+
}
4948
};
5049

5150
/**
@@ -54,7 +53,7 @@ proto.getConnection = function () {
5453
* @return {Transaction} transaction instance
5554
*/
5655
proto.beginTransaction = function* () {
57-
let conn = yield this.getConnection();
56+
const conn = yield this.getConnection();
5857
try {
5958
yield conn.beginTransaction();
6059
} catch (err) {
@@ -68,10 +67,10 @@ proto.beginTransaction = function* () {
6867
/**
6968
* Auto commit or rollback on a transaction scope
7069
*
71-
* @param {Function*} scope
70+
* @param {Function} scope - scope with code
7271
* @param {Object} [ctx] - transaction env context, like koa's ctx.
7372
* To make sure only one active transaction on this ctx.
74-
* @return {Object} scope return result
73+
* @return {Object} - scope return result
7574
*/
7675
proto.beginTransactionScope = function* (scope, ctx) {
7776
ctx = ctx || {};

lib/connection.js

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,46 @@
66

77
const util = require('util');
88
const Operator = require('./operator');
9+
const promisify = require('pify');
910

1011
module.exports = RDSConnection;
1112

1213
function RDSConnection(conn) {
1314
Operator.call(this);
1415
this.conn = conn;
16+
if (!conn._wrapToRDS) {
17+
[
18+
'query',
19+
'beginTransaction',
20+
'commit',
21+
'rollback',
22+
].forEach(key => {
23+
this.conn[key] = promisify(this.conn[key]);
24+
});
25+
conn._wrapToRDS = true;
26+
}
1527
}
28+
1629
util.inherits(RDSConnection, Operator);
1730

1831
const proto = RDSConnection.prototype;
1932

20-
proto.release = function () {
33+
proto.release = function() {
2134
this.conn.release();
2235
};
2336

24-
proto._query = function (sql) {
25-
let conn = this.conn;
26-
return function (callback) {
27-
conn.query(sql, function (err, rows) {
28-
callback(err, rows);
29-
});
30-
};
37+
proto._query = function(sql) {
38+
return this.conn.query(sql);
3139
};
3240

33-
proto.beginTransaction = function () {
34-
let conn = this.conn;
35-
return function (callback) {
36-
conn.beginTransaction(function (err, result) {
37-
callback(err, result);
38-
});
39-
};
41+
proto.beginTransaction = function() {
42+
return this.conn.beginTransaction();
4043
};
4144

42-
proto.commit = function () {
43-
let conn = this.conn;
44-
return function (callback) {
45-
conn.commit(function (err, result) {
46-
callback(err, result);
47-
});
48-
};
45+
proto.commit = function() {
46+
return this.conn.commit();
4947
};
5048

51-
proto.rollback = function () {
52-
let conn = this.conn;
53-
return function (callback) {
54-
conn.rollback(function () {
55-
callback();
56-
});
57-
};
49+
proto.rollback = function() {
50+
return this.conn.rollback();
5851
};

lib/literals.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Literal(text) {
1313
this.text = text;
1414
}
1515

16-
Literal.prototype.toString = function () {
16+
Literal.prototype.toString = function() {
1717
return this.text;
1818
};
1919

0 commit comments

Comments
 (0)