Skip to content

Commit

Permalink
Merge 760d57e into 51cc445
Browse files Browse the repository at this point in the history
  • Loading branch information
airondumael committed Nov 26, 2015
2 parents 51cc445 + 760d57e commit 7e04f8b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,21 @@ mysql.use('db1')
.end();
```

### Closing a pooled connection
Use mysql.end_pool() to close a pooled connection.
```javascript
mysql.add('db1', config.DB1, true);

let db1 = mysql.use('db1');
db1.query('INSERT INTO unique_email(email) VALUES (?)', ['unique'], callback)
.end();

db1.query('INSERT INTO unique_email(email) VALUES (?)', ['uniqu3'], callback)
.end();

db1.end_pool();
```


### Using a different logger
```javascript
Expand Down
28 changes: 24 additions & 4 deletions lib/CustomMySQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ var _mysql2 = _interopRequireDefault(_mysql);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
* This is my class description
*/
Expand Down Expand Up @@ -156,6 +156,26 @@ var CustomMySQL = (function () {

return this;
}
}, {
key: 'end_pool',
value: function end_pool() {
var _this = this;

if (this._key && this[this._key].connection) {
this[this._key].connection.end(function (err) {
if (err) {
return _this._logger.error('Closing pool ' + _this._key + ' error: \n', err);
}

_this._logger.info('Pool ' + _this._key + ' closed');
});

this.current_connection = null;
this[this._key].connection = null;
}

return this;
}

/* Everything below will be deprecated */

Expand Down Expand Up @@ -198,7 +218,7 @@ var CustomMySQL = (function () {
}, {
key: 'async',
value: function async(query, args, async_args, collector, fn) {
var _this = this;
var _this2 = this;

var results = [];
var len = args.length;
Expand Down Expand Up @@ -229,7 +249,7 @@ var CustomMySQL = (function () {
}

args.forEach(function (arg, index) {
_this.args(async_args && async_args.hasOwnProperty(index) ? async_args[index] : arg).query(query, arg, _collector);
_this2.args(async_args && async_args.hasOwnProperty(index) ? async_args[index] : arg).query(query, arg, _collector);
});

return this;
Expand Down
4 changes: 2 additions & 2 deletions lib/Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ var _mysql2 = _interopRequireDefault(_mysql);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Query = (function () {
function Query(mysql) {
_classCallCheck(this, Query);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "anytv-node-mysql",
"version": "0.0.36",
"version": "0.0.37",
"description": "Our version of mysql that makes connecting to mysql simpler and more elegant. Especially made for our awesome expressjs boilerplate.",
"main": "index.js",
"dependencies": {
Expand Down
15 changes: 15 additions & 0 deletions src/CustomMySQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,22 @@ export default class CustomMySQL {
return this;
}

end_pool () {
if (this._key && this[this._key].connection) {
this[this._key].connection.end((err) => {
if (err) {
return this._logger.error(`Closing pool ${this._key} error: \n`, err);
}

this._logger.info(`Pool ${this._key} closed`);
});

this.current_connection = null;
this[this._key].connection = null;
}

return this;
}

/* Everything below will be deprecated */

Expand Down
29 changes: 29 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,4 +735,33 @@ describe('Overall test', () => {
done();
});

it ('mysql.end_pool should close pooled connections', (done) => {
const mysql = new CustomMySQL();
const key = 'key';

mysql.set_logger(noop_logger)
.add(key, {}, true)
.use(key)
.end_pool();

should.equal(mysql[key].connection, null);

should.equal(mysql.current_connection, null);

done();
});

it ('mysql.end_pool should return the mysql object', (done) => {
const mysql = new CustomMySQL();
const key = 'key';

mysql.set_logger(noop_logger)
.add(key, {}, true)
.use(key)
.end_pool()
.should.be.equal(mysql)

done();
});

});

0 comments on commit 7e04f8b

Please sign in to comment.