Skip to content

Commit

Permalink
feat: support end()
Browse files Browse the repository at this point in the history
callback style and promise style
  • Loading branch information
fengmk2 committed Jun 7, 2016
1 parent 0b2dae4 commit b3eab93
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,27 @@ Support `MySQL`, `SQL Server` and `PostgreSQL`.
```js
const rds = require('ali-rds');

let db = rds({
const db = rds({
host: 'your-rds-address.mysql.rds.aliyuncs.com',
port: 3306,
user: 'your-username',
password: 'your-password',
database: 'your-database-name',

// optional params
// The charset for the connection.
// This is called "collation" in the SQL-level of MySQL (like utf8_general_ci).
// If a SQL-level charset is specified (like utf8mb4)
// then the default collation for that charset is used. (Default: 'UTF8_GENERAL_CI')
// charset: 'utf8_general_ci',
//
// The maximum number of connections to create at once. (Default: 10)
// connectionLimit: 10,
//
// The maximum number of connection requests the pool will queue
// before returning an error from getConnection.
// If set to 0, there is no limit to the number of queued connection requests. (Default: 0)
// queueLimit: 0,
});
```

Expand Down
14 changes: 14 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,17 @@ proto.beginTransactionScope = function* (scope, ctx) {
throw err;
}
};

proto.end = function(callback) {
// callback style
if (callback) return this.pool.end(callback);

// promise style
const that = this;
return new Promise(function(resolve, reject) {
that.pool.end(function(err) {
if (err) return reject(err);
resolve();
});
});
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"devDependencies": {
"autod": "*",
"co": "^4.6.0",
"co-mocha": "*",
"istanbul": "*",
"jshint": "*",
Expand All @@ -41,4 +42,4 @@
},
"author": "fengmk2 <m@fengmk2.com> (http://fengmk2.com)",
"license": "MIT"
}
}
32 changes: 32 additions & 0 deletions test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Module dependencies.
*/

const co = require('co');
const assert = require('assert');
const rds = require('../');
const config = require('./config');
Expand All @@ -16,6 +17,10 @@ describe('client.test.js', function () {
yield this.db.query('delete from ?? where name like ?', [table, prefix + '%']);
});

after(function(done) {
this.db.end(done);
});

describe('rds(options)', function () {
it('should connect rds success', function* () {
let rows = yield this.db.query('show tables');
Expand Down Expand Up @@ -820,4 +825,31 @@ describe('client.test.js', function () {
assert.equal(count, 0);
});
});

describe('mock query after client end', function() {
it('should query throw error after end', function*() {
const db = rds(config);
yield db.query('select * from ?? limit 10', [table]);
yield db.end();
const db2 = rds(config);

try {
yield db.query('select * from ?? limit 10', [table]);
throw new Error('should not run this');
} catch (err) {
assert.equal(err.message, 'Pool is closed.');
}

yield db2.query('select * from ?? limit 10', [table]);
yield db2.end();
});

it('should support end with callback style', function(done) {
const db = rds(config);
co(function*() {
yield db.query('select * from ?? limit 10', [table]);
db.end(done);
}).catch(done);
});
});
});

0 comments on commit b3eab93

Please sign in to comment.