Entity & Model
The trait
Every generated entity implements zyvor::Entity:
#![allow(unused)]
fn main() {
pub trait Entity {
type Model;
const TABLE: &'static str;
const COLUMNS: &'static [Column];
fn columns() -> &'static [Column] { Self::COLUMNS }
fn find() -> Query<Self> where Self: Sized;
async fn create<'c, D, A>(db: A, data: D) -> Result<Self::Model>
where
D: InsertData<Self>,
A: sqlx::Acquire<'c, Database = sqlx::Postgres>,
for<'r> Self::Model: sqlx::FromRow<'r, PgRow> + Send + Unpin;
async fn update<'c, D, A>(db: A, id: i32, data: D) -> Result<Self::Model>;
async fn delete<'c, A>(db: A, id: i32) -> Result<u64>;
async fn create_many<'c, D, A>(db: A, data: Vec<D>) -> Result<Vec<Self::Model>>;
async fn update_many<'c, D, A>(db: A, condition: Condition<Self>, data: D) -> Result<u64>;
async fn delete_many<'c, A>(db: A, condition: Condition<Self>) -> Result<u64>;
}
}
Signatures abbreviated — update, delete, and the bulk methods carry the same
Acquire bound as create.
Three things to notice:
A: sqlx::Acquire— that’s why one call works with both&PgPooland&mut Transaction. See Transactions.D: InsertData<Self>/UpdateData<Self>— the generatedCreate/Updatestructs implement these. You can hand-write your own input type by implementing the trait.id: i32— hardcoded. See the limitation below.
Two types per model
| Type | Role |
|---|---|
User | zero-sized marker. Holds constants and typed fields. Never instantiated. |
UserModel | the actual row. Debug + Serialize + Deserialize + FromRow. |
#![allow(unused)]
fn main() {
pub struct User; // entity: User::find(), User::id, User::posts
pub struct UserModel { /* ... */ } // data: what you get back
}
User::find() returns Query<User>, and Query::all() returns
Vec<UserModel> — the association is Entity::Model.
Column
#![allow(unused)]
fn main() {
impl Column {
pub const fn new(name: &'static str) -> Self;
pub const fn qualified(table: &'static str, name: &'static str) -> Self;
pub const fn name(&self) -> &'static str;
pub const fn table(&self) -> Option<&'static str>;
}
}
Column is Debug + Clone + Copy. Unqualified columns are what generated
COLUMNS contains; qualified is used internally for joins and subqueries.
You’ll pass Column to group_by:
#![allow(unused)]
fn main() {
.group_by(User::name.column())
}
Relation traits
Generated models implement these so the eager loader can attach children:
#![allow(unused)]
fn main() {
pub trait RelationKey {
fn relation_key(&self, column: Column) -> Option<i64>;
}
pub trait RelationLoader<Related> {
fn load_relation(&mut self, related: Vec<Related>); // collections
}
pub trait SingleRelationLoader<Related> {
fn load_relation(&mut self, related: Option<Related>); // single relations
}
}
RelationKey returning Option<i64> is why keys are normalized to i64
internally even though your id is i32.
You rarely touch these directly — but if you hand-write a model, they’re what
you implement to make .with() work.
⚠️ Primary keys must be Int
update and delete take id: i32 concretely. A String or UUID primary key
will generate a model, but you won’t be able to call User::update / User::delete
on it. update_many / delete_many take a Condition, so they still work.
Use Int @id @default(autoincrement()) for now.
Two error types in the API
| Method | Returns |
|---|---|
create, update, delete, bulk variants | zyvor::error::Result<T> (= Result<T, zyvor::Error>) |
Query::all, count, fetch_all | Result<T, sqlx::Error> |
zyvor::Error is a typed enum that auto-classifies unique and foreign-key
violations. Queries return the raw sqlx error — convert it when you want the
same classification:
#![allow(unused)]
fn main() {
let users = User::find().all(&pool).await
.map_err(zyvor::Error::from)?;
}
See Errors & HTTP Status for matching patterns and the current caveats.