Skip to content

Commit b3eab93

Browse files
committed
feat: support end()
callback style and promise style
1 parent 0b2dae4 commit b3eab93

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,27 @@ Support `MySQL`, `SQL Server` and `PostgreSQL`.
3232
```js
3333
const rds = require('ali-rds');
3434

35-
let db = rds({
35+
const db = rds({
3636
host: 'your-rds-address.mysql.rds.aliyuncs.com',
3737
port: 3306,
3838
user: 'your-username',
3939
password: 'your-password',
4040
database: 'your-database-name',
41+
42+
// optional params
43+
// The charset for the connection.
44+
// This is called "collation" in the SQL-level of MySQL (like utf8_general_ci).
45+
// If a SQL-level charset is specified (like utf8mb4)
46+
// then the default collation for that charset is used. (Default: 'UTF8_GENERAL_CI')
47+
// charset: 'utf8_general_ci',
48+
//
4149
// The maximum number of connections to create at once. (Default: 10)
4250
// connectionLimit: 10,
51+
//
52+
// The maximum number of connection requests the pool will queue
53+
// before returning an error from getConnection.
54+
// If set to 0, there is no limit to the number of queued connection requests. (Default: 0)
55+
// queueLimit: 0,
4356
});
4457
```
4558

lib/client.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,17 @@ proto.beginTransactionScope = function* (scope, ctx) {
101101
throw err;
102102
}
103103
};
104+
105+
proto.end = function(callback) {
106+
// callback style
107+
if (callback) return this.pool.end(callback);
108+
109+
// promise style
110+
const that = this;
111+
return new Promise(function(resolve, reject) {
112+
that.pool.end(function(err) {
113+
if (err) return reject(err);
114+
resolve();
115+
});
116+
});
117+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"devDependencies": {
2020
"autod": "*",
21+
"co": "^4.6.0",
2122
"co-mocha": "*",
2223
"istanbul": "*",
2324
"jshint": "*",
@@ -41,4 +42,4 @@
4142
},
4243
"author": "fengmk2 <m@fengmk2.com> (http://fengmk2.com)",
4344
"license": "MIT"
44-
}
45+
}

test/client.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Module dependencies.
55
*/
66

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

20+
after(function(done) {
21+
this.db.end(done);
22+
});
23+
1924
describe('rds(options)', function () {
2025
it('should connect rds success', function* () {
2126
let rows = yield this.db.query('show tables');
@@ -820,4 +825,31 @@ describe('client.test.js', function () {
820825
assert.equal(count, 0);
821826
});
822827
});
828+
829+
describe('mock query after client end', function() {
830+
it('should query throw error after end', function*() {
831+
const db = rds(config);
832+
yield db.query('select * from ?? limit 10', [table]);
833+
yield db.end();
834+
const db2 = rds(config);
835+
836+
try {
837+
yield db.query('select * from ?? limit 10', [table]);
838+
throw new Error('should not run this');
839+
} catch (err) {
840+
assert.equal(err.message, 'Pool is closed.');
841+
}
842+
843+
yield db2.query('select * from ?? limit 10', [table]);
844+
yield db2.end();
845+
});
846+
847+
it('should support end with callback style', function(done) {
848+
const db = rds(config);
849+
co(function*() {
850+
yield db.query('select * from ?? limit 10', [table]);
851+
db.end(done);
852+
}).catch(done);
853+
});
854+
});
823855
});

0 commit comments

Comments
 (0)