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

Commit cde11b7f by Jan Aagaard Meier

[ci skip] Change bulk update and destroy to use where argument

1 parent edc5cbd5
Showing with 9 additions and 14 deletions
......@@ -144,14 +144,11 @@ afterBulkCreate / afterBulkUpdate / afterBulkDestroy
If you want to emit hooks for each individual record, along with the bulk hooks you can pass `individualHooks: true` to the call.
```js
Model.destroy({accessLevel: 0}, {individualHooks: true})
Model.destroy({ where: {accessLevel: 0}}, {individualHooks: true})
// Will select all records that are about to be deleted and emit before- + after- Destroy on each instance
Model.update({username: 'Toni'}, {accessLevel: 0}, {individualHooks: true})
Model.update({username: 'Toni'}, { where: {accessLevel: 0}}, {individualHooks: true})
// Will select all records that are about to be updated and emit before- + after- Update on each instance
Model.bulkCreate({accessLevel: 0}, null, {individualHooks: true})
// Will select all records that are about to be deleted and emit before- + after- Create on each instance
```
Some model hooks have two or three parameters sent to each hook depending on it's type.
......@@ -172,13 +169,13 @@ Model.beforeBulkUpdate(function(attributes, where, fn) {
// where = second argument sent to Model.update
})
Model.update({gender: 'Male'} /*attributes argument*/, {username: 'Tom'} /*where argument*/)
Model.update({gender: 'Male'} /*attributes argument*/, { where: {username: 'Tom'}} /*where argument*/)
Model.beforeBulkDestroy(function(whereClause, fn) {
// whereClause = first argument sent to Model.destroy
})
Model.destroy({username: 'Tom'} /*whereClause argument*/)
Model.destroy({ where: {username: 'Tom'}} /*whereClause argument*/)
```
## Associations
......@@ -248,4 +245,4 @@ If we had not included the transaction option in our call to `User.update` in th
It is very important to recognize that sequelize may make use of transactions internally for certain operations such as `Model.findOrCreate`. If your hook functions execute read or write operations that rely on the object's presence in the database, or modify the object's stored values like the example in the preceding section, you should always specify `{ transaction: options.transaction }`.
If the hook has been called in the process of a transacted operation, this makes sure that your dependent read/write is a part of that same transaction. If the hook is not transacted, you have simply specified `{ transaction: null }` and can expect the default behaviour.
\ No newline at end of file
If the hook has been called in the process of a transacted operation, this makes sure that your dependent read/write is a part of that same transaction. If the hook is not transacted, you have simply specified `{ transaction: null }` and can expect the default behaviour.
......@@ -155,8 +155,8 @@ Task.bulkCreate([
{subject: 'programming', status: 'finished'}
]).success(function() {
Task.update(
{status: 'inactive'} /* set attributes' value */,
{subject: 'programming'} /* where criteria */
{ status: 'inactive' } /* set attributes' value */,
{ where: { subject: 'programming' }} /* where criteria */
).success(function(affectedRows) {
// affectedRows will be 2
Task.findAll().success(function(tasks) {
......@@ -175,7 +175,7 @@ Task.bulkCreate([
{subject: 'programming', status: 'finished'}
]).success(function() {
Task.destroy(
{subject: 'programming'} /* where criteria */,
{ where: {subject: 'programming'}} /* where criteria */,
{truncate: true /* truncate the whole table, ignoring where criteria */} /* options */
).success(function(affectedRows) {
// affectedRows will be 2
......@@ -338,4 +338,4 @@ User.find(1).success(function(user) {
'my-very-other-field': 3
}).success(/* ... */)
})
```
\ No newline at end of file
```
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!