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 9877aa89
authored
Sep 20, 2014
by
overlookmotel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add beforeFindAfterOptions hook
1 parent
f889d6f0
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
0 deletions
lib/hooks.js
lib/model.js
test/hooks.test.js
lib/hooks.js
View file @
9877aa8
...
@@ -52,6 +52,7 @@ var hookTypes = {
...
@@ -52,6 +52,7 @@ var hookTypes = {
afterBulkUpdate
:
{
params
:
1
},
afterBulkUpdate
:
{
params
:
1
},
beforeFind
:
{
params
:
1
},
beforeFind
:
{
params
:
1
},
beforeFindAfterExpandIncludeAll
:
{
params
:
1
},
beforeFindAfterExpandIncludeAll
:
{
params
:
1
},
beforeFindAfterOptions
:
{
params
:
1
},
afterFind
:
{
params
:
2
}
afterFind
:
{
params
:
2
}
};
};
var
hookAliases
=
{
var
hookAliases
=
{
...
@@ -313,6 +314,15 @@ Hooks.beforeFindAfterExpandIncludeAll = function(name, fn) {
...
@@ -313,6 +314,15 @@ Hooks.beforeFindAfterExpandIncludeAll = function(name, fn) {
};
};
/**
/**
* A hook that is run before a find (select) query, after all option parsing is complete
* @param {String} name
* @param {Function} fn A callback function that is called with options, callback(err)
*/
Hooks
.
beforeFindAfterOptions
=
function
(
name
,
fn
)
{
return
Hooks
.
addHook
.
call
(
this
,
'beforeFindAfterOptions'
,
name
,
fn
);
};
/**
* A hook that is run after a find (select) query
* A hook that is run after a find (select) query
* @param {String} name
* @param {String} name
* @param {Function} fn A callback function that is called with instance(s), options, callback(err)
* @param {Function} fn A callback function that is called with instance(s), options, callback(err)
...
...
lib/model.js
View file @
9877aa8
...
@@ -735,6 +735,10 @@ module.exports = (function() {
...
@@ -735,6 +735,10 @@ module.exports = (function() {
options
=
paranoidClause
.
call
(
this
,
options
);
options
=
paranoidClause
.
call
(
this
,
options
);
if
(
options
.
hooks
)
{
return
this
.
runHooks
(
'beforeFindAfterOptions'
,
options
);
}
}).
then
(
function
()
{
return
this
.
QueryInterface
.
select
(
this
,
this
.
getTableName
(),
options
,
Utils
.
_
.
defaults
({
return
this
.
QueryInterface
.
select
(
this
,
this
.
getTableName
(),
options
,
Utils
.
_
.
defaults
({
type
:
QueryTypes
.
SELECT
,
type
:
QueryTypes
.
SELECT
,
hasJoin
:
hasJoin
,
hasJoin
:
hasJoin
,
...
...
test/hooks.test.js
View file @
9877aa8
...
@@ -4201,6 +4201,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
...
@@ -4201,6 +4201,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
it
(
'all hooks run'
,
function
(
done
)
{
it
(
'all hooks run'
,
function
(
done
)
{
var
beforeHook
=
false
var
beforeHook
=
false
,
beforeHook2
=
false
,
beforeHook2
=
false
,
beforeHook3
=
false
,
afterHook
=
false
;
,
afterHook
=
false
;
this
.
User
.
beforeFind
(
function
(
options
)
{
this
.
User
.
beforeFind
(
function
(
options
)
{
...
@@ -4211,6 +4212,10 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
...
@@ -4211,6 +4212,10 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
beforeHook2
=
true
;
beforeHook2
=
true
;
});
});
this
.
User
.
beforeFindAfterOptions
(
function
(
options
)
{
beforeHook3
=
true
;
});
this
.
User
.
afterFind
(
function
(
users
,
options
)
{
this
.
User
.
afterFind
(
function
(
users
,
options
)
{
afterHook
=
true
;
afterHook
=
true
;
});
});
...
@@ -4219,6 +4224,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
...
@@ -4219,6 +4224,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
expect
(
user
.
mood
).
to
.
equal
(
'happy'
);
expect
(
user
.
mood
).
to
.
equal
(
'happy'
);
expect
(
beforeHook
).
to
.
be
.
true
;
expect
(
beforeHook
).
to
.
be
.
true
;
expect
(
beforeHook2
).
to
.
be
.
true
;
expect
(
beforeHook2
).
to
.
be
.
true
;
expect
(
beforeHook3
).
to
.
be
.
true
;
expect
(
afterHook
).
to
.
be
.
true
;
expect
(
afterHook
).
to
.
be
.
true
;
done
();
done
();
});
});
...
@@ -4246,6 +4252,17 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
...
@@ -4246,6 +4252,17 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
});
});
});
});
it
(
'beforeFindAfterOptions hook can change options'
,
function
(
done
)
{
this
.
User
.
beforeFindAfterOptions
(
function
(
options
)
{
options
.
where
.
username
=
'joe'
;
});
this
.
User
.
find
({
where
:
{
username
:
'adam'
}}).
then
(
function
(
user
)
{
expect
(
user
.
mood
).
to
.
equal
(
'sad'
);
done
();
});
});
it
(
'afterFind hook can change results'
,
function
(
done
)
{
it
(
'afterFind hook can change results'
,
function
(
done
)
{
this
.
User
.
afterFind
(
function
(
user
,
options
)
{
this
.
User
.
afterFind
(
function
(
user
,
options
)
{
user
.
mood
=
'sad'
;
user
.
mood
=
'sad'
;
...
@@ -4281,6 +4298,17 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
...
@@ -4281,6 +4298,17 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
});
});
});
});
it
(
'in beforeFindAfterOptions hook returns error'
,
function
(
done
)
{
this
.
User
.
beforeFindAfterOptions
(
function
(
options
)
{
throw
new
Error
(
'Oops!'
);
});
this
.
User
.
find
({
where
:
{
username
:
'adam'
}}).
catch
(
function
(
err
)
{
expect
(
err
.
message
).
to
.
equal
(
'Oops!'
);
done
();
});
});
it
(
'in afterFind hook returns error'
,
function
(
done
)
{
it
(
'in afterFind hook returns error'
,
function
(
done
)
{
this
.
User
.
afterFind
(
function
(
options
)
{
this
.
User
.
afterFind
(
function
(
options
)
{
throw
new
Error
(
'Oops!'
);
throw
new
Error
(
'Oops!'
);
...
...
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