| Crates.io | entity-derive |
| lib.rs | entity-derive |
| version | 0.5.0 |
| created_at | 2025-12-24 00:54:32.447474+00 |
| updated_at | 2026-01-08 03:46:18.522475+00 |
| description | Derive macro for generating DTOs, repositories, and SQL from a single entity definition |
| homepage | |
| repository | https://github.com/RAprogramm/entity-derive |
| max_upload_size | |
| id | 2002593 |
| size | 127,258 |
One macro to rule them all
Generate DTOs, repositories, mappers, and SQL from a single entity definition
Building a typical CRUD application requires writing the same boilerplate over and over: entity struct, create DTO, update DTO, response DTO, row struct, repository trait, SQL implementation, and 6+ From implementations.
That's 200+ lines of boilerplate for a single entity.
#[derive(Entity)]
#[entity(table = "users")]
pub struct User {
#[id]
pub id: Uuid,
#[field(create, update, response)]
pub name: String,
#[field(create, update, response)]
pub email: String,
#[field(skip)]
pub password_hash: String,
#[field(response)]
#[auto]
pub created_at: DateTime<Utc>,
}
Done. The macro generates everything else.
[dependencies]
entity-derive = { version = "0.4", features = ["postgres", "api"] }
| Feature | Description |
|---|---|
| Zero Runtime Cost | All code generation at compile time |
| Type Safe | Change a field once, everything updates |
| Auto HTTP Handlers | api(handlers) generates CRUD endpoints + router |
| OpenAPI Docs | Auto-generated Swagger/OpenAPI documentation |
| Query Filtering | Type-safe #[filter], #[filter(like)], #[filter(range)] |
| Relations | #[belongs_to] and #[has_many] |
| Transactions | Multi-entity atomic operations |
| Lifecycle Events | Created, Updated, Deleted events |
| Real-Time Streams | Postgres LISTEN/NOTIFY integration |
| Lifecycle Hooks | before_create, after_update, etc. |
| CQRS Commands | Business-oriented command pattern |
| Soft Delete | deleted_at timestamp support |
| Topic | Languages |
|---|---|
| Getting Started | |
| Attributes | 🇬🇧 🇷🇺 🇰🇷 🇪🇸 🇨🇳 |
| Examples | 🇬🇧 🇷🇺 🇰🇷 🇪🇸 🇨🇳 |
| Features | |
| Filtering | 🇬🇧 🇷🇺 🇰🇷 🇪🇸 🇨🇳 |
| Relations | 🇬🇧 🇷🇺 🇰🇷 🇪🇸 🇨🇳 |
| Events | 🇬🇧 🇷🇺 🇰🇷 🇪🇸 🇨🇳 |
| Streams | 🇬🇧 🇷🇺 🇰🇷 🇪🇸 🇨🇳 |
| Hooks | 🇬🇧 🇷🇺 🇰🇷 🇪🇸 🇨🇳 |
| Commands | 🇬🇧 🇷🇺 🇰🇷 🇪🇸 🇨🇳 |
| Advanced | |
| Custom SQL | 🇬🇧 🇷🇺 🇰🇷 🇪🇸 🇨🇳 |
| Web Frameworks | 🇬🇧 🇷🇺 🇰🇷 🇪🇸 🇨🇳 |
| Best Practices | 🇬🇧 🇷🇺 🇰🇷 🇪🇸 🇨🇳 |
#[entity(
table = "users", // Required: table name
schema = "public", // Optional: schema (default: public)
dialect = "postgres", // Optional: database dialect
soft_delete, // Optional: use deleted_at instead of DELETE
events, // Optional: generate lifecycle events
streams, // Optional: real-time Postgres NOTIFY
hooks, // Optional: before/after lifecycle hooks
commands, // Optional: CQRS command pattern
transactions, // Optional: multi-entity transaction support
api( // Optional: generate HTTP handlers + OpenAPI
tag = "Users",
handlers, // All CRUD, or handlers(get, list, create)
security = "bearer", // cookie, bearer, api_key, or none
title = "My API",
api_version = "1.0.0",
),
)]
#[id] // Primary key (auto-generated UUID)
#[auto] // Auto-generated (timestamps)
#[field(create)] // Include in CreateRequest
#[field(update)] // Include in UpdateRequest
#[field(response)] // Include in Response
#[field(skip)] // Exclude from all DTOs
#[filter] // Exact match filter
#[filter(like)] // ILIKE pattern filter
#[filter(range)] // Range filter (from/to)
#[belongs_to(Entity)] // Foreign key relation
#[has_many(Entity)] // One-to-many relation
#[projection(Name: fields)] // Partial view