Skip to content

Commit

Permalink
feat: promiseify (ali-sdk#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Apr 2, 2017
1 parent 60c3f18 commit e4aed30
Show file tree
Hide file tree
Showing 14 changed files with 1,140 additions and 262 deletions.
9 changes: 9 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "eslint-config-egg",
"parserOptions": {
"ecmaVersion": 2017
},
"rules": {
"no-multi-str": 0,
},
}
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: node_js
node_js:
- '4'
- '6'
- '7'
install:
- npm i npminstall && npminstall
before_script:
Expand Down
47 changes: 23 additions & 24 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const wrap = require('co-wrap-all');
const Operator = require('./operator');
const RDSConnection = require('./connection');
const RDSTransaction = require('./transaction');
const promisify = require('pify');

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

this.pool = mysql.createPool(options);
[
'query',
'getConnection',
].forEach(method => {
this.pool[method] = promisify(this.pool[method]);
});
}

util.inherits(RDSClient, Operator);

const proto = RDSClient.prototype;

proto._query = function (sql) {
let pool = this.pool;
return function (callback) {
pool.query(sql, function (err, rows) {
callback(err, rows);
});
};
proto._query = function(sql) {
return this.pool.query(sql);
};

proto.getConnection = function () {
let pool = this.pool;
return function (callback) {
pool.getConnection(function (err, connection) {
if (err) {
if (err.name === 'Error') {
err.name = 'RDSClientGetConnectionError';
}
return callback(err);
}
let conn = new RDSConnection(connection);
callback(null, conn);
});
};
proto.getConnection = function() {
return this.pool.getConnection().then(onConnection, onError);
function onConnection(conn) {
return new RDSConnection(conn);
}
function onError(err) {
if (err.name === 'Error') {
err.name = 'RDSClientGetConnectionError';
}
throw err;
}
};

/**
Expand All @@ -54,7 +53,7 @@ proto.getConnection = function () {
* @return {Transaction} transaction instance
*/
proto.beginTransaction = function* () {
let conn = yield this.getConnection();
const conn = yield this.getConnection();
try {
yield conn.beginTransaction();
} catch (err) {
Expand All @@ -68,10 +67,10 @@ proto.beginTransaction = function* () {
/**
* Auto commit or rollback on a transaction scope
*
* @param {Function*} scope
* @param {Function} scope - scope with code
* @param {Object} [ctx] - transaction env context, like koa's ctx.
* To make sure only one active transaction on this ctx.
* @return {Object} scope return result
* @return {Object} - scope return result
*/
proto.beginTransactionScope = function* (scope, ctx) {
ctx = ctx || {};
Expand Down
51 changes: 22 additions & 29 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,46 @@

const util = require('util');
const Operator = require('./operator');
const promisify = require('pify');

module.exports = RDSConnection;

function RDSConnection(conn) {
Operator.call(this);
this.conn = conn;
if (!conn._wrapToRDS) {
[
'query',
'beginTransaction',
'commit',
'rollback',
].forEach(key => {
this.conn[key] = promisify(this.conn[key]);
});
conn._wrapToRDS = true;
}
}

util.inherits(RDSConnection, Operator);

const proto = RDSConnection.prototype;

proto.release = function () {
proto.release = function() {
this.conn.release();
};

proto._query = function (sql) {
let conn = this.conn;
return function (callback) {
conn.query(sql, function (err, rows) {
callback(err, rows);
});
};
proto._query = function(sql) {
return this.conn.query(sql);
};

proto.beginTransaction = function () {
let conn = this.conn;
return function (callback) {
conn.beginTransaction(function (err, result) {
callback(err, result);
});
};
proto.beginTransaction = function() {
return this.conn.beginTransaction();
};

proto.commit = function () {
let conn = this.conn;
return function (callback) {
conn.commit(function (err, result) {
callback(err, result);
});
};
proto.commit = function() {
return this.conn.commit();
};

proto.rollback = function () {
let conn = this.conn;
return function (callback) {
conn.rollback(function () {
callback();
});
};
proto.rollback = function() {
return this.conn.rollback();
};
2 changes: 1 addition & 1 deletion lib/literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function Literal(text) {
this.text = text;
}

Literal.prototype.toString = function () {
Literal.prototype.toString = function() {
return this.text;
};

Expand Down
Loading

0 comments on commit e4aed30

Please sign in to comment.