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 d3b0388e
authored
Mar 08, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test case and fix for N:M schema support
1 parent
b68e5345
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
94 additions
and
26 deletions
lib/associations/has-many-double-linked.js
lib/associations/has-many.js
lib/dao-factory.js
lib/query-chainer.js
test/associations/belongs-to.test.js
test/associations/has-many.test.js
lib/associations/has-many-double-linked.js
View file @
d3b0388
...
@@ -29,8 +29,10 @@ module.exports = (function() {
...
@@ -29,8 +29,10 @@ module.exports = (function() {
foreignPrimary
=
foreignPrimary
.
length
===
1
?
foreignPrimary
[
0
]
:
'id'
foreignPrimary
=
foreignPrimary
.
length
===
1
?
foreignPrimary
[
0
]
:
'id'
where
[
through
.
tableName
+
"."
+
self
.
association
.
identifier
]
=
self
.
instance
[
instancePrimaryKey
]
where
[
self
.
QueryInterface
.
quoteIdentifiers
(
through
.
getTableName
())
+
"."
+
self
.
association
.
identifier
]
=
self
.
instance
[
instancePrimaryKey
]
where
[
through
.
tableName
+
"."
+
self
.
association
.
foreignIdentifier
]
=
{
join
:
self
.
association
.
target
.
tableName
+
"."
+
foreignPrimary
}
where
[
self
.
QueryInterface
.
quoteIdentifiers
(
through
.
getTableName
())
+
"."
+
self
.
association
.
foreignIdentifier
]
=
{
join
:
self
.
QueryInterface
.
quoteIdentifiers
(
self
.
association
.
target
.
getTableName
())
+
"."
+
foreignPrimary
}
if
(
Object
(
targetAssociation
.
through
)
===
targetAssociation
.
through
)
{
if
(
Object
(
targetAssociation
.
through
)
===
targetAssociation
.
through
)
{
queryOptions
.
hasJoinTableModel
=
true
queryOptions
.
hasJoinTableModel
=
true
...
@@ -38,21 +40,21 @@ module.exports = (function() {
...
@@ -38,21 +40,21 @@ module.exports = (function() {
if
(
!
options
.
attributes
)
{
if
(
!
options
.
attributes
)
{
options
.
attributes
=
[
options
.
attributes
=
[
self
.
QueryInterface
.
quoteIdentifier
(
self
.
association
.
target
.
tableName
)
+
".*"
self
.
QueryInterface
.
quoteIdentifier
s
(
self
.
association
.
target
.
getTableName
()
)
+
".*"
]
]
}
}
if
(
options
.
joinTableAttributes
)
{
if
(
options
.
joinTableAttributes
)
{
options
.
joinTableAttributes
.
forEach
(
function
(
elem
)
{
options
.
joinTableAttributes
.
forEach
(
function
(
elem
)
{
options
.
attributes
.
push
(
options
.
attributes
.
push
(
self
.
QueryInterface
.
quoteIdentifiers
(
through
.
tableName
+
'.'
+
elem
)
+
' as '
+
self
.
QueryInterface
.
quoteIdentifiers
(
through
.
getTableName
()
+
'.'
+
elem
)
+
' as '
+
self
.
QueryInterface
.
quoteIdentifier
(
through
.
name
+
'.'
+
elem
,
true
)
self
.
QueryInterface
.
quoteIdentifier
(
through
.
name
+
'.'
+
elem
,
true
)
)
)
})
})
}
else
{
}
else
{
Utils
.
_
.
forOwn
(
through
.
rawAttributes
,
function
(
elem
,
key
)
{
Utils
.
_
.
forOwn
(
through
.
rawAttributes
,
function
(
elem
,
key
)
{
options
.
attributes
.
push
(
options
.
attributes
.
push
(
self
.
QueryInterface
.
quoteIdentifiers
(
through
.
tableName
+
'.'
+
key
)
+
' as '
+
self
.
QueryInterface
.
quoteIdentifiers
(
through
.
getTableName
()
+
'.'
+
key
)
+
' as '
+
self
.
QueryInterface
.
quoteIdentifier
(
through
.
name
+
'.'
+
key
,
true
)
self
.
QueryInterface
.
quoteIdentifier
(
through
.
name
+
'.'
+
key
,
true
)
)
)
})
})
...
@@ -77,7 +79,7 @@ module.exports = (function() {
...
@@ -77,7 +79,7 @@ module.exports = (function() {
options
.
where
=
where
;
options
.
where
=
where
;
}
}
self
.
association
.
target
.
findAllJoin
(
through
.
tableName
,
options
,
queryOptions
)
self
.
association
.
target
.
findAllJoin
(
through
.
getTableName
()
,
options
,
queryOptions
)
.
on
(
'success'
,
function
(
objects
)
{
customEventEmitter
.
emit
(
'success'
,
objects
)
})
.
on
(
'success'
,
function
(
objects
)
{
customEventEmitter
.
emit
(
'success'
,
objects
)
})
.
on
(
'error'
,
function
(
err
){
customEventEmitter
.
emit
(
'error'
,
err
)
})
.
on
(
'error'
,
function
(
err
){
customEventEmitter
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
customEventEmitter
.
emit
(
'sql'
,
sql
)})
.
on
(
'sql'
,
function
(
sql
)
{
customEventEmitter
.
emit
(
'sql'
,
sql
)})
...
...
lib/associations/has-many.js
View file @
d3b0388
...
@@ -188,10 +188,6 @@ module.exports = (function() {
...
@@ -188,10 +188,6 @@ module.exports = (function() {
this
.
through
.
rawAttributes
=
Utils
.
_
.
merge
(
this
.
through
.
rawAttributes
,
combinedTableAttributes
)
this
.
through
.
rawAttributes
=
Utils
.
_
.
merge
(
this
.
through
.
rawAttributes
,
combinedTableAttributes
)
this
.
through
.
init
(
this
.
through
.
daoFactoryManager
)
this
.
through
.
init
(
this
.
through
.
daoFactoryManager
)
if
(
this
.
options
.
syncOnAssociation
)
{
this
.
through
.
sync
()
}
}
else
{
}
else
{
var
newAttributes
=
{}
var
newAttributes
=
{}
newAttributes
[
this
.
identifier
]
=
{
type
:
this
.
options
.
keyType
||
this
.
target
.
rawAttributes
[
this
.
target
.
primaryKeyAttribute
].
type
}
newAttributes
[
this
.
identifier
]
=
{
type
:
this
.
options
.
keyType
||
this
.
target
.
rawAttributes
[
this
.
target
.
primaryKeyAttribute
].
type
}
...
@@ -282,7 +278,7 @@ module.exports = (function() {
...
@@ -282,7 +278,7 @@ module.exports = (function() {
,
where
=
{}
,
where
=
{}
where
[
newAssociatedObject
.
Model
.
tableName
+
'.'
+
primaryKey
]
=
newAssociatedObject
[
primaryKey
]
where
[
newAssociatedObject
.
Model
.
getTableName
()
+
'.'
+
primaryKey
]
=
newAssociatedObject
[
primaryKey
]
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
instance
[
self
.
accessors
.
get
]({
where
:
where
})
instance
[
self
.
accessors
.
get
]({
where
:
where
})
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]})
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]})
...
...
lib/dao-factory.js
View file @
d3b0388
...
@@ -472,8 +472,7 @@ module.exports = (function() {
...
@@ -472,8 +472,7 @@ module.exports = (function() {
//right now, the caller (has-many-double-linked) is in charge of the where clause
//right now, the caller (has-many-double-linked) is in charge of the where clause
DAOFactory
.
prototype
.
findAllJoin
=
function
(
joinTableName
,
options
,
queryOptions
)
{
DAOFactory
.
prototype
.
findAllJoin
=
function
(
joinTableName
,
options
,
queryOptions
)
{
var
optcpy
=
Utils
.
_
.
clone
(
options
)
var
optcpy
=
Utils
.
_
.
clone
(
options
)
optcpy
.
attributes
=
optcpy
.
attributes
||
[
this
.
QueryInterface
.
quoteIdentifier
(
this
.
tableName
)
+
".*"
]
optcpy
.
attributes
=
optcpy
.
attributes
||
[
this
.
QueryInterface
.
quoteIdentifier
(
this
.
getTableName
())
+
".*"
]
// whereCollection is used for non-primary key updates
// whereCollection is used for non-primary key updates
this
.
options
.
whereCollection
=
optcpy
.
where
||
null
;
this
.
options
.
whereCollection
=
optcpy
.
where
||
null
;
...
...
lib/query-chainer.js
View file @
d3b0388
...
@@ -89,7 +89,9 @@ module.exports = (function() {
...
@@ -89,7 +89,9 @@ module.exports = (function() {
}
else
{
}
else
{
onSuccess
()
onSuccess
()
}
}
}).
error
(
onError
)
}).
error
(
onError
).
on
(
'sql'
,
function
(
sql
)
{
self
.
eventEmitter
.
emit
(
'sql'
,
sql
)
})
}
}
}
else
{
}
else
{
self
.
wasRunning
=
true
self
.
wasRunning
=
true
...
...
test/associations/belongs-to.test.js
View file @
d3b0388
...
@@ -75,6 +75,31 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
...
@@ -75,6 +75,31 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
})
})
})
})
})
})
it
(
'supports schemas'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'UserXYZ'
,
{
username
:
Sequelize
.
STRING
,
gender
:
Sequelize
.
STRING
}).
schema
(
'archive'
)
,
Task
=
this
.
sequelize
.
define
(
'TaskXYZ'
,
{
title
:
Sequelize
.
STRING
,
status
:
Sequelize
.
STRING
}).
schema
(
'archive'
)
,
self
=
this
Task
.
belongsTo
(
User
)
self
.
sequelize
.
dropAllSchemas
().
done
(
function
()
{
self
.
sequelize
.
createSchema
(
'archive'
).
done
(
function
()
{
self
.
sequelize
.
sync
({
force
:
true
}).
done
(
function
()
{
User
.
create
({
username
:
'foo'
,
gender
:
'male'
}).
success
(
function
(
user
)
{
Task
.
create
({
title
:
'task'
,
status
:
'inactive'
}).
success
(
function
(
task
)
{
task
.
setUserXYZ
(
user
).
success
(
function
()
{
task
.
getUserXYZ
().
success
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
ok
done
()
})
})
})
})
})
})
})
})
})
})
describe
(
'setAssociation'
,
function
()
{
describe
(
'setAssociation'
,
function
()
{
...
...
test/associations/has-many.test.js
View file @
d3b0388
...
@@ -542,7 +542,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -542,7 +542,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
describe
(
'(N:M)'
,
function
()
{
describe
(
'(N:M)'
,
function
()
{
describe
(
"get
ting assocations with op
tions"
,
function
()
{
describe
(
"get
Associa
tions"
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
(
done
)
{
var
self
=
this
var
self
=
this
...
@@ -552,18 +552,16 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -552,18 +552,16 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
self
.
User
.
hasMany
(
self
.
Task
)
self
.
User
.
hasMany
(
self
.
Task
)
self
.
Task
.
hasMany
(
self
.
User
)
self
.
Task
.
hasMany
(
self
.
User
)
this
.
User
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
sequelize
.
sync
({
force
:
true
}).
done
(
function
(
err
)
{
self
.
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
var
chainer
=
new
Sequelize
.
Utils
.
QueryChainer
([
var
chainer
=
new
Sequelize
.
Utils
.
QueryChainer
([
self
.
User
.
create
({
username
:
'John'
}),
self
.
User
.
create
({
username
:
'John'
}),
self
.
Task
.
create
({
title
:
'Get rich'
,
active
:
true
}),
self
.
Task
.
create
({
title
:
'Get rich'
,
active
:
true
}),
self
.
Task
.
create
({
title
:
'Die trying'
,
active
:
false
})
self
.
Task
.
create
({
title
:
'Die trying'
,
active
:
false
})
])
])
chainer
.
run
().
success
(
function
(
results
,
john
,
task1
,
task2
)
{
chainer
.
run
().
success
(
function
(
results
,
john
,
task1
,
task2
)
{
john
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
john
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
done
()
done
()
})
})
})
})
})
})
})
...
@@ -634,6 +632,52 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -634,6 +632,52 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
done
();
done
();
})
})
})
})
it
(
'should support schemas'
,
function
(
done
)
{
var
self
=
this
,
AcmeUser
=
self
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
}).
schema
(
'acme'
,
'_'
)
,
AcmeProject
=
self
.
sequelize
.
define
(
'Project'
,
{
title
:
DataTypes
.
STRING
,
active
:
DataTypes
.
BOOLEAN
}).
schema
(
'acme'
,
'_'
)
,
AcmeProjectUsers
=
self
.
sequelize
.
define
(
'ProjectUsers'
,
{
status
:
DataTypes
.
STRING
,
data
:
DataTypes
.
INTEGER
}).
schema
(
'acme'
,
'_'
)
AcmeUser
.
hasMany
(
AcmeProject
,
{
through
:
AcmeProjectUsers
})
AcmeProject
.
hasMany
(
AcmeUser
,
{
through
:
AcmeProjectUsers
})
self
.
sequelize
.
dropAllSchemas
().
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
self
.
sequelize
.
createSchema
(
'acme'
).
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
self
.
sequelize
.
sync
({
force
:
true
}).
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
AcmeUser
.
create
().
done
(
function
(
err
,
u
)
{
expect
(
err
).
not
.
to
.
be
.
ok
AcmeProject
.
create
().
done
(
function
(
err
,
p
)
{
expect
(
err
).
not
.
to
.
be
.
ok
u
.
addProject
(
p
,
{
status
:
'active'
,
data
:
42
}).
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
u
.
getProjects
().
done
(
function
(
err
,
projects
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
projects
).
to
.
have
.
length
(
1
);
var
project
=
projects
[
0
]
expect
(
project
.
ProjectUsers
).
to
.
be
.
defined
expect
(
project
.
status
).
not
.
to
.
exist
expect
(
project
.
ProjectUsers
.
status
).
to
.
equal
(
'active'
)
done
()
})
})
})
})
})
})
})
})
})
})
it
(
"removes the reference id, which was added in the first place"
,
function
(
done
)
{
it
(
"removes the reference id, which was added in the first place"
,
function
(
done
)
{
...
...
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