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

Commit 02bceaa3 by Jan Aagaard Meier

add: afterConnect hook

1 parent 4f95f176
# Future # Future
- [FIXED] Soft-delete not returning number of affected rows on mssql [#6916](https://github.com/sequelize/sequelize/pull/6916) - [FIXED] Soft-delete not returning number of affected rows on mssql [#6916](https://github.com/sequelize/sequelize/pull/6916)
- [ADDED] `afterConnect` hook
# 3.27.0 # 3.27.0
- [FIXED] Incorrect column name for generateThroughJoin [#6878](https://github.com/sequelize/sequelize/pull/6878) - [FIXED] Incorrect column name for generateThroughJoin [#6878](https://github.com/sequelize/sequelize/pull/6878)
......
...@@ -244,9 +244,9 @@ ConnectionManager.prototype.releaseConnection = function(connection) { ...@@ -244,9 +244,9 @@ ConnectionManager.prototype.releaseConnection = function(connection) {
ConnectionManager.prototype.$connect = function(config) { ConnectionManager.prototype.$connect = function(config) {
return this.sequelize.runHooks('beforeConnect', config).bind(this).then(function () { return this.sequelize.runHooks('beforeConnect', config).bind(this).then(function () {
return this.dialect.connectionManager.connect(config).then(function (connection) { return this.dialect.connectionManager.connect(config);
return connection; }).then(function(connection) {
}); return this.sequelize.runHooks('afterConnect', connection, config).return(connection)
}); });
}; };
ConnectionManager.prototype.$disconnect = function(connection) { ConnectionManager.prototype.$disconnect = function(connection) {
...@@ -254,7 +254,7 @@ ConnectionManager.prototype.$disconnect = function(connection) { ...@@ -254,7 +254,7 @@ ConnectionManager.prototype.$disconnect = function(connection) {
}; };
ConnectionManager.prototype.$validate = function(connection) { ConnectionManager.prototype.$validate = function(connection) {
if (!this.dialect.connectionManager.validate) return Promise.resolve(); if (!this.dialect.connectionManager.validate) return true
return this.dialect.connectionManager.validate(connection); return this.dialect.connectionManager.validate(connection);
}; };
......
...@@ -66,6 +66,7 @@ var hookTypes = { ...@@ -66,6 +66,7 @@ var hookTypes = {
beforeInit: {params: 2, sync: true}, beforeInit: {params: 2, sync: true},
afterInit: {params: 1, sync: true}, afterInit: {params: 1, sync: true},
beforeConnect: {params: 1}, beforeConnect: {params: 1},
afterConnect: {params: 2},
beforeSync: {params: 1}, beforeSync: {params: 1},
afterSync: {params: 1}, afterSync: {params: 1},
beforeBulkSync: {params: 1}, beforeBulkSync: {params: 1},
...@@ -420,6 +421,31 @@ Hooks.hasHooks = Hooks.hasHook; ...@@ -420,6 +421,31 @@ Hooks.hasHooks = Hooks.hasHook;
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with sequelize * @param {Function} fn A callback function that is called with sequelize
* @name afterInit * @name afterInit
* @memberOf Sequelize
*/
/**
* 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
* @memberOf Sequelize
*/
/**
* A hook that is run after a connection is created
* @param {String} name
* @param {Function} fn A callback function that is called with the connection object and thye config passed to connection
* @name afterConnect
* @memberOf Sequelize
*/
/**
* A hook that is run before Model.sync call
* @param {String} name
* @param {Function} fn A callback function that is called with options passed to Model.sync
* @name beforeSync
* @memberOf Sequelize
*/ */
/** /**
......
...@@ -13,10 +13,11 @@ describe('connection manager', function () { ...@@ -13,10 +13,11 @@ describe('connection manager', function () {
describe('$connect', function () { describe('$connect', function () {
beforeEach(function () { beforeEach(function () {
this.sinon = sinon.sandbox.create(); this.sinon = sinon.sandbox.create();
this.connection = {};
this.dialect = { this.dialect = {
connectionManager: { connectionManager: {
connect: this.sinon.stub().returns(Promise.resolve()) connect: this.sinon.stub().returns(Promise.resolve(this.connection))
} }
}; };
...@@ -59,5 +60,18 @@ describe('connection manager', function () { ...@@ -59,5 +60,18 @@ describe('connection manager', function () {
}); });
}.bind(this)); }.bind(this));
}); });
it('should call afterConnect', function() {
const spy = sinon.spy();
this.sequelize.afterConnect(spy);
var connectionManager = new ConnectionManager(this.dialect, this.sequelize);
return connectionManager.$connect({}).then(() => {
expect(spy.callCount).to.equal(1);
expect(spy.firstCall.args[0]).to.equal(this.connection);
expect(spy.firstCall.args[1]).to.eql({});
});
});
}); });
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!