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 4516bebd
authored
Dec 29, 2015
by
Verdier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Inject has-one scope in where clause
1 parent
2f7c717f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
10 deletions
lib/associations/has-one.js
test/integration/associations/scope.test.js
test/integration/model/scope/associations.test.js
lib/associations/has-one.js
View file @
4516beb
...
...
@@ -21,6 +21,7 @@ var HasOne = function(srcModel, targetModel, options) {
this
.
source
=
srcModel
;
this
.
target
=
targetModel
;
this
.
options
=
options
;
this
.
scope
=
options
.
scope
;
this
.
isSingleAssociation
=
true
;
this
.
isSelfAssociation
=
(
this
.
source
===
this
.
target
);
this
.
as
=
this
.
options
.
as
;
...
...
test/integration/associations/scope.test.js
View file @
4516beb
...
...
@@ -17,7 +17,8 @@ describe(Support.getTestDialectTeaser('associations'), function() {
this
.
Comment
=
this
.
sequelize
.
define
(
'comment'
,
{
title
:
Sequelize
.
STRING
,
commentable
:
Sequelize
.
STRING
,
commentable_id
:
Sequelize
.
INTEGER
commentable_id
:
Sequelize
.
INTEGER
,
isMain
:
Sequelize
.
BOOLEAN
},
{
instanceMethods
:
{
getItem
:
function
()
{
...
...
@@ -29,12 +30,26 @@ describe(Support.getTestDialectTeaser('associations'), function() {
this
.
Post
.
addScope
(
'withComments'
,
{
include
:
[
this
.
Comment
]
});
this
.
Post
.
addScope
(
'withMainComment'
,
{
include
:
[{
model
:
this
.
Comment
,
as
:
'mainComment'
}]
});
this
.
Post
.
hasMany
(
this
.
Comment
,
{
foreignKey
:
'commentable_id'
,
scope
:
{
commentable
:
'post'
}
});
this
.
Post
.
hasOne
(
this
.
Comment
,
{
foreignKey
:
'commentable_id'
,
as
:
'mainComment'
,
scope
:
{
commentable
:
'post'
,
isMain
:
true
}
});
this
.
Comment
.
belongsTo
(
this
.
Post
,
{
foreignKey
:
'commentable_id'
,
as
:
'post'
...
...
@@ -63,6 +78,41 @@ describe(Support.getTestDialectTeaser('associations'), function() {
});
});
describe
(
'1:1'
,
function
()
{
it
(
'should create, find and include associations with scope values'
,
function
()
{
var
self
=
this
;
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
Promise
.
join
(
self
.
Post
.
create
(),
self
.
Comment
.
create
({
title
:
'I am a comment'
}),
self
.
Comment
.
create
({
title
:
'I am a main comment'
,
isMain
:
true
})
);
}).
bind
(
this
).
spread
(
function
(
post
)
{
this
.
post
=
post
;
return
post
.
createComment
({
title
:
'I am a post comment'
});
}).
then
(
function
()
{
return
self
.
Post
.
scope
(
'withMainComment'
).
findById
(
this
.
post
.
id
);
}).
then
(
function
(
post
)
{
expect
(
post
.
mainComment
).
to
.
be
.
null
;
return
Promise
.
join
(
post
.
createMainComment
({
title
:
'I am a main post comment'
}),
self
.
Post
.
scope
(
'withMainComment'
).
findById
(
this
.
post
.
id
)
);
}).
spread
(
function
(
mainComment
,
post
)
{
expect
(
post
.
mainComment
.
id
).
to
.
equal
(
mainComment
.
id
);
});
});
});
describe
(
'1:M'
,
function
()
{
it
(
'should create, find and include associations with scope values'
,
function
()
{
var
self
=
this
;
...
...
test/integration/model/scope/associations.test.js
View file @
4516beb
...
...
@@ -86,7 +86,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
where
:
{
active
:
false
}
}
,
}
}
});
...
...
@@ -106,12 +106,12 @@ describe(Support.getTestDialectTeaser('Model'), function() {
this
.
ScopeMe
.
create
({
id
:
4
,
username
:
'fred'
,
email
:
'fred@foobar.com'
,
access_level
:
3
,
other_value
:
7
,
parent_id
:
1
}),
this
.
ScopeMe
.
create
({
id
:
5
,
username
:
'bob'
,
email
:
'bob@foobar.com'
,
access_level
:
1
,
other_value
:
9
,
parent_id
:
5
}),
this
.
Company
.
create
({
id
:
1
,
active
:
true
}),
this
.
Company
.
create
({
id
:
2
,
active
:
false
})
,
this
.
Company
.
create
({
id
:
2
,
active
:
false
})
]);
}).
spread
(
function
(
u1
,
u2
,
u3
,
u4
,
u5
,
c1
,
c2
)
{
return
Promise
.
all
([
c1
.
setUsers
([
u1
,
u2
,
u3
,
u4
]),
c2
.
setUsers
([
u5
])
,
c2
.
setUsers
([
u5
])
]);
});
});
...
...
@@ -178,8 +178,8 @@ describe(Support.getTestDialectTeaser('Model'), function() {
return
this
.
ScopeMe
.
findById
(
1
);
}).
then
(
function
(
user
)
{
return
user
.
getProfile
({
scope
:
false
});
}).
then
(
function
(
pro
ject
)
{
expect
(
pro
ject
).
to
.
be
.
ok
;
}).
then
(
function
(
pro
file
)
{
expect
(
pro
file
).
to
.
be
.
ok
;
});
});
...
...
@@ -217,8 +217,8 @@ describe(Support.getTestDialectTeaser('Model'), function() {
return
this
.
ScopeMe
.
findById
(
1
);
}).
then
(
function
(
user
)
{
return
user
.
getProfile
();
}).
then
(
function
(
pro
ject
)
{
expect
(
pro
ject
).
not
.
to
.
be
.
ok
;
}).
then
(
function
(
pro
file
)
{
expect
(
pro
file
).
not
.
to
.
be
.
ok
;
});
});
...
...
@@ -258,8 +258,8 @@ describe(Support.getTestDialectTeaser('Model'), function() {
return
this
.
ScopeMe
.
findById
(
1
);
}).
then
(
function
(
user
)
{
return
user
.
getProfile
({
scope
:
'notActive'
});
}).
then
(
function
(
pro
ject
)
{
expect
(
pro
ject
).
not
.
to
.
be
.
ok
;
}).
then
(
function
(
pro
file
)
{
expect
(
pro
file
).
not
.
to
.
be
.
ok
;
});
});
...
...
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