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 6cb715f1
authored
May 19, 2015
by
Francisco Cardoso
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add hability to use raw queries in Model scopes
1 parent
c59268fd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
2 deletions
lib/model.js
test/unit/model/scope.test.js
lib/model.js
View file @
6cb715f
...
@@ -593,7 +593,7 @@ module.exports = (function() {
...
@@ -593,7 +593,7 @@ module.exports = (function() {
if
(
!!
scope
)
{
if
(
!!
scope
)
{
_
.
assign
(
self
.
$scope
,
scope
,
function
scopeCustomizer
(
objectValue
,
sourceValue
,
key
)
{
_
.
assign
(
self
.
$scope
,
scope
,
function
scopeCustomizer
(
objectValue
,
sourceValue
,
key
)
{
if
(
key
===
'where'
)
{
if
(
key
===
'where'
)
{
return
_
.
assign
(
objectValue
||
{},
sourceValue
);
return
Array
.
isArray
(
sourceValue
)
?
sourceValue
:
_
.
assign
(
objectValue
||
{},
sourceValue
);
}
else
if
(
key
===
'include'
&&
Array
.
isArray
(
objectValue
)
&&
Array
.
isArray
(
sourceValue
))
{
}
else
if
(
key
===
'include'
&&
Array
.
isArray
(
objectValue
)
&&
Array
.
isArray
(
sourceValue
))
{
return
objectValue
.
concat
(
sourceValue
);
return
objectValue
.
concat
(
sourceValue
);
}
}
...
...
test/unit/model/scope.test.js
View file @
6cb715f
...
@@ -12,6 +12,11 @@ describe(Support.getTestDialectTeaser('Model'), function() {
...
@@ -12,6 +12,11 @@ describe(Support.getTestDialectTeaser('Model'), function() {
,
Company
;
,
Company
;
var
scopes
=
{
var
scopes
=
{
complexFunction
:
function
(
value
)
{
return
{
where
:
[(
value
+
' IN (SELECT foobar FROM some_sql_function(foo.bar))'
)]
};
},
somethingTrue
:
{
somethingTrue
:
{
where
:
{
where
:
{
something
:
true
,
something
:
true
,
...
@@ -91,7 +96,6 @@ describe(Support.getTestDialectTeaser('Model'), function() {
...
@@ -91,7 +96,6 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect
(
scoped2
.
$scope
).
to
.
deep
.
equal
(
scopes
.
somethingFalse
);
expect
(
scoped2
.
$scope
).
to
.
deep
.
equal
(
scopes
.
somethingFalse
);
});
});
it
(
'should work with function scopes'
,
function
()
{
it
(
'should work with function scopes'
,
function
()
{
expect
(
Company
.
scope
({
method
:
[
'actualValue'
,
11
]}).
$scope
).
to
.
deep
.
equal
({
expect
(
Company
.
scope
({
method
:
[
'actualValue'
,
11
]}).
$scope
).
to
.
deep
.
equal
({
where
:
{
where
:
{
...
@@ -128,6 +132,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
...
@@ -128,6 +132,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
});
});
it
(
'should be able to use raw queries'
,
function
()
{
expect
(
Company
.
scope
([{
method
:
[
'complexFunction'
,
'qux'
]}]).
$scope
).
to
.
deep
.
equal
({
where
:
[
'qux IN (SELECT foobar FROM some_sql_function(foo.bar))'
]
});
});
it
(
'should override the default scope'
,
function
()
{
expect
(
Company
.
scope
([
'defaultScope'
,
{
method
:
[
'complexFunction'
,
'qux'
]}]).
$scope
).
to
.
deep
.
equal
({
where
:
[
'qux IN (SELECT foobar FROM some_sql_function(foo.bar))'
]
});
});
it
(
'should emit an error for scopes that dont exist'
,
function
()
{
it
(
'should emit an error for scopes that dont exist'
,
function
()
{
expect
(
function
()
{
expect
(
function
()
{
Company
.
scope
(
'doesntexist'
);
Company
.
scope
(
'doesntexist'
);
...
...
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