Entity Structure
Kit uses SeaORM entities under the hood. When you runkit db:sync, two files are generated for each table:
- Auto-generated entity (
src/models/entities/{table}.rs) - SeaORM entity definition, regenerated on sync - User model (
src/models/{table}.rs) - Your custom code with the fluent API, never overwritten
Generating Models
Use thedb:sync command to generate models from your database:
Eloquent-like Fluent API
The generated models provide a fluent, chainable API similar to Laravel’s Eloquent.Querying Records
UseModel::query() to start a fluent query builder:
Creating Records
UseModel::create() to get a fluent builder for creating new records:
Updating Records
Chain setters on an existing model and callupdate():
Deleting Records
Calldelete() on a model instance:
Complete Example
Here’s a complete CRUD controller using the fluent API:Model and ModelMut Traits
The fluent API is built on top of two core traits that you can also use directly:The Model Trait
Provides read-only operations on the Entity:Implementing
kit::database::Model also enables Route Model Binding, allowing you to use Model types directly as handler parameters for automatic database lookup.The ModelMut Trait
Provides write operations:Working with ActiveModel
For advanced use cases, you can work directly with SeaORM’sActiveModel: