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 0a3a4ced
authored
Nov 11, 2014
by
Simon Gaeremynck
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow removing has-many associations by id
1 parent
dee68a54
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
4 deletions
lib/associations/has-many.js
test/associations/has-many.test.js
lib/associations/has-many.js
View file @
0a3a4ce
...
@@ -474,6 +474,14 @@ module.exports = (function() {
...
@@ -474,6 +474,14 @@ module.exports = (function() {
return
instance
[
association
.
accessors
.
get
]({},
options
).
then
(
function
(
currentAssociatedObjects
)
{
return
instance
[
association
.
accessors
.
get
]({},
options
).
then
(
function
(
currentAssociatedObjects
)
{
var
newAssociations
=
[];
var
newAssociations
=
[];
if
(
!
(
oldAssociatedObject
instanceof
association
.
target
.
Instance
))
{
var
tmpInstance
=
{};
tmpInstance
[
primaryKeyAttribute
]
=
oldAssociatedObject
;
oldAssociatedObject
=
association
.
target
.
build
(
tmpInstance
,
{
isNewRecord
:
false
});
}
currentAssociatedObjects
.
forEach
(
function
(
association
)
{
currentAssociatedObjects
.
forEach
(
function
(
association
)
{
if
(
!
Utils
.
_
.
isEqual
(
oldAssociatedObject
.
identifiers
,
association
.
identifiers
))
{
if
(
!
Utils
.
_
.
isEqual
(
oldAssociatedObject
.
identifiers
,
association
.
identifiers
))
{
newAssociations
.
push
(
association
);
newAssociations
.
push
(
association
);
...
@@ -489,6 +497,18 @@ module.exports = (function() {
...
@@ -489,6 +497,18 @@ module.exports = (function() {
return
instance
[
association
.
accessors
.
get
]({},
options
).
then
(
function
(
currentAssociatedObjects
)
{
return
instance
[
association
.
accessors
.
get
]({},
options
).
then
(
function
(
currentAssociatedObjects
)
{
var
newAssociations
=
[];
var
newAssociations
=
[];
// Ensure the oldAssociatedObjects array is an array of target instances
oldAssociatedObjects
=
oldAssociatedObjects
.
map
(
function
(
oldAssociatedObject
)
{
if
(
!
(
oldAssociatedObject
instanceof
association
.
target
.
Instance
))
{
var
tmpInstance
=
{};
tmpInstance
[
primaryKeyAttribute
]
=
oldAssociatedObject
;
oldAssociatedObject
=
association
.
target
.
build
(
tmpInstance
,
{
isNewRecord
:
false
});
}
return
oldAssociatedObject
;
});
currentAssociatedObjects
.
forEach
(
function
(
association
)
{
currentAssociatedObjects
.
forEach
(
function
(
association
)
{
// Determine is this is an association we want to remove
// Determine is this is an association we want to remove
...
...
test/associations/has-many.test.js
View file @
0a3a4ce
...
@@ -1971,14 +1971,16 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -1971,14 +1971,16 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
return
this
.
sequelize
.
sync
().
then
(
function
()
{
return
this
.
sequelize
.
sync
().
then
(
function
()
{
return
Sequelize
.
Promise
.
all
([
return
Sequelize
.
Promise
.
all
([
Worker
.
create
({}),
Worker
.
create
({}),
Task
.
bulkCreate
([{},
{}]).
then
(
function
()
{
Task
.
bulkCreate
([{},
{}
,
{}
]).
then
(
function
()
{
return
Task
.
findAll
();
return
Task
.
findAll
();
})
})
]);
]);
}).
spread
(
function
(
worker
,
tasks
)
{
}).
spread
(
function
(
worker
,
tasks
)
{
// Set all tasks, then remove one task
s
, then return all tasks
// Set all tasks, then remove one task
by instance, then remove one task by id
, then return all tasks
return
worker
.
setTasks
(
tasks
).
then
(
function
()
{
return
worker
.
setTasks
(
tasks
).
then
(
function
()
{
return
worker
.
removeTask
(
tasks
[
0
]);
return
worker
.
removeTask
(
tasks
[
0
]);
}).
then
(
function
()
{
return
worker
.
removeTask
(
tasks
[
1
].
id
);
}).
then
(
function
()
{
}).
then
(
function
()
{
return
worker
.
getTasks
();
return
worker
.
getTasks
();
});
});
...
@@ -1999,15 +2001,17 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -1999,15 +2001,17 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
return
this
.
sequelize
.
sync
().
then
(
function
()
{
return
this
.
sequelize
.
sync
().
then
(
function
()
{
return
Sequelize
.
Promise
.
all
([
return
Sequelize
.
Promise
.
all
([
Worker
.
create
({}),
Worker
.
create
({}),
Task
.
bulkCreate
([{},
{},
{}]).
then
(
function
()
{
Task
.
bulkCreate
([{},
{},
{}
,
{},
{}
]).
then
(
function
()
{
return
Task
.
findAll
();
return
Task
.
findAll
();
})
})
]);
]);
}).
spread
(
function
(
worker
,
tasks
)
{
}).
spread
(
function
(
worker
,
tasks
)
{
// Set all tasks, then remove two tasks, then return all tasks
// Set all tasks, then remove two tasks
by instance, then remove two tasks by id
, then return all tasks
return
worker
.
setTasks
(
tasks
).
then
(
function
()
{
return
worker
.
setTasks
(
tasks
).
then
(
function
()
{
return
worker
.
removeTasks
([
tasks
[
0
],
tasks
[
1
]]);
return
worker
.
removeTasks
([
tasks
[
0
],
tasks
[
1
]]);
}).
then
(
function
()
{
}).
then
(
function
()
{
return
worker
.
removeTasks
([
tasks
[
2
].
id
,
tasks
[
3
].
id
]);
}).
then
(
function
()
{
return
worker
.
getTasks
();
return
worker
.
getTasks
();
});
});
}).
then
(
function
(
tasks
)
{
}).
then
(
function
(
tasks
)
{
...
...
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