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 b0eecfa8
authored
Feb 26, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed eager loading of 2 associations with the same origin but different alias
1 parent
08122ca4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
48 deletions
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
spec/dao-factory.spec.js
lib/dialects/mysql/query-generator.js
View file @
b0eecfa
...
@@ -133,22 +133,19 @@ module.exports = (function() {
...
@@ -133,22 +133,19 @@ module.exports = (function() {
options
.
include
.
forEach
(
function
(
include
)
{
options
.
include
.
forEach
(
function
(
include
)
{
var
attributes
=
Object
.
keys
(
include
.
daoFactory
.
attributes
).
map
(
function
(
attr
)
{
var
attributes
=
Object
.
keys
(
include
.
daoFactory
.
attributes
).
map
(
function
(
attr
)
{
var
template
=
Utils
.
_
.
template
(
"`<%= table %>`.`<%= attr %>` AS `<%= as %>.<%= attr %>`"
)
var
template
=
Utils
.
_
.
template
(
"`<%= as %>`.`<%= attr %>` AS `<%= as %>.<%= attr %>`"
)
return
template
({
return
template
({
as
:
include
.
as
,
attr
:
attr
})
table
:
include
.
daoFactory
.
tableName
,
as
:
include
.
as
,
attr
:
attr
})
})
})
optAttributes
=
optAttributes
.
concat
(
attributes
)
optAttributes
=
optAttributes
.
concat
(
attributes
)
var
joinQuery
=
" LEFT OUTER JOIN `<%= table %>` ON `<%= tableLeft %>`.`<%= attrLeft %>` = `<%= tableRight %>`.`<%= attrRight %>`"
var
joinQuery
=
" LEFT OUTER JOIN `<%= table %>`
AS `<%= as %>`
ON `<%= tableLeft %>`.`<%= attrLeft %>` = `<%= tableRight %>`.`<%= attrRight %>`"
query
+=
Utils
.
_
.
template
(
joinQuery
)({
query
+=
Utils
.
_
.
template
(
joinQuery
)({
table
:
include
.
daoFactory
.
tableName
,
table
:
include
.
daoFactory
.
tableName
,
tableLeft
:
((
include
.
association
.
associationType
===
'BelongsTo'
)
?
include
.
daoFactory
.
tableName
:
tableName
),
as
:
include
.
as
,
tableLeft
:
((
include
.
association
.
associationType
===
'BelongsTo'
)
?
include
.
as
:
tableName
),
attrLeft
:
'id'
,
attrLeft
:
'id'
,
tableRight
:
((
include
.
association
.
associationType
===
'BelongsTo'
)
?
tableName
:
include
.
daoFactory
.
tableName
),
tableRight
:
((
include
.
association
.
associationType
===
'BelongsTo'
)
?
tableName
:
include
.
as
),
attrRight
:
include
.
association
.
identifier
attrRight
:
include
.
association
.
identifier
})
})
})
})
...
...
lib/dialects/postgres/query-generator.js
View file @
b0eecfa
...
@@ -235,22 +235,19 @@ module.exports = (function() {
...
@@ -235,22 +235,19 @@ module.exports = (function() {
options
.
include
.
forEach
(
function
(
include
)
{
options
.
include
.
forEach
(
function
(
include
)
{
var
attributes
=
Object
.
keys
(
include
.
daoFactory
.
attributes
).
map
(
function
(
attr
)
{
var
attributes
=
Object
.
keys
(
include
.
daoFactory
.
attributes
).
map
(
function
(
attr
)
{
var
template
=
Utils
.
_
.
template
(
'"<%= table %>"."<%= attr %>" AS "<%= as %>.<%= attr %>"'
)
var
template
=
Utils
.
_
.
template
(
'"<%= as %>"."<%= attr %>" AS "<%= as %>.<%= attr %>"'
)
return
template
({
return
template
({
as
:
include
.
as
,
attr
:
attr
})
table
:
include
.
daoFactory
.
tableName
,
as
:
include
.
as
,
attr
:
attr
})
})
})
optAttributes
=
optAttributes
.
concat
(
attributes
)
optAttributes
=
optAttributes
.
concat
(
attributes
)
var
joinQuery
=
' LEFT OUTER JOIN "<%= table %>" ON "<%= tableLeft %>"."<%= attrLeft %>" = "<%= tableRight %>"."<%= attrRight %>"'
var
joinQuery
=
' LEFT OUTER JOIN "<%= table %>"
AS "<%= as %>"
ON "<%= tableLeft %>"."<%= attrLeft %>" = "<%= tableRight %>"."<%= attrRight %>"'
query
+=
Utils
.
_
.
template
(
joinQuery
)({
query
+=
Utils
.
_
.
template
(
joinQuery
)({
table
:
include
.
daoFactory
.
tableName
,
table
:
include
.
daoFactory
.
tableName
,
tableLeft
:
((
include
.
association
.
associationType
===
'BelongsTo'
)
?
include
.
daoFactory
.
tableName
:
tableName
),
as
:
include
.
as
,
tableLeft
:
((
include
.
association
.
associationType
===
'BelongsTo'
)
?
include
.
as
:
tableName
),
attrLeft
:
'id'
,
attrLeft
:
'id'
,
tableRight
:
((
include
.
association
.
associationType
===
'BelongsTo'
)
?
tableName
:
include
.
daoFactory
.
tableName
),
tableRight
:
((
include
.
association
.
associationType
===
'BelongsTo'
)
?
tableName
:
include
.
as
),
attrRight
:
include
.
association
.
identifier
attrRight
:
include
.
association
.
identifier
})
})
})
})
...
...
spec/dao-factory.spec.js
View file @
b0eecfa
...
@@ -52,18 +52,12 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
...
@@ -52,18 +52,12 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
})
it
(
"throws an error if 2 autoIncrements are passed"
,
function
()
{
it
(
"throws an error if 2 autoIncrements are passed"
,
function
()
{
try
{
Helpers
.
assertException
(
function
()
{
var
User
=
this
.
sequelize
.
define
(
'UserWithTwoAutoIncrements'
,
{
this
.
sequelize
.
define
(
'UserWithTwoAutoIncrements'
,
{
userid
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
userid
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
userscore
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
}
userscore
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
}
})
})
// the parse shouldn't execute the following line
}.
bind
(
this
),
'Invalid DAO definition. Only one autoincrement field allowed.'
)
// this tests needs to be refactored...
// we need to use expect.toThrow when a later version than 0.6 was released
expect
(
1
).
toEqual
(
2
)
}
catch
(
e
)
{
expect
(
e
.
message
).
toEqual
(
'Invalid DAO definition. Only one autoincrement field allowed.'
)
}
})
})
})
})
...
@@ -205,18 +199,12 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
...
@@ -205,18 +199,12 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
})
})
})
it
(
'raises an error if you mess up the datatype'
,
function
(
done
)
{
it
(
'raises an error if you mess up the datatype'
,
function
()
{
Helpers
.
assertException
(
function
()
{
try
{
this
.
sequelize
.
define
(
'UserBadDataType'
,
{
var
User
=
this
.
sequelize
.
define
(
'UserBadDataType'
,
{
activity_date
:
Sequelize
.
DATe
activity_date
:
Sequelize
.
DATe
});
})
done
()
}.
bind
(
this
),
'Unrecognized data type for field activity_date'
)
}
catch
(
e
)
{
expect
(
e
.
message
).
toEqual
(
'Unrecognized data type for field activity_date'
)
done
()
}
})
})
it
(
'sets a 64 bit int in bigint'
,
function
(
done
)
{
it
(
'sets a 64 bit int in bigint'
,
function
(
done
)
{
...
@@ -468,24 +456,29 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
...
@@ -468,24 +456,29 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
describe
(
'eager loading'
,
function
()
{
describe
(
'eager loading'
,
function
()
{
before
(
function
()
{
before
(
function
()
{
this
.
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
this
.
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
this
.
Worker
=
this
.
sequelize
.
define
(
'Worker'
,
{
name
:
Sequelize
.
STRING
})
this
.
Worker
=
this
.
sequelize
.
define
(
'Worker'
,
{
name
:
Sequelize
.
STRING
})
})
describe
(
'belongsTo'
,
function
()
{
before
(
function
(
done
)
{
this
.
Task
.
belongsTo
(
this
.
Worker
)
this
.
init
=
function
(
callback
)
{
this
.
sequelize
.
sync
({
force
:
true
}).
complete
(
function
()
{
this
.
sequelize
.
sync
({
force
:
true
}).
complete
(
function
()
{
this
.
Worker
.
create
({
name
:
'worker'
}).
success
(
function
(
worker
)
{
this
.
Worker
.
create
({
name
:
'worker'
}).
success
(
function
(
worker
)
{
this
.
Task
.
create
({
title
:
'homework'
}).
success
(
function
(
task
)
{
this
.
Task
.
create
({
title
:
'homework'
}).
success
(
function
(
task
)
{
this
.
worker
=
worker
this
.
worker
=
worker
this
.
task
=
task
this
.
task
=
task
this
.
task
.
setWorker
(
this
.
worker
).
success
(
done
)
callback
(
)
}.
bind
(
this
))
}.
bind
(
this
))
}.
bind
(
this
))
}.
bind
(
this
))
}.
bind
(
this
))
}.
bind
(
this
))
}.
bind
(
this
)
})
describe
(
'belongsTo'
,
function
()
{
before
(
function
(
done
)
{
this
.
Task
.
belongsTo
(
this
.
Worker
)
this
.
init
(
function
()
{
this
.
task
.
setWorker
(
this
.
worker
).
success
(
done
)
}.
bind
(
this
))
})
})
it
(
'throws an error about unexpected input if include contains a non-object'
,
function
()
{
it
(
'throws an error about unexpected input if include contains a non-object'
,
function
()
{
...
@@ -518,6 +511,43 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
...
@@ -518,6 +511,43 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
done
()
done
()
}.
bind
(
this
))
}.
bind
(
this
))
})
})
it
(
'returns the private and public ip'
,
function
(
done
)
{
var
Domain
=
this
.
sequelize
.
define
(
'Domain'
,
{
ip
:
Sequelize
.
STRING
})
var
Environment
=
this
.
sequelize
.
define
(
'Environment'
,
{
name
:
Sequelize
.
STRING
})
Environment
.
belongsTo
(
Domain
,
{
as
:
'PrivateDomain'
,
foreignKey
:
'privateDomainId'
})
.
belongsTo
(
Domain
,
{
as
:
'PublicDomain'
,
foreignKey
:
'publicDomainId'
})
this
.
sequelize
.
sync
({
force
:
true
}).
complete
(
function
()
{
Domain
.
create
({
ip
:
'192.168.0.1'
}).
success
(
function
(
privateIp
)
{
Domain
.
create
({
ip
:
'91.65.189.19'
}).
success
(
function
(
publicIp
)
{
Environment
.
create
({
name
:
'environment'
}).
success
(
function
(
env
)
{
env
.
setPrivateDomain
(
privateIp
).
success
(
function
()
{
env
.
setPublicDomain
(
publicIp
).
success
(
function
()
{
Environment
.
find
({
where
:
{
name
:
'environment'
},
include
:
[
{
daoFactory
:
Domain
,
as
:
'PrivateDomain'
},
{
daoFactory
:
Domain
,
as
:
'PublicDomain'
}
]
}).
complete
(
function
(
err
,
environment
)
{
expect
(
err
).
toBeNull
()
expect
(
environment
).
toBeDefined
()
expect
(
environment
.
privateDomain
).
toBeDefined
()
expect
(
environment
.
privateDomain
.
ip
).
toEqual
(
'192.168.0.1'
)
expect
(
environment
.
publicDomain
).
toBeDefined
()
expect
(
environment
.
publicDomain
.
ip
).
toEqual
(
'91.65.189.19'
)
done
()
})
})
})
})
})
})
})
})
})
})
describe
(
'hasOne'
,
function
()
{
describe
(
'hasOne'
,
function
()
{
...
...
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