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 6174a7cd
authored
Jan 13, 2016
by
Matthew Heck
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add a beforeCount hook
1 parent
5383fa39
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
112 additions
and
2 deletions
lib/hooks.js
lib/model.js
test/integration/hooks.test.js
lib/hooks.js
View file @
6174a7c
...
@@ -59,6 +59,7 @@ var hookTypes = {
...
@@ -59,6 +59,7 @@ var hookTypes = {
beforeFindAfterExpandIncludeAll
:
{
params
:
1
},
beforeFindAfterExpandIncludeAll
:
{
params
:
1
},
beforeFindAfterOptions
:
{
params
:
1
},
beforeFindAfterOptions
:
{
params
:
1
},
afterFind
:
{
params
:
2
},
afterFind
:
{
params
:
2
},
beforeCount
:
{
params
:
1
},
beforeDefine
:
{
params
:
2
,
sync
:
true
},
beforeDefine
:
{
params
:
2
,
sync
:
true
},
afterDefine
:
{
params
:
1
,
sync
:
true
},
afterDefine
:
{
params
:
1
,
sync
:
true
},
beforeInit
:
{
params
:
2
,
sync
:
true
},
beforeInit
:
{
params
:
2
,
sync
:
true
},
...
@@ -376,6 +377,13 @@ Hooks.hasHooks = Hooks.hasHook;
...
@@ -376,6 +377,13 @@ Hooks.hasHooks = Hooks.hasHook;
*/
*/
/**
/**
* A hook that is run before a count query
* @param {String} name
* @param {Function} fn A callback function that is called with options
* @name beforeCount
*/
/**
* A hook that is run before a define call
* A hook that is run before a define call
* @param {String} name
* @param {String} name
* @param {Function} fn A callback function that is called with attributes, options
* @param {Function} fn A callback function that is called with attributes, options
...
...
lib/model.js
View file @
6174a7c
...
@@ -1569,11 +1569,19 @@ Model.prototype.aggregate = function(attribute, aggregateFunction, options) {
...
@@ -1569,11 +1569,19 @@ Model.prototype.aggregate = function(attribute, aggregateFunction, options) {
*/
*/
Model
.
prototype
.
count
=
function
(
options
)
{
Model
.
prototype
.
count
=
function
(
options
)
{
options
=
Utils
.
_
.
clone
(
options
||
{});
options
=
Utils
.
_
.
clone
(
options
||
{});
conformOptions
(
options
,
this
);
this
.
$injectScope
(
options
);
_
.
defaults
(
options
,
{
hooks
:
true
}
);
var
col
=
'*'
;
var
col
=
'*'
;
return
Promise
.
bind
(
this
).
then
(
function
()
{
conformOptions
(
options
,
this
);
this
.
$injectScope
(
options
);
if
(
options
.
hooks
)
{
return
this
.
runHooks
(
'beforeCount'
,
options
);
}
}).
then
(
function
()
{
if
(
options
.
include
)
{
if
(
options
.
include
)
{
col
=
this
.
name
+
'.'
+
this
.
primaryKeyField
;
col
=
this
.
name
+
'.'
+
this
.
primaryKeyField
;
expandIncludeAll
.
call
(
this
,
options
);
expandIncludeAll
.
call
(
this
,
options
);
...
@@ -1591,6 +1599,7 @@ Model.prototype.count = function(options) {
...
@@ -1591,6 +1599,7 @@ Model.prototype.count = function(options) {
options
.
attributes
=
[];
options
.
attributes
=
[];
return
this
.
aggregate
(
col
,
'count'
,
options
);
return
this
.
aggregate
(
col
,
'count'
,
options
);
});
};
};
/**
/**
...
...
test/integration/hooks.test.js
View file @
6174a7c
...
@@ -978,6 +978,99 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
...
@@ -978,6 +978,99 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
});
});
});
});
describe
(
'#count'
,
function
()
{
beforeEach
(
function
()
{
return
this
.
User
.
bulkCreate
([
{
username
:
'adam'
,
mood
:
'happy'
},
{
username
:
'joe'
,
mood
:
'sad'
},
{
username
:
'joe'
,
mood
:
'happy'
}
]);
});
describe
(
'on success'
,
function
()
{
it
(
'hook runs'
,
function
()
{
var
beforeHook
=
false
;
this
.
User
.
beforeCount
(
function
()
{
beforeHook
=
true
;
});
return
this
.
User
.
count
().
then
(
function
(
count
)
{
expect
(
count
).
to
.
equal
(
3
);
expect
(
beforeHook
).
to
.
be
.
true
;
});
});
it
(
'beforeCount hook can change options'
,
function
()
{
this
.
User
.
beforeCount
(
function
(
options
)
{
options
.
where
.
username
=
'adam'
;
});
return
this
.
User
.
count
({
where
:
{
username
:
'joe'
}}).
then
(
function
(
count
)
{
expect
(
count
).
to
.
equal
(
1
);
});
});
});
describe
(
'on error'
,
function
()
{
it
(
'in beforeCount hook returns error'
,
function
()
{
this
.
User
.
beforeCount
(
function
()
{
throw
new
Error
(
'Oops!'
);
});
return
this
.
User
.
find
({
where
:
{
username
:
'adam'
}}).
catch
(
function
(
err
)
{
expect
(
err
.
message
).
to
.
equal
(
'Oops!'
);
});
});
});
});
describe
(
'#count'
,
function
()
{
beforeEach
(
function
()
{
return
this
.
User
.
bulkCreate
([
{
username
:
'adam'
,
mood
:
'happy'
},
{
username
:
'joe'
,
mood
:
'sad'
},
{
username
:
'joe'
,
mood
:
'happy'
}
]);
});
describe
(
'on success'
,
function
()
{
it
(
'hook runs'
,
function
()
{
var
beforeHook
=
false
;
this
.
User
.
beforeCount
(
function
()
{
beforeHook
=
true
;
});
return
this
.
User
.
count
().
then
(
function
(
count
)
{
expect
(
count
).
to
.
equal
(
3
);
expect
(
beforeHook
).
to
.
be
.
true
;
});
});
it
(
'beforeCount hook can change options'
,
function
()
{
this
.
User
.
beforeCount
(
function
(
options
)
{
options
.
where
.
username
=
'adam'
;
});
return
this
.
User
.
count
({
where
:
{
username
:
'joe'
}}).
then
(
function
(
count
)
{
expect
(
count
).
to
.
equal
(
1
);
});
});
});
describe
(
'on error'
,
function
()
{
it
(
'in beforeCount hook returns error'
,
function
()
{
this
.
User
.
beforeCount
(
function
()
{
throw
new
Error
(
'Oops!'
);
});
return
this
.
User
.
find
({
where
:
{
username
:
'adam'
}}).
catch
(
function
(
err
)
{
expect
(
err
.
message
).
to
.
equal
(
'Oops!'
);
});
});
});
});
describe
(
'#define'
,
function
()
{
describe
(
'#define'
,
function
()
{
before
(
function
()
{
before
(
function
()
{
this
.
sequelize
.
addHook
(
'beforeDefine'
,
function
(
attributes
,
options
)
{
this
.
sequelize
.
addHook
(
'beforeDefine'
,
function
(
attributes
,
options
)
{
...
...
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