不要怂,就是干,撸起袖子干!

Commit b73bd2db by Mick Hansen

add: beforeConnect hook

1 parent d894f107
...@@ -243,7 +243,11 @@ ConnectionManager.prototype.releaseConnection = function(connection) { ...@@ -243,7 +243,11 @@ ConnectionManager.prototype.releaseConnection = function(connection) {
}; };
ConnectionManager.prototype.$connect = function(config) { ConnectionManager.prototype.$connect = function(config) {
return this.dialect.connectionManager.connect(config); return this.sequelize.runHooks('beforeConnect', config).bind(this).then(function () {
return this.dialect.connectionManager.connect(config).then(function (connection) {
return connection;
});
});
}; };
ConnectionManager.prototype.$disconnect = function(connection) { ConnectionManager.prototype.$disconnect = function(connection) {
return this.dialect.connectionManager.disconnect(connection); return this.dialect.connectionManager.disconnect(connection);
......
...@@ -65,6 +65,7 @@ var hookTypes = { ...@@ -65,6 +65,7 @@ var hookTypes = {
afterDefine: {params: 1, sync: true}, afterDefine: {params: 1, sync: true},
beforeInit: {params: 2, sync: true}, beforeInit: {params: 2, sync: true},
afterInit: {params: 1, sync: true}, afterInit: {params: 1, sync: true},
beforeConnect: {params: 1},
beforeSync: {params: 1}, beforeSync: {params: 1},
afterSync: {params: 1}, afterSync: {params: 1},
beforeBulkSync: {params: 1}, beforeBulkSync: {params: 1},
...@@ -75,7 +76,8 @@ var hookAliases = { ...@@ -75,7 +76,8 @@ var hookAliases = {
beforeDelete: 'beforeDestroy', beforeDelete: 'beforeDestroy',
afterDelete: 'afterDestroy', afterDelete: 'afterDestroy',
beforeBulkDelete: 'beforeBulkDestroy', beforeBulkDelete: 'beforeBulkDestroy',
afterBulkDelete: 'afterBulkDestroy' afterBulkDelete: 'afterBulkDestroy',
beforeConnection: 'beforeConnect'
}; };
var Hooks = { var Hooks = {
...@@ -421,6 +423,13 @@ Hooks.hasHooks = Hooks.hasHook; ...@@ -421,6 +423,13 @@ Hooks.hasHooks = Hooks.hasHook;
*/ */
/** /**
* A hook that is run before a connection is created
* @param {String} name
* @param {Function} fn A callback function that is called with config passed to connection
* @name beforeConnect
*/
/**
* A hook that is run before Model.sync call * A hook that is run before Model.sync call
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with options passed to Model.sync * @param {Function} fn A callback function that is called with options passed to Model.sync
......
'use strict';
/* jshint -W030 */
var chai = require('chai')
, sinon = require('sinon')
, expect = chai.expect
, Support = require(__dirname + '/support')
, Sequelize = require(__dirname + '/../../index')
, ConnectionManager = require(__dirname + '/../../lib/dialects/abstract/connection-manager')
, Promise = Sequelize.Promise;
describe('connection manager', function () {
describe('$connect', function () {
beforeEach(function () {
this.sinon = sinon.sandbox.create();
this.dialect = {
connectionManager: {
connect: this.sinon.stub().returns(Promise.resolve())
}
};
this.sequelize = Support.createSequelizeInstance();
});
afterEach(function () {
this.sinon.restore();
});
it('should resolve connection on dialect connection manager', function () {
var connection = {};
this.dialect.connectionManager.connect.returns(Promise.resolve(connection));
var connectionManager = new ConnectionManager(this.dialect, this.sequelize);
var config = {};
return expect(connectionManager.$connect(config)).to.eventually.equal(connection).then(function () {
expect(this.dialect.connectionManager.connect).to.have.been.calledWith(config);
}.bind(this));
});
it('should let beforeConnect hook modify config', function () {
var username = Math.random().toString()
, password = Math.random().toString();
this.sequelize.beforeConnect(function (config) {
config.username = username;
config.password = password;
return config;
});
var connectionManager = new ConnectionManager(this.dialect, this.sequelize);
return connectionManager.$connect({}).then(function () {
expect(this.dialect.connectionManager.connect).to.have.been.calledWith({
username: username,
password: password
});
}.bind(this));
});
});
});
...@@ -2,9 +2,7 @@ ...@@ -2,9 +2,7 @@
/* jshint -W030 */ /* jshint -W030 */
var chai = require('chai') var chai = require('chai')
, sinon = require('sinon')
, expect = chai.expect , expect = chai.expect
, _ = require('lodash')
, Sequelize = require(__dirname + '/../../index'); , Sequelize = require(__dirname + '/../../index');
describe('sequelize constructor', function () { describe('sequelize constructor', function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!