不要怂,就是干,撸起袖子干!

Commit 68e61305 by Mick Hansen

Merge pull request #5328 from ashokfernandez/master

Added documentation note about bulkCreate hook and updatesOnDuplicate option
2 parents 0ddfbc5d 12acc40c
Showing with 21 additions and 0 deletions
...@@ -235,6 +235,27 @@ Model.beforeBulkDestroy(function(whereClause) { ...@@ -235,6 +235,27 @@ Model.beforeBulkDestroy(function(whereClause) {
Model.destroy({ where: {username: 'Tom'}} /*whereClause argument*/) Model.destroy({ where: {username: 'Tom'}} /*whereClause argument*/)
``` ```
If you use `Model.bulkCreate(...)` with the `updatesOnDuplicate` option, changes made in the hook to fields that aren't given in the `updatesOnDuplicate` array will not be persisted to the database. However it is possible to change the updatesOnDuplicate option inside the hook if this is what you want.
```
// Bulk updating existing users with updatesOnDuplicate option
Users.bulkCreate([{ id: 1, isMemeber: true},
{ id: 2, isMember: false}],
{ updatesOnDuplicate: ['isMember']})
User.beforeBulkCreate(function (users, options) {
users.forEach(function (user) {
if (user.isMember) {
user.memberSince = new Date()
}
})
// Add memberSince to updatesOnDuplicate otherwise the memberSince date wont be
// saved to the database
options.updatesOnDuplicate.push('memberSince')
})
```
## Associations ## Associations
For the most part hooks will work the same for instances when being associated except a few things For the most part hooks will work the same for instances when being associated except a few things
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!