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

Commit 4214143e by Jan Aagaard Meier

add: afterConnect hook

1 parent 9b204af6
...@@ -231,7 +231,8 @@ class ConnectionManager { ...@@ -231,7 +231,8 @@ class ConnectionManager {
_connect(config) { _connect(config) {
return this.sequelize.runHooks('beforeConnect', config) return this.sequelize.runHooks('beforeConnect', config)
.then(() => this.dialect.connectionManager.connect(config)); .then(() => this.dialect.connectionManager.connect(config))
.then(connection => this.sequelize.runHooks('afterConnect', connection, config).return(connection));
} }
_disconnect(connection) { _disconnect(connection) {
...@@ -239,7 +240,7 @@ class ConnectionManager { ...@@ -239,7 +240,7 @@ class ConnectionManager {
} }
_validate(connection) { _validate(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);
} }
} }
......
...@@ -38,6 +38,7 @@ const hookTypes = { ...@@ -38,6 +38,7 @@ const 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},
...@@ -497,6 +498,14 @@ exports.applyTo = applyTo; ...@@ -497,6 +498,14 @@ exports.applyTo = applyTo;
*/ */
/** /**
* 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 * 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
......
...@@ -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!