use crate::query::QueryBuilder;
pub trait Model: Sized {
fn table_name() -> &'static str;
fn primary_key() -> &'static str {
"id"
}
fn columns() -> &'static [&'static str];
fn soft_delete_column() -> Option<&'static str> {
None
}
fn timestamp_columns() -> Option<(&'static str, &'static str)> {
None
}
fn tenant_column() -> Option<&'static str> {
None
}
fn query() -> QueryBuilder<Self> {
let q = QueryBuilder::new(Self::table_name());
#[cfg(feature = "tenant")]
if let Some(col) = Self::tenant_column() {
if let Some(tid) = crate::tenant::current_tenant_id() {
return q.where_eq(col, tid);
}
}
q
}
fn without_tenant_scope() -> QueryBuilder<Self> {
QueryBuilder::new(Self::table_name())
}
fn pk_value(&self) -> crate::condition::SqlValue {
panic!(
"`pk_value` not implemented for `{}` — use `#[derive(Model)]` to generate it",
std::any::type_name::<Self>()
)
}
fn find(id: impl Into<crate::condition::SqlValue>) -> QueryBuilder<Self> {
Self::query().where_eq(Self::primary_key(), id)
}
fn nearest_to(embedding: &[f32], k: usize) -> QueryBuilder<Self> {
Self::query().nearest_to("embedding", embedding, k)
}
fn where_cosine_distance(
col: &str,
embedding: &[f32],
op: &str,
threshold: f64,
) -> QueryBuilder<Self> {
Self::query().where_cosine_distance(col, embedding, op, threshold)
}
}