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 b73bd2db
authored
Apr 01, 2016
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add: beforeConnect hook
1 parent
d894f107
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
78 additions
and
4 deletions
lib/dialects/abstract/connection-manager.js
lib/hooks.js
test/unit/connection-manager.test.js
test/unit/sequelize.test.js
lib/dialects/abstract/connection-manager.js
View file @
b73bd2d
...
@@ -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
);
...
...
lib/hooks.js
View file @
b73bd2d
...
@@ -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
...
...
test/unit/connection-manager.test.js
0 → 100644
View file @
b73bd2d
'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
));
});
});
});
test/unit/sequelize.test.js
View file @
b73bd2d
...
@@ -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
()
{
...
...
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