Crate entity_derive

Crate entity_derive 

Source
Expand description

entity-derive logo

entity-derive

One macro to rule them all

Generate DTOs, repositories, mappers, and SQL from a single entity definition

Crates.io Documentation CI Status

Coverage License: MIT REUSE Compliant Wiki


§The Problem

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.

§The Solution

#[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.


§Installation

[dependencies]
entity-derive = { version = "0.4", features = ["postgres", "api"] }

§Features

FeatureDescription
Zero Runtime CostAll code generation at compile time
Type SafeChange a field once, everything updates
Auto HTTP Handlersapi(handlers) generates CRUD endpoints + router
OpenAPI DocsAuto-generated Swagger/OpenAPI documentation
Query FilteringType-safe #[filter], #[filter(like)], #[filter(range)]
Relations#[belongs_to] and #[has_many]
TransactionsMulti-entity atomic operations
Lifecycle EventsCreated, Updated, Deleted events
Real-Time StreamsPostgres LISTEN/NOTIFY integration
Lifecycle Hooksbefore_create, after_update, etc.
CQRS CommandsBusiness-oriented command pattern
Soft Deletedeleted_at timestamp support

§Documentation


§Quick Reference

§Entity Attributes

#[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",
    ),
)]

§Field Attributes

#[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

§Code Coverage

Coverage Sunburst

§entity-derive

One crate, all features. Re-exports:

§Quick Start

use entity_derive::{Entity, Pagination};

#[derive(Entity)]
#[entity(table = "users")]
pub struct User {
    #[id]
    pub id: Uuid,
    #[field(create, update, response)]
    pub name: String,
}

// Use pagination
let page = Pagination::page(0, 25);

Modules§

policy
Policy/authorization types for entity-derive.
prelude
Convenient re-exports for common usage.
streamstreams
Streaming types for real-time entity updates.
transaction
Transaction support for entity-derive.

Structs§

Pagination
Pagination parameters for list operations.

Enums§

CommandKind
Kind of business command.
EventKind
Kind of lifecycle event.
SortDirection
Sort direction for ordered queries.

Traits§

EntityCommand
Base trait for entity commands.
EntityEvent
Base trait for entity lifecycle events.
Repository
Base repository trait.

Attribute Macros§

async_trait

Derive Macros§

Entity
Derive macro for generating complete domain boilerplate from a single entity definition.