Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 4214143e
authored
Dec 20, 2016
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add: afterConnect hook
1 parent
9b204af6
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
3 deletions
lib/dialects/abstract/connection-manager.js
lib/hooks.js
test/unit/connection-manager.test.js
lib/dialects/abstract/connection-manager.js
View file @
4214143
...
...
@@ -231,7 +231,8 @@ class ConnectionManager {
_connect
(
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
)
{
...
...
@@ -239,7 +240,7 @@ class ConnectionManager {
}
_validate
(
connection
)
{
if
(
!
this
.
dialect
.
connectionManager
.
validate
)
return
Promise
.
resolve
()
;
if
(
!
this
.
dialect
.
connectionManager
.
validate
)
return
true
;
return
this
.
dialect
.
connectionManager
.
validate
(
connection
);
}
}
...
...
lib/hooks.js
View file @
4214143
...
...
@@ -38,6 +38,7 @@ const hookTypes = {
beforeInit
:
{
params
:
2
,
sync
:
true
},
afterInit
:
{
params
:
1
,
sync
:
true
},
beforeConnect
:
{
params
:
1
},
afterConnect
:
{
params
:
2
},
beforeSync
:
{
params
:
1
},
afterSync
:
{
params
:
1
},
beforeBulkSync
:
{
params
:
1
},
...
...
@@ -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
* @param {String} name
* @param {Function} fn A callback function that is called with options passed to Model.sync
...
...
test/unit/connection-manager.test.js
View file @
4214143
...
...
@@ -13,10 +13,11 @@ describe('connection manager', function () {
describe
(
'_connect'
,
function
()
{
beforeEach
(
function
()
{
this
.
sinon
=
sinon
.
sandbox
.
create
();
this
.
connection
=
{};
this
.
dialect
=
{
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 () {
});
}.
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
({});
});
});
});
});
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment