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 6876dfcd
authored
Jul 16, 2015
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(has-many): countAssociations
1 parent
ccff0d8f
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
120 additions
and
6 deletions
lib/associations/has-many.js
test/integration/associations/belongs-to-many.test.js
test/integration/associations/has-many.test.js
lib/associations/has-many.js
View file @
6876dfc
...
@@ -156,7 +156,17 @@ var HasMany = function(source, target, options) {
...
@@ -156,7 +156,17 @@ var HasMany = function(source, target, options) {
* @return {Promise}
* @return {Promise}
* @method hasAssociations
* @method hasAssociations
*/
*/
hasAll
:
'has'
+
plural
hasAll
:
'has'
+
plural
,
/**
* Count everything currently associated with this, using an optional where clause
*
* @param {Object} [options]
* @param {Object} [options.where] An optional where clause to limit the associated models
* @param {String|Boolean} [options.scope] Apply a scope on the related model, or remove its default scope by passing false
* @return {Promise<Int>}
* @method countAssociations
*/
count
:
'count'
+
plural
};
};
if
(
this
.
options
.
counterCache
)
{
if
(
this
.
options
.
counterCache
)
{
...
@@ -233,6 +243,22 @@ HasMany.prototype.injectGetter = function(obj) {
...
@@ -233,6 +243,22 @@ HasMany.prototype.injectGetter = function(obj) {
return
Model
.
all
(
options
);
return
Model
.
all
(
options
);
};
};
obj
[
this
.
accessors
.
count
]
=
function
(
options
)
{
var
model
=
association
.
target
,
sequelize
=
model
.
sequelize
;
options
=
association
.
target
.
__optClone
(
options
)
||
{};
options
.
attributes
=
[
[
sequelize
.
fn
(
'COUNT'
,
sequelize
.
col
(
model
.
primaryKeyAttribute
)),
'count'
]
];
options
.
raw
=
true
;
options
.
plain
=
true
;
return
obj
[
association
.
accessors
.
get
].
call
(
this
,
options
).
then
(
function
(
result
)
{
return
parseInt
(
result
.
count
,
10
);
});
};
obj
[
this
.
accessors
.
hasSingle
]
=
obj
[
this
.
accessors
.
hasAll
]
=
function
(
instances
,
options
)
{
obj
[
this
.
accessors
.
hasSingle
]
=
obj
[
this
.
accessors
.
hasAll
]
=
function
(
instances
,
options
)
{
var
where
=
{};
var
where
=
{};
...
...
test/integration/associations/belongs-to-many.test.js
View file @
6876dfc
...
@@ -415,11 +415,22 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
...
@@ -415,11 +415,22 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
beforeEach
(
function
()
{
beforeEach
(
function
()
{
var
self
=
this
;
var
self
=
this
;
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
});
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
this
.
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
DataTypes
.
STRING
,
active
:
DataTypes
.
BOOLEAN
});
username
:
DataTypes
.
STRING
});
this
.
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
DataTypes
.
STRING
,
active
:
DataTypes
.
BOOLEAN
});
this
.
UserTask
=
this
.
sequelize
.
define
(
'UserTask'
,
{
started
:
{
type
:
DataTypes
.
BOOLEAN
,
defaultValue
:
false
}
});
this
.
User
.
belongsToMany
(
this
.
Task
,
{
through
:
'UserTasks'
});
this
.
User
.
belongsToMany
(
this
.
Task
,
{
through
:
this
.
UserTask
});
this
.
Task
.
belongsToMany
(
this
.
User
,
{
through
:
'UserTasks'
});
this
.
Task
.
belongsToMany
(
this
.
User
,
{
through
:
this
.
UserTask
});
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
Promise
.
all
([
return
Promise
.
all
([
...
@@ -449,7 +460,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
...
@@ -449,7 +460,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
it
(
'should count scoped associations'
,
function
()
{
it
(
'should count scoped associations'
,
function
()
{
this
.
User
.
belongsToMany
(
this
.
Task
,
{
this
.
User
.
belongsToMany
(
this
.
Task
,
{
as
:
'activeTasks'
,
as
:
'activeTasks'
,
through
:
'UserTasks'
,
through
:
this
.
UserTask
,
scope
:
{
scope
:
{
active
:
true
active
:
true
}
}
...
@@ -457,6 +468,35 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
...
@@ -457,6 +468,35 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
return
expect
(
this
.
user
.
countActiveTasks
({})).
to
.
eventually
.
equal
(
1
);
return
expect
(
this
.
user
.
countActiveTasks
({})).
to
.
eventually
.
equal
(
1
);
});
});
it
(
'should count scoped through associations'
,
function
()
{
var
user
=
this
.
user
;
this
.
User
.
belongsToMany
(
this
.
Task
,
{
as
:
'startedTasks'
,
through
:
{
model
:
this
.
UserTask
,
scope
:
{
started
:
true
}
}
});
return
Promise
.
join
(
this
.
Task
.
create
().
then
(
function
(
task
)
{
return
user
.
addTask
(
task
,
{
started
:
true
})
}),
this
.
Task
.
create
().
then
(
function
(
task
)
{
return
user
.
addTask
(
task
,
{
started
:
true
})
})
).
then
(
function
()
{
return
expect
(
user
.
countStartedTasks
({})).
to
.
eventually
.
equal
(
2
);
});
});
});
});
describe
(
'setAssociations'
,
function
()
{
describe
(
'setAssociations'
,
function
()
{
...
...
test/integration/associations/has-many.test.js
View file @
6876dfc
...
@@ -634,6 +634,54 @@ describe(Support.getTestDialectTeaser('HasMany'), function() {
...
@@ -634,6 +634,54 @@ describe(Support.getTestDialectTeaser('HasMany'), function() {
});
});
});
});
describe
(
'countAssociations'
,
function
()
{
beforeEach
(
function
()
{
var
self
=
this
;
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
});
this
.
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
DataTypes
.
STRING
,
active
:
DataTypes
.
BOOLEAN
});
this
.
User
.
hasMany
(
self
.
Task
,
{
foreignKey
:
'userId'
});
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
Promise
.
all
([
self
.
User
.
create
({
username
:
'John'
}),
self
.
Task
.
create
({
title
:
'Get rich'
,
active
:
true
}),
self
.
Task
.
create
({
title
:
'Die trying'
,
active
:
false
})
]);
}).
spread
(
function
(
john
,
task1
,
task2
)
{
self
.
user
=
john
;
return
john
.
setTasks
([
task1
,
task2
]);
});
});
it
(
'should count all associations'
,
function
()
{
return
expect
(
this
.
user
.
countTasks
({})).
to
.
eventually
.
equal
(
2
);
});
it
(
'should count filtered associations'
,
function
()
{
return
expect
(
this
.
user
.
countTasks
({
where
:
{
active
:
true
}
})).
to
.
eventually
.
equal
(
1
);
});
it
(
'should count scoped associations'
,
function
()
{
this
.
User
.
hasMany
(
this
.
Task
,
{
foreignKey
:
'userId'
,
as
:
'activeTasks'
,
scope
:
{
active
:
true
}
});
return
expect
(
this
.
user
.
countActiveTasks
({})).
to
.
eventually
.
equal
(
1
);
});
});
describe
(
'selfAssociations'
,
function
()
{
describe
(
'selfAssociations'
,
function
()
{
it
(
'should work with alias'
,
function
()
{
it
(
'should work with alias'
,
function
()
{
var
Person
=
this
.
sequelize
.
define
(
'Group'
,
{});
var
Person
=
this
.
sequelize
.
define
(
'Group'
,
{});
...
...
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