autumn_macros_repository/lib.rs
1//! Repository proc macros for the Autumn web framework.
2//!
3//! This crate provides the `#[repository]` attribute macro. It is split
4//! out of `autumn-macros` so a build that never touches the database
5//! layer never compiles this codegen.
6//!
7//! Users should not depend on this crate directly — use `autumn-web`
8//! instead, which re-exports everything behind its `db` feature.
9
10mod api;
11mod repository;
12mod retention;
13
14use proc_macro::TokenStream;
15
16/// Derive a repository with CRUD operations and derived queries.
17///
18/// Generates a `PgXxxRepository` struct implementing the annotated trait,
19/// with auto-generated CRUD methods and query-by-name derived methods.
20///
21/// # Read replica routing
22///
23/// When `database.replica_url` is configured, generated read-only methods
24/// (`find_by_id`, `find_all`, `count`, `paginate`, `cursor_page`, derived
25/// `find_by_*`, search reads) acquire their connection from the replica
26/// pool; mutating methods always use the primary. Add `primary_reads` to
27/// pin a read-after-write-sensitive repository's reads to the primary, or
28/// call the generated `on_primary()` method to pin a single call chain
29/// (read-your-writes).
30///
31/// # Examples
32///
33/// ```ignore
34/// use autumn_web::repository;
35///
36/// #[repository(Post)]
37/// trait PostRepository {
38/// fn find_by_published(published: bool) -> Vec<Post>;
39/// }
40///
41/// // Reads pinned to the primary even when a replica is configured.
42/// #[repository(LedgerEntry, primary_reads)]
43/// trait LedgerEntryRepository {}
44///
45/// // Cache coherence (#1716): every write below can strand
46/// // `views::recent_posts`, so the edge is declared here. The path resolves
47/// // to the identity constant `#[cached]` generates beside that function, so
48/// // naming anything else does not compile.
49/// #[repository(Post, invalidates(crate::views::recent_posts))]
50/// trait CoherentPostRepository {
51/// // A per-method edge adds to the trait-level ones.
52/// #[invalidates(crate::views::by_author)]
53/// fn delete_by_author_id(author_id: i64) -> ();
54/// }
55/// ```
56///
57/// # Cache coherence (issue #1716)
58///
59/// Every generated write method publishes which model it mutates, so
60/// `autumn cache audit` can fail the build when a write's model appears in a
61/// `#[cached]` read's dependency set with no invalidation covering the pair.
62/// Discharge the obligation with `invalidates(...)` — on the attribute for
63/// every write, or as `#[invalidates(...)]` on one trait method — or opt out
64/// with `acknowledge_stale = "reason"`. A repository that declares any edge
65/// drops those cached reads after each generated write commits. It also gets
66/// `invalidate_declared_caches()` and `invalidate_declared_caches_async()` for
67/// writes that do not use the repository. See `docs/guide/cache-coherence.md`.
68#[proc_macro_attribute]
69pub fn repository(attr: TokenStream, item: TokenStream) -> TokenStream {
70 autumn_macros_support::crate_path::with_override(attr.into(), item.into(), |attr, item| {
71 repository::repository_macro(attr, item)
72 })
73 .into()
74}