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 3780d3b7
authored
Feb 10, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1365 from overlookmotel/order-by-associations-refactor
Order by associations refactor
2 parents
dc07afa3
8210ddef
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
73 additions
and
96 deletions
lib/associations/belongs-to.js
lib/associations/has-many.js
lib/associations/has-one.js
lib/associations/mixin.js
lib/dao-factory.js
lib/dialects/abstract/query-generator.js
test/dao-factory/find.test.js
lib/associations/belongs-to.js
View file @
3780d3b
...
@@ -17,8 +17,10 @@ module.exports = (function() {
...
@@ -17,8 +17,10 @@ module.exports = (function() {
this
.
options
.
foreignKey
=
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
,
this
.
source
.
options
.
language
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
this
.
options
.
foreignKey
=
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
,
this
.
source
.
options
.
language
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
}
}
if
(
!
this
.
as
)
{
if
(
this
.
as
)
{
this
.
as
=
this
.
options
.
as
=
Utils
.
singularize
(
this
.
target
.
tableName
,
this
.
target
.
options
.
language
)
this
.
isAliased
=
true
}
else
{
this
.
as
=
Utils
.
singularize
(
this
.
target
.
tableName
,
this
.
target
.
options
.
language
)
}
}
this
.
associationAccessor
=
this
.
isSelfAssociation
this
.
associationAccessor
=
this
.
isSelfAssociation
...
...
lib/associations/has-many.js
View file @
3780d3b
...
@@ -21,9 +21,10 @@ module.exports = (function() {
...
@@ -21,9 +21,10 @@ module.exports = (function() {
this
.
isMultiAssociation
=
true
this
.
isMultiAssociation
=
true
this
.
isSelfAssociation
=
this
.
source
===
this
.
target
this
.
isSelfAssociation
=
this
.
source
===
this
.
target
this
.
doubleLinked
=
false
this
.
doubleLinked
=
false
this
.
as
=
this
.
options
.
as
this
.
combinedTableName
=
Utils
.
combineTableNames
(
this
.
combinedTableName
=
Utils
.
combineTableNames
(
this
.
source
.
tableName
,
this
.
source
.
tableName
,
this
.
isSelfAssociation
?
(
this
.
options
.
as
||
this
.
target
.
tableName
)
:
this
.
target
.
tableName
this
.
isSelfAssociation
?
(
this
.
as
||
this
.
target
.
tableName
)
:
this
.
target
.
tableName
)
)
/*
/*
...
@@ -48,7 +49,7 @@ module.exports = (function() {
...
@@ -48,7 +49,7 @@ module.exports = (function() {
* Determine associationAccessor, especially for include options to identify the correct model
* Determine associationAccessor, especially for include options to identify the correct model
*/
*/
this
.
associationAccessor
=
this
.
options
.
as
this
.
associationAccessor
=
this
.
as
if
(
!
this
.
associationAccessor
&&
(
typeof
this
.
through
===
"string"
||
Object
(
this
.
through
)
===
this
.
through
))
{
if
(
!
this
.
associationAccessor
&&
(
typeof
this
.
through
===
"string"
||
Object
(
this
.
through
)
===
this
.
through
))
{
this
.
associationAccessor
=
this
.
through
.
tableName
||
this
.
through
this
.
associationAccessor
=
this
.
through
.
tableName
||
this
.
through
}
}
...
@@ -112,16 +113,20 @@ module.exports = (function() {
...
@@ -112,16 +113,20 @@ module.exports = (function() {
this
.
options
.
tableName
=
this
.
combinedName
=
(
this
.
through
===
Object
(
this
.
through
)
?
this
.
through
.
tableName
:
this
.
through
)
this
.
options
.
tableName
=
this
.
combinedName
=
(
this
.
through
===
Object
(
this
.
through
)
?
this
.
through
.
tableName
:
this
.
through
)
var
as
=
(
this
.
options
.
as
||
Utils
.
pluralize
(
this
.
target
.
tableName
,
this
.
target
.
options
.
language
))
if
(
this
.
as
)
{
this
.
isAliased
=
true
}
else
{
this
.
as
=
Utils
.
pluralize
(
this
.
target
.
tableName
,
this
.
target
.
options
.
language
)
}
this
.
accessors
=
{
this
.
accessors
=
{
get
:
Utils
.
_
.
camelize
(
'get_'
+
as
),
get
:
Utils
.
_
.
camelize
(
'get_'
+
this
.
as
),
set
:
Utils
.
_
.
camelize
(
'set_'
+
as
),
set
:
Utils
.
_
.
camelize
(
'set_'
+
this
.
as
),
add
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'add_'
+
as
,
this
.
target
.
options
.
language
)),
add
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'add_'
+
this
.
as
,
this
.
target
.
options
.
language
)),
create
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'create_'
+
as
,
this
.
target
.
options
.
language
)),
create
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'create_'
+
this
.
as
,
this
.
target
.
options
.
language
)),
remove
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'remove_'
+
as
,
this
.
target
.
options
.
language
)),
remove
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'remove_'
+
this
.
as
,
this
.
target
.
options
.
language
)),
hasSingle
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'has_'
+
as
,
this
.
target
.
options
.
language
)),
hasSingle
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'has_'
+
this
.
as
,
this
.
target
.
options
.
language
)),
hasAll
:
Utils
.
_
.
camelize
(
'has_'
+
as
)
hasAll
:
Utils
.
_
.
camelize
(
'has_'
+
this
.
as
)
}
}
}
}
...
...
lib/associations/has-one.js
View file @
3780d3b
...
@@ -11,25 +11,28 @@ module.exports = (function() {
...
@@ -11,25 +11,28 @@ module.exports = (function() {
this
.
options
=
options
this
.
options
=
options
this
.
isSingleAssociation
=
true
this
.
isSingleAssociation
=
true
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
==
this
.
target
.
tableName
)
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
==
this
.
target
.
tableName
)
this
.
as
=
this
.
options
.
as
if
(
this
.
isSelfAssociation
&&
!
this
.
options
.
foreignKey
&&
!!
this
.
options
.
as
)
{
if
(
this
.
isSelfAssociation
&&
!
this
.
options
.
foreignKey
&&
!!
this
.
as
)
{
this
.
options
.
foreignKey
=
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
options
.
as
,
this
.
target
.
options
.
language
)
+
"Id"
,
this
.
options
.
underscored
)
this
.
options
.
foreignKey
=
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
as
,
this
.
target
.
options
.
language
)
+
"Id"
,
this
.
options
.
underscored
)
}
}
if
(
!
this
.
options
.
as
)
{
if
(
this
.
as
)
{
this
.
options
.
as
=
Utils
.
singularize
(
this
.
target
.
tableName
,
this
.
target
.
options
.
language
)
this
.
isAliased
=
true
}
else
{
this
.
as
=
Utils
.
singularize
(
this
.
target
.
tableName
,
this
.
target
.
options
.
language
)
}
}
this
.
associationAccessor
=
this
.
isSelfAssociation
this
.
associationAccessor
=
this
.
isSelfAssociation
?
Utils
.
combineTableNames
(
this
.
target
.
tableName
,
this
.
options
.
as
)
?
Utils
.
combineTableNames
(
this
.
target
.
tableName
,
this
.
as
)
:
this
.
options
.
as
:
this
.
as
this
.
options
.
useHooks
=
options
.
useHooks
this
.
options
.
useHooks
=
options
.
useHooks
this
.
accessors
=
{
this
.
accessors
=
{
get
:
Utils
.
_
.
camelize
(
'get_'
+
this
.
options
.
as
),
get
:
Utils
.
_
.
camelize
(
'get_'
+
this
.
as
),
set
:
Utils
.
_
.
camelize
(
'set_'
+
this
.
options
.
as
),
set
:
Utils
.
_
.
camelize
(
'set_'
+
this
.
as
),
create
:
Utils
.
_
.
camelize
(
'create_'
+
this
.
options
.
as
)
create
:
Utils
.
_
.
camelize
(
'create_'
+
this
.
as
)
}
}
}
}
...
...
lib/associations/mixin.js
View file @
3780d3b
...
@@ -59,36 +59,32 @@ Mixin.hasMany = function(associatedDAOFactory, options) {
...
@@ -59,36 +59,32 @@ Mixin.hasMany = function(associatedDAOFactory, options) {
return
this
return
this
}
}
Mixin
.
getAssociation
=
function
(
target
)
{
Mixin
.
getAssociation
=
function
(
target
,
alias
)
{
var
result
=
null
for
(
var
associationName
in
this
.
associations
)
{
for
(
var
associationName
in
this
.
associations
)
{
if
(
this
.
associations
.
hasOwnProperty
(
associationName
))
{
if
(
this
.
associations
.
hasOwnProperty
(
associationName
))
{
var
association
=
this
.
associations
[
associationName
]
var
association
=
this
.
associations
[
associationName
]
if
(
!
result
&&
(
association
.
target
===
target
))
{
if
(
association
.
target
===
target
&&
(
alias
===
undefined
?
!
association
.
isAliased
:
association
.
as
===
alias
))
{
re
sult
=
association
re
turn
association
}
}
}
}
}
}
return
result
return
null
}
}
Mixin
.
getAssociationByAlias
=
function
(
alias
)
{
Mixin
.
getAssociationByAlias
=
function
(
alias
)
{
var
result
=
null
for
(
var
associationName
in
this
.
associations
)
{
for
(
var
associationName
in
this
.
associations
)
{
if
(
this
.
associations
.
hasOwnProperty
(
associationName
))
{
if
(
this
.
associations
.
hasOwnProperty
(
associationName
))
{
var
association
=
this
.
associations
[
associationName
]
var
association
=
this
.
associations
[
associationName
]
if
(
!
result
&&
(
association
.
options
.
as
===
alias
)
)
{
if
(
association
.
as
===
alias
)
{
re
sult
=
association
re
turn
association
}
}
}
}
}
}
return
result
return
null
}
}
/* example for instance methods:
/* example for instance methods:
...
...
lib/dao-factory.js
View file @
3780d3b
...
@@ -1349,21 +1349,22 @@ module.exports = (function() {
...
@@ -1349,21 +1349,22 @@ module.exports = (function() {
var
validateIncludedElement
=
function
(
include
,
parent
)
{
var
validateIncludedElement
=
function
(
include
,
parent
)
{
if
(
include
instanceof
DAOFactory
)
{
if
(
include
instanceof
DAOFactory
)
{
include
=
{
daoFactory
:
include
,
as
:
include
.
tableName
}
include
=
{
daoFactory
:
include
}
}
}
if
(
typeof
parent
===
"undefined"
)
{
if
(
typeof
parent
===
"undefined"
)
{
parent
=
this
parent
=
this
}
}
if
(
typeof
include
===
'object'
)
{
if
(
typeof
include
!==
'object'
)
{
throw
new
Error
(
'Include unexpected. Element has to be either an instance of DAOFactory or an object.'
)
}
if
(
include
.
hasOwnProperty
(
'model'
))
{
if
(
include
.
hasOwnProperty
(
'model'
))
{
include
.
daoFactory
=
include
.
model
include
.
daoFactory
=
include
.
model
delete
include
.
model
delete
include
.
model
}
}
else
if
(
!
include
.
hasOwnProperty
(
'daoFactory'
))
{
throw
new
Error
(
'Include malformed. Expected attributes: daoFactory, as!'
)
if
(
!
include
.
hasOwnProperty
(
'as'
))
{
include
.
as
=
include
.
daoFactory
.
tableName
}
}
if
(
include
.
hasOwnProperty
(
'attributes'
))
{
if
(
include
.
hasOwnProperty
(
'attributes'
))
{
...
@@ -1384,18 +1385,11 @@ module.exports = (function() {
...
@@ -1384,18 +1385,11 @@ module.exports = (function() {
// pseudo include just needed the attribute logic, return
// pseudo include just needed the attribute logic, return
if
(
include
.
_pseudo
)
return
include
if
(
include
.
_pseudo
)
return
include
if
(
include
.
hasOwnProperty
(
'daoFactory'
)
&&
(
include
.
hasOwnProperty
(
'as'
)))
{
var
usesAlias
=
(
include
.
as
!==
include
.
daoFactory
.
tableName
)
,
association
=
(
usesAlias
?
parent
.
getAssociationByAlias
(
include
.
as
)
:
parent
.
getAssociation
(
include
.
daoFactory
))
// If single (1:1) association, we singularize the alias, so it will match the automatically generated alias of belongsTo/HasOne
if
(
association
&&
!
usesAlias
&&
association
.
isSingleAssociation
)
{
include
.
as
=
Utils
.
singularize
(
include
.
daoFactory
.
tableName
,
include
.
daoFactory
.
options
.
language
)
}
// check if the current daoFactory is actually associated with the passed daoFactory - or it's a pseudo include
// check if the current daoFactory is actually associated with the passed daoFactory - or it's a pseudo include
if
(
association
&&
(
!
association
.
options
.
as
||
(
association
.
options
.
as
===
include
.
as
)))
{
var
association
=
parent
.
getAssociation
(
include
.
daoFactory
,
include
.
as
)
if
(
association
)
{
include
.
association
=
association
include
.
association
=
association
include
.
as
=
association
.
as
// If through, we create a pseudo child include, to ease our parsing later on
// If through, we create a pseudo child include, to ease our parsing later on
if
(
Object
(
include
.
association
.
through
)
===
include
.
association
.
through
)
{
if
(
Object
(
include
.
association
.
through
)
===
include
.
association
.
through
)
{
...
@@ -1415,10 +1409,7 @@ module.exports = (function() {
...
@@ -1415,10 +1409,7 @@ module.exports = (function() {
}
}
if
(
include
.
required
===
undefined
)
{
if
(
include
.
required
===
undefined
)
{
include
.
required
=
false
include
.
required
=
!!
include
.
where
if
(
include
.
where
)
{
include
.
required
=
true
}
}
}
// Validate child includes
// Validate child includes
...
@@ -1430,7 +1421,7 @@ module.exports = (function() {
...
@@ -1430,7 +1421,7 @@ module.exports = (function() {
}
else
{
}
else
{
var
msg
=
include
.
daoFactory
.
name
var
msg
=
include
.
daoFactory
.
name
if
(
usesAli
as
)
{
if
(
include
.
as
)
{
msg
+=
" ("
+
include
.
as
+
")"
msg
+=
" ("
+
include
.
as
+
")"
}
}
...
@@ -1438,12 +1429,6 @@ module.exports = (function() {
...
@@ -1438,12 +1429,6 @@ module.exports = (function() {
throw
new
Error
(
msg
)
throw
new
Error
(
msg
)
}
}
}
else
{
throw
new
Error
(
'Include malformed. Expected attributes: daoFactory, as!'
)
}
}
else
{
throw
new
Error
(
'Include unexpected. Element has to be either an instance of DAOFactory or an object.'
)
}
}
}
var
replaceReferencesWithTableNames
=
function
(
attributes
)
{
var
replaceReferencesWithTableNames
=
function
(
attributes
)
{
...
...
lib/dialects/abstract/query-generator.js
View file @
3780d3b
...
@@ -356,58 +356,44 @@ module.exports = (function() {
...
@@ -356,58 +356,44 @@ module.exports = (function() {
}
else
if
(
Array
.
isArray
(
obj
))
{
}
else
if
(
Array
.
isArray
(
obj
))
{
// loop through array, adding table names of models to quoted
// loop through array, adding table names of models to quoted
// (checking associations to see if names should be singularised or not)
// (checking associations to see if names should be singularised or not)
var
quoted
=
[]
var
tableNames
=
[]
,
i
,
parentAssociation
,
len
=
obj
.
length
,
len
=
obj
.
length
for
(
i
=
0
;
i
<
len
-
1
;
i
++
)
{
for
(
var
i
=
0
;
i
<
len
-
1
;
i
++
)
{
var
item
=
obj
[
i
]
var
item
=
obj
[
i
]
if
(
Utils
.
_
.
isString
(
item
)
||
item
instanceof
Utils
.
fn
||
item
instanceof
Utils
.
col
||
item
instanceof
Utils
.
literal
||
item
instanceof
Utils
.
cast
||
'raw'
in
item
)
{
if
(
Utils
.
_
.
isString
(
item
)
||
item
instanceof
Utils
.
fn
||
item
instanceof
Utils
.
col
||
item
instanceof
Utils
.
literal
||
item
instanceof
Utils
.
cast
||
'raw'
in
item
)
{
break
break
}
}
var
model
,
as
if
(
item
instanceof
daoFactory
)
{
if
(
item
instanceof
daoFactory
)
{
item
=
{
model
:
item
}
model
=
item
}
// find applicable association for linking parent to this model
var
model
=
item
.
model
,
as
,
associations
=
parent
.
associations
,
association
if
(
item
.
hasOwnProperty
(
'as'
))
{
as
=
item
.
as
association
=
Utils
.
_
.
find
(
associations
,
function
(
association
,
associationName
)
{
return
association
.
target
===
model
&&
associationName
===
as
})
}
else
{
}
else
{
association
=
Utils
.
_
.
find
(
associations
,
function
(
association
,
associationName
)
{
model
=
item
.
model
return
association
.
target
===
model
?
as
=
item
.
as
associationName
===
(
association
.
doubleLinked
?
association
.
combinedName
:
(
association
.
isSingleAssociation
?
Utils
.
singularize
(
model
.
tableName
,
model
.
options
.
language
)
:
parent
.
tableName
+
model
.
tableName
)
)
:
association
.
targetAssociation
&&
association
.
targetAssociation
.
through
===
model
})
// NB association.target !== model clause below is to singularize names of through tables in hasMany-hasMany joins
as
=
(
association
&&
(
association
.
isSingleAssociation
||
association
.
target
!==
model
))
?
Utils
.
singularize
(
model
.
tableName
,
model
.
options
.
language
)
:
model
.
tableName
}
}
quoted
[
i
]
=
as
// check if model provided is through table
var
association
if
(
!
association
)
{
if
(
!
as
&&
parentAssociation
&&
parentAssociation
.
through
===
model
)
{
throw
new
Error
(
'\''
+
quoted
.
join
(
'.'
)
+
'\' in order / group clause is not valid association'
)
association
=
{
as
:
Utils
.
singularize
(
model
.
tableName
,
model
.
options
.
language
)}
}
else
{
// find applicable association for linking parent to this model
association
=
parent
.
getAssociation
(
model
,
as
)
}
}
if
(
association
)
{
tableNames
[
i
]
=
association
.
as
parent
=
model
parent
=
model
parentAssociation
=
association
}
else
{
tableNames
[
i
]
=
model
.
tableName
throw
new
Error
(
'\''
+
tableNames
.
join
(
'.'
)
+
'\' in order / group clause is not valid association'
)
}
}
}
// add 1st string as quoted, 2nd as unquoted raw
// add 1st string as quoted, 2nd as unquoted raw
var
sql
=
(
i
>
0
?
this
.
quoteIdentifier
(
quoted
.
join
(
'.'
))
+
'.'
:
''
)
+
this
.
quote
(
obj
[
i
],
parent
,
force
)
var
sql
=
(
i
>
0
?
this
.
quoteIdentifier
(
tableNames
.
join
(
'.'
))
+
'.'
:
''
)
+
this
.
quote
(
obj
[
i
],
parent
,
force
)
if
(
i
<
len
-
1
)
{
if
(
i
<
len
-
1
)
{
sql
+=
' '
+
obj
[
i
+
1
]
sql
+=
' '
+
obj
[
i
+
1
]
}
}
...
...
test/dao-factory/find.test.js
View file @
3780d3b
...
@@ -511,7 +511,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -511,7 +511,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
'user_id'
,
'user_id'
,
'message'
'message'
],
],
include
:
[{
model
:
User
,
a
s
:
User
.
tableName
,
a
ttributes
:
[
'username'
]
}]
include
:
[{
model
:
User
,
attributes
:
[
'username'
]
}]
}).
success
(
function
(
messages
)
{
}).
success
(
function
(
messages
)
{
...
...
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