Skip to main content

Crate crudcrate_derive

Crate crudcrate_derive 

Source
Expand description

Procedural macros for generating CRUD operations from Sea-ORM entities.

Main macro: #[derive(EntityToModels)] - see entity_to_models

§Available Attributes

§Struct-Level Attributes

Use on the struct with #[crudcrate(...)]:

AttributeTypeDescription
generate_routerflagGenerate Axum router function
api_struct = "Name"stringOverride generated struct name
name_singular = "item"stringSingular resource name for errors/headers
name_plural = "items"stringPlural resource name for routes
description = "..."stringOpenAPI description
fulltext_language = "english"stringPostgreSQL fulltext language
batch_limit = 100integerMax items for batch create/update/delete
max_page_size = 1000integerMax items per page for pagination
operations = MyOpspathCustom CRUDOperations implementation
derive_partial_eqflagDerive PartialEq on generated structs
derive_eqflagDerive Eq on generated structs

§Hook Attributes

Format: {operation}::{cardinality}::{phase} = function_name

OperationCardinalityPhaseDescription
createone, manypre, body, transform, postCreate hooks
readone, manypre, body, transform, postRead hooks
updateone, manypre, body, transform, postUpdate hooks
deleteone, manypre, body, transform, postDelete hooks

Example: #[crudcrate(create::one::pre = validate_input)]

§Field-Level Attributes

Use on fields with #[crudcrate(...)]:

AttributeTypeDescription
primary_keyflagMark as primary key field
filterableflagEnable filtering on this field
sortableflagEnable sorting on this field
fulltextflagInclude in fulltext search
exclude(create)listExclude from create model
exclude(update)listExclude from update model
exclude(one)listExclude from get_one response
exclude(list)listExclude from get_all response
on_create = exprexprAuto-generate value on create
on_update = exprexprAuto-generate value on update
non_db_attrflagMark as non-database field (for joins)
join(one)configLoad in get_one only
join(all)configLoad in get_all only
join(one, all)configLoad in both endpoints
join(one, all, depth = N)configWith max recursion depth (1-5)
join_filterable("col1", "col2")listEnable filtering on join columns
join_sortable("col1", "col2")listEnable sorting on join columns

§Examples

#[derive(EntityToModels)]
#[crudcrate(
    generate_router,
    batch_limit = 500,
    create::one::pre = validate_task,
)]
#[sea_orm(table_name = "tasks")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    #[crudcrate(primary_key, exclude(create, update), on_create = Uuid::new_v4())]
    pub id: Uuid,

    #[crudcrate(filterable, sortable, fulltext)]
    pub title: String,

    #[crudcrate(exclude(create, update), on_create = chrono::Utc::now())]
    pub created_at: DateTime<Utc>,

    #[sea_orm(ignore)]
    #[crudcrate(non_db_attr, join(one, all, depth = 2))]
    pub comments: Vec<Comment>,
}

Module guide: fields/ (field processing) | codegen/ (models, handlers, joins, routes)

Derive Macros§

EntityToModels
Generates complete CRUD API structures from Sea-ORM entities.
ToCreateModel
Generates <Name>Create struct with fields not excluded by exclude(create). Fields with on_create become Option<T> to allow user override. Implements From<NameCreate> for ActiveModel with automatic value generation.
ToListModel
Generates <Name>List struct with fields not excluded by exclude(list). Optimizes API payloads by excluding heavy fields (joins, large text) from list endpoints. Implements From<Name> and From<Model> conversions.
ToUpdateModel
Generates <Name>Update struct with fields not excluded by exclude(update). All fields are Option<Option<T>> to support partial updates and explicit null. Implements MergeIntoActiveModel trait with on_update expression handling.