Models and fields¶
Fexu models are Rust structs deriving FexuModel. The CLI creates and registers a model in the correct app automatically.
fexu new app catalog
fexu generate model Book --app catalog
File: apps/catalog/models/book.rs (generated by the fullstack template):
use fexu::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(FexuModel, Serialize, Deserialize)]
#[fexu(
table = "books",
api = "/api/v1/books",
admin = true,
admin_label = "Book",
permissions = "books"
)]
pub struct Book {
#[field(primary_key, auto)]
pub id: Uuid,
#[field(required, min_length = 1)]
pub name: String,
}
For an existing table, create a migration after changing the struct:
fexu migrate make add_stock --app catalog --backfill books.stock:i64=0
fexu migrate plan
fexu migrate up
Supported model field types include UUIDs, strings, integers, booleans, decimal values, date/time values and nullable forms of supported values. Field attributes provide validation and schema metadata.
#[field(min = 0)]
pub stock: i64,
Place the field inside the Book struct, then generate the migration. Fexu does not silently alter an existing database just because you changed a Rust struct. For authenticated models, permissions remain mandatory for protected operations.
See database, migrations, and the complete tutorial.