diff --git a/README.md b/README.md index 716d26d..59d30b9 100644 --- a/README.md +++ b/README.md @@ -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, }); ``` diff --git a/lib/client.js b/lib/client.js index c85183f..c14f0d7 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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(); + }); + }); +}; diff --git a/package.json b/package.json index c21aee3..47ec5a6 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ }, "devDependencies": { "autod": "*", + "co": "^4.6.0", "co-mocha": "*", "istanbul": "*", "jshint": "*", @@ -41,4 +42,4 @@ }, "author": "fengmk2 (http://fengmk2.com)", "license": "MIT" -} +} \ No newline at end of file diff --git a/test/client.test.js b/test/client.test.js index f1f8df3..0e1b513 100644 --- a/test/client.test.js +++ b/test/client.test.js @@ -4,6 +4,7 @@ * Module dependencies. */ +const co = require('co'); const assert = require('assert'); const rds = require('../'); const config = require('./config'); @@ -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'); @@ -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); + }); + }); });