Skip to content

#127 feat: repository wrapper invoking the generated hooks - #267

Merged
RAprogramm merged 2 commits into
mainfrom
127
Jul 26, 2026
Merged

#127 feat: repository wrapper invoking the generated hooks#267
RAprogramm merged 2 commits into
mainfrom
127

Conversation

@RAprogramm

Copy link
Copy Markdown
Owner

Closes #127

The orphan rule keeps a user crate from implementing {Entity}Hooks for sqlx::PgPool — both the trait and the type are foreign there — so the repository impl on the pool could never call hooks, and the documented answer was to call them by hand at the service layer.

#[entity(hooks)] now also emits {Entity}Repo<H>, the type that owns both:

let repo = UserRepo::new(pool, Audit);

let user = repo.create(dto).await?;      // before_create → INSERT → after_create
let found = repo.find_by_id(id).await?;  // reads carry no hooks
Operation Hooks
create, save before_create may rewrite the DTO, after_create sees the persisted row
update before_update may rewrite the patch
delete after_delete only when a row was actually affected
hard_delete, restore on soft-delete entities
everything else reaches the pool through Deref, unchanged

A failing before_* aborts before anything is written. The hook error only has to convert into the repository error, so hooks keep their own error type — this is the RFC's option A, with the delegation done through Deref so the wrapper answers every repository method instead of re-declaring them.

Backward compatible: the bare pool path is untouched, and manual wiring keeps working.

Four live-Postgres cases cover the order of calls across create, update, delete and restore; that a refusing before_create leaves the table empty and never runs the after hook; that a refusing before_delete leaves the row in place; and that a read invokes nothing. examples/hooks is rewritten around the wrapper — the handlers no longer call hooks by hand. README, the module docs and the hooks page in all five wiki languages are updated.

@codecov

codecov Bot commented Jul 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 7 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...tes/entity-derive-impl/src/entity/hooks/wrapper.rs 89.85% 7 Missing ⚠️

📢 Thoughts on this report? Let us know!

@RAprogramm
RAprogramm merged commit ed4b109 into main Jul 26, 2026
22 checks passed
@RAprogramm
RAprogramm deleted the 127 branch July 26, 2026 23:41
@RAprogramm RAprogramm mentioned this pull request Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RFC: auto-invoke {Entity}Hooks from generated Repository methods

1 participant