pub trait Model: Sized {
// Required methods
fn table_name() -> &'static str;
fn columns() -> &'static [&'static str];
// Provided methods
fn primary_key() -> &'static str { ... }
fn soft_delete_column() -> Option<&'static str> { ... }
fn timestamp_columns() -> Option<(&'static str, &'static str)> { ... }
fn tenant_column() -> Option<&'static str> { ... }
fn query() -> QueryBuilder<Self> { ... }
fn without_tenant_scope() -> QueryBuilder<Self> { ... }
fn pk_value(&self) -> SqlValue { ... }
fn find(id: impl Into<SqlValue>) -> QueryBuilder<Self> { ... }
fn nearest_to(embedding: &[f32], k: usize) -> QueryBuilder<Self> { ... }
fn where_cosine_distance(
col: &str,
embedding: &[f32],
op: &str,
threshold: f64,
) -> QueryBuilder<Self> { ... }
}Expand description
The core ORM trait. Implemented via #[derive(Model)] from rok-orm.
use rok_orm::Model;
#[derive(Model)]
pub struct Post {
pub id: i64,
pub title: String,
pub body: String,
}
let q = Post::query()
.where_eq("title", "Hello")
.order_by_desc("id")
.limit(10);
let (sql, params) = q.to_sql();
assert!(sql.starts_with("SELECT * FROM posts"));Required Methods§
Sourcefn table_name() -> &'static str
fn table_name() -> &'static str
SQL table name (e.g. "users").
Provided Methods§
Sourcefn primary_key() -> &'static str
fn primary_key() -> &'static str
Primary-key column. Defaults to "id".
Sourcefn soft_delete_column() -> Option<&'static str>
fn soft_delete_column() -> Option<&'static str>
The soft-delete column name, if this model uses soft deletes.
When Some("deleted_at"), fluent queries automatically append
WHERE deleted_at IS NULL and delete operations update the timestamp
instead of issuing a DELETE FROM.
Sourcefn timestamp_columns() -> Option<(&'static str, &'static str)>
fn timestamp_columns() -> Option<(&'static str, &'static str)>
Auto-timestamp column names (created_at, updated_at), if configured.
Used for introspection; the DB DEFAULT NOW() handles the actual value.
Sourcefn tenant_column() -> Option<&'static str>
fn tenant_column() -> Option<&'static str>
The tenant column for row-level multi-tenancy, if this model is tenant-scoped.
Set via #[rok_orm(tenant_column = "tenant_id")] on the struct.
When set and a tenant is active (via TenantLayer), all queries produced by
query() automatically include WHERE tenant_id = $current_tenant.
Sourcefn query() -> QueryBuilder<Self>
fn query() -> QueryBuilder<Self>
Start a new QueryBuilder scoped to this model.
If this model has a tenant_column and the tenant feature is enabled,
the current request’s tenant ID (from TenantLayer) is automatically
injected as the first WHERE condition.
Sourcefn without_tenant_scope() -> QueryBuilder<Self>
fn without_tenant_scope() -> QueryBuilder<Self>
Start a new QueryBuilder that bypasses the tenant scope.
Use for admin/cross-tenant queries where the tenant filter should not apply.
Sourcefn pk_value(&self) -> SqlValue
fn pk_value(&self) -> SqlValue
The primary key value of this model instance as a [SqlValue].
Generated automatically by #[derive(Model)]. The default panics to
catch models that weren’t generated via the derive macro.
Sourcefn find(id: impl Into<SqlValue>) -> QueryBuilder<Self>
fn find(id: impl Into<SqlValue>) -> QueryBuilder<Self>
Build a SELECT … WHERE <pk> = $1 query.
Sourcefn nearest_to(embedding: &[f32], k: usize) -> QueryBuilder<Self>
fn nearest_to(embedding: &[f32], k: usize) -> QueryBuilder<Self>
KNN similarity search: ORDER BY {col} <-> embedding LIMIT k.
Uses the "embedding" column by default. For a different column use
Self::query().nearest_to("other_col", embedding, k).
Sourcefn where_cosine_distance(
col: &str,
embedding: &[f32],
op: &str,
threshold: f64,
) -> QueryBuilder<Self>
fn where_cosine_distance( col: &str, embedding: &[f32], op: &str, threshold: f64, ) -> QueryBuilder<Self>
Cosine-distance filter: WHERE {col} <=> embedding {op} {threshold}.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.