also looking at this
fix(orm)!: query delete() honours soft deletes and runs delete hooks
#1163
6 files
+406
-9
| @@ -149,6 +149,24 @@ await Post.forceDelete(1) // or Post.remove(1) | ||
| 149 | 149 | An instance has the same pair: `post.delete()` marks a soft-deletable row, |
| 150 | 150 | `post.forceDelete()` removes it. |
| 151 | 151 | |
| 152 | ## Deleting with a Model Query | |
| 153 | ||
| 154 | A model query has the same pair. `delete()` marks the matching rows on a model | |
| 155 | with `useSoftDeletes` and removes them on any other; `forceDelete()` removes | |
| 156 | them either way. Both resolve to the number of rows affected. | |
| 157 | ||
| 158 | ```typescript | |
| 159 | await Post.where('views', 0).delete() // marks them | |
| 160 | await Post.where('views', 0).forceDelete() // removes them | |
| 161 | await Post.onlyTrashed().delete() // purges the trash | |
| 162 | ``` | |
| 163 | ||
| 164 | `beforeDelete` and `afterDelete` run once per row. To give each hook its row, | |
| 165 | the query reads the matching rows first, then deletes them by primary key, so | |
| 166 | the hooks and the delete cover the same rows. Every `beforeDelete` runs before | |
| 167 | anything is written, so one that throws cancels the whole delete. A model with | |
| 168 | no delete hooks skips the read and sends a single statement. | |
| 169 | ||
| 152 | 170 | ## Delete with Returning |
| 153 | 171 | |
| 154 | 172 | ```typescript |