ReviewOS

also looking at this

stacks/bun-query-builder

fix(orm)!: query delete() honours soft deletes and runs delete hooks

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