A set of procedural macros for Diesel that auto‑generates repository types and CRUD implementations in a “Spring‑JPA‑like” style.
-
Repository Derivation:
Use#[derive(Repository)]
with a custom attribute to generate a repository type for your entity. -
CRUD & Paging Macros:
Annotate your entity with#[crud_repo(...)]
and#[paging_repo(...)]
to automatically implement repository traits such as find, insert, update, delete, and paging. -
Async & Sync Support:
Enable asynchronous (Tokio‑based) implementations via theasync
Cargo feature, or compile the synchronous version by default.
Annotate your Diesel entity as follows:
use diesel_repository::{Repository, crud_repo, paging_repo};
#[derive(
Debug, Eq, PartialEq, Queryable, Identifiable, Selectable, Insertable, AsChangeset, Repository,
)]
#[repository(pool = "db::DbPool")]
#[diesel(table_name = crate::accounts)]
#[crud_repo(find_all, find_one, insert, update, delete)]
#[paging_repo(find_all)]
pub struct Account {
pub id: String,
pub sub: String,
pub name: String,
}
This generates an AccountRepo
type with implementations for the repository traits (both async and sync based on your feature flag).
So something like this is now possible:
fn main() -> anyhow::Result<()> {
// Create a dummy pool (replace with your actual pool creation logic).
let pool = Arc::new(dummy_pool());
let repo = AccountRepo::new(pool);
println!("Sync test run completed.");
let _result: Vec<Account> = repo.find_all()?;
let _paged = repo.find_all_paging(1, 10)?;
Ok(())
}
Look at the projects diesel-repository-test-async
and diesel-repository-test-sync
for more.
The workspace includes dedicated test packages:
- Async Tests:
Run:cargo run -p diesel-repository-test-async
- Sync Tests:
Run:cargo run -p diesel-repository-test-sync
This project is dual‑licensed under the MIT and Apache-2.0 licenses.