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(...)]:
| Attribute | Type | Description |
|---|---|---|
generate_router | flag | Generate Axum router function |
api_struct = "Name" | string | Override generated struct name |
name_singular = "item" | string | Singular resource name for errors/headers |
name_plural = "items" | string | Plural resource name for routes |
description = "..." | string | OpenAPI description |
fulltext_language = "english" | string | PostgreSQL fulltext language |
batch_limit = 100 | integer | Max items for batch create/update/delete |
max_page_size = 1000 | integer | Max items per page for pagination |
operations = MyOps | path | Custom CRUDOperations implementation |
derive_partial_eq | flag | Derive PartialEq on generated structs |
derive_eq | flag | Derive Eq on generated structs |
§Hook Attributes
Format: {operation}::{cardinality}::{phase} = function_name
| Operation | Cardinality | Phase | Description |
|---|---|---|---|
create | one, many | pre, body, transform, post | Create hooks |
read | one, many | pre, body, transform, post | Read hooks |
update | one, many | pre, body, transform, post | Update hooks |
delete | one, many | pre, body, transform, post | Delete hooks |
Example: #[crudcrate(create::one::pre = validate_input)]
§Field-Level Attributes
Use on fields with #[crudcrate(...)]:
| Attribute | Type | Description |
|---|---|---|
primary_key | flag | Mark as primary key field |
filterable | flag | Enable filtering on this field |
sortable | flag | Enable sorting on this field |
fulltext | flag | Include in fulltext search |
exclude(create) | list | Exclude from create model |
exclude(update) | list | Exclude from update model |
exclude(one) | list | Exclude from get_one response |
exclude(list) | list | Exclude from get_all response |
on_create = expr | expr | Auto-generate value on create |
on_update = expr | expr | Auto-generate value on update |
non_db_attr | flag | Mark as non-database field (for joins) |
join(one) | config | Load in get_one only |
join(all) | config | Load in get_all only |
join(one, all) | config | Load in both endpoints |
join(one, all, depth = N) | config | With max recursion depth (1-5) |
join_filterable("col1", "col2") | list | Enable filtering on join columns |
join_sortable("col1", "col2") | list | Enable 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§
- Entity
ToModels - Generates complete CRUD API structures from Sea-ORM entities.
- ToCreate
Model - Generates
<Name>Createstruct with fields not excluded byexclude(create). Fields withon_createbecomeOption<T>to allow user override. ImplementsFrom<NameCreate>forActiveModelwith automatic value generation. - ToList
Model - Generates
<Name>Liststruct with fields not excluded byexclude(list). Optimizes API payloads by excluding heavy fields (joins, large text) from list endpoints. ImplementsFrom<Name>andFrom<Model>conversions. - ToUpdate
Model - Generates
<Name>Updatestruct with fields not excluded byexclude(update). All fields areOption<Option<T>>to support partial updates and explicit null. ImplementsMergeIntoActiveModeltrait withon_updateexpression handling.