Skip to main content

Model

Trait Model 

Source
pub trait Model:
    Serialize
    + DeserializeOwned
    + Send
    + Sync
    + Clone {
Show 38 methods // Required methods fn table_name() -> &'static str; fn get_primary_key(&self) -> Option<i64>; fn set_primary_key(&mut self, id: i64); fn columns() -> Vec<&'static str>; fn migration_sql() -> String; fn to_map(&self) -> Result<HashMap<String, Value>>; fn from_map(map: HashMap<String, Value>) -> Result<Self>; // Provided methods fn primary_key() -> &'static str { ... } async fn create(&self, db: &Database) -> Result<Self> { ... } async fn create_or_update(&self, db: &Database) -> Result<Self> { ... } async fn upsert( &self, unique_columns: &[&str], db: &Database, ) -> Result<Self> { ... } async fn bulk_create(models: &[Self], db: &Database) -> Result<Vec<Self>> { ... } async fn find_by_id(id: i64, db: &Database) -> Result<Option<Self>> { ... } async fn find_one( filter: FilterOperator, db: &Database, ) -> Result<Option<Self>> { ... } async fn find_all(db: &Database) -> Result<Vec<Self>> { ... } async fn find_where( filter: FilterOperator, db: &Database, ) -> Result<Vec<Self>> { ... } async fn find_paginated( pagination: &Pagination, db: &Database, ) -> Result<PaginatedResult<Self>> { ... } async fn find_where_paginated( filter: FilterOperator, pagination: &Pagination, db: &Database, ) -> Result<PaginatedResult<Self>> { ... } async fn search( search_filter: &SearchFilter, pagination: Option<&Pagination>, db: &Database, ) -> Result<PaginatedResult<Self>> { ... } async fn count(db: &Database) -> Result<u64> { ... } async fn count_where(filter: FilterOperator, db: &Database) -> Result<u64> { ... } async fn update(&self, db: &Database) -> Result<Self> { ... } async fn bulk_update(models: &[Self], db: &Database) -> Result<Vec<Self>> { ... } async fn delete(&self, db: &Database) -> Result<bool> { ... } async fn bulk_delete(ids: &[i64], db: &Database) -> Result<u64> { ... } async fn delete_where(filter: FilterOperator, db: &Database) -> Result<u64> { ... } async fn list( sort: Option<Vec<Sort>>, pagination: Option<&Pagination>, db: &Database, ) -> Result<PaginatedResult<Self>> { ... } async fn list_where( filter: FilterOperator, sort: Option<Vec<Sort>>, pagination: Option<&Pagination>, db: &Database, ) -> Result<PaginatedResult<Self>> { ... } async fn query(builder: QueryBuilder, db: &Database) -> Result<Vec<Self>> { ... } async fn query_paginated( builder: QueryBuilder, pagination: &Pagination, db: &Database, ) -> Result<PaginatedResult<Self>> { ... } async fn aggregate( function: Aggregate, column: &str, filter: Option<FilterOperator>, db: &Database, ) -> Result<Option<f64>> { ... } fn row_to_map(row: &LibsqlRow) -> Result<HashMap<String, Value>> { ... } fn value_to_libsql_value(value: &Value) -> LibsqlValue { ... } fn libsql_value_to_value(value: &LibsqlValue) -> Value { ... } fn log_info(message: &str) { ... } fn log_debug(message: &str) { ... } fn log_warn(message: &str) { ... } fn log_error(message: &str) { ... }
}
Expand description

Core trait for all database models

Required Methods§

Source

fn table_name() -> &'static str

Get the table name for this model

Source

fn get_primary_key(&self) -> Option<i64>

Get the primary key value

Source

fn set_primary_key(&mut self, id: i64)

Set the primary key value

Source

fn columns() -> Vec<&'static str>

Get all column names for this model

Source

fn migration_sql() -> String

Generate SQL for creating the table

Source

fn to_map(&self) -> Result<HashMap<String, Value>>

Convert the model to a HashMap for database operations

Source

fn from_map(map: HashMap<String, Value>) -> Result<Self>

Create a model from a HashMap

Provided Methods§

Source

fn primary_key() -> &'static str

Get the primary key column name

Source

async fn create(&self, db: &Database) -> Result<Self>

Create a new record in the database

Source

async fn create_or_update(&self, db: &Database) -> Result<Self>

Create or update a record based on whether it has a primary key

Source

async fn upsert(&self, unique_columns: &[&str], db: &Database) -> Result<Self>

Create or update a record based on unique constraints

Source

async fn bulk_create(models: &[Self], db: &Database) -> Result<Vec<Self>>

Create multiple records in the database

Source

async fn find_by_id(id: i64, db: &Database) -> Result<Option<Self>>

Find a record by its primary key

Source

async fn find_one(filter: FilterOperator, db: &Database) -> Result<Option<Self>>

Find a single record by a specific condition

Source

async fn find_all(db: &Database) -> Result<Vec<Self>>

Find all records

Source

async fn find_where(filter: FilterOperator, db: &Database) -> Result<Vec<Self>>

Find records with a filter

Source

async fn find_paginated( pagination: &Pagination, db: &Database, ) -> Result<PaginatedResult<Self>>

Find records with pagination

Source

async fn find_where_paginated( filter: FilterOperator, pagination: &Pagination, db: &Database, ) -> Result<PaginatedResult<Self>>

Find records with filter and pagination

Source

async fn search( search_filter: &SearchFilter, pagination: Option<&Pagination>, db: &Database, ) -> Result<PaginatedResult<Self>>

Search records with text search

Source

async fn count(db: &Database) -> Result<u64>

Count all records

Source

async fn count_where(filter: FilterOperator, db: &Database) -> Result<u64>

Count records with a filter

Source

async fn update(&self, db: &Database) -> Result<Self>

Update a record

Source

async fn bulk_update(models: &[Self], db: &Database) -> Result<Vec<Self>>

Update multiple records

Source

async fn delete(&self, db: &Database) -> Result<bool>

Delete a record

Source

async fn bulk_delete(ids: &[i64], db: &Database) -> Result<u64>

Delete multiple records

Source

async fn delete_where(filter: FilterOperator, db: &Database) -> Result<u64>

Delete records with a filter

Source

async fn list( sort: Option<Vec<Sort>>, pagination: Option<&Pagination>, db: &Database, ) -> Result<PaginatedResult<Self>>

List records with optional sorting and pagination

Source

async fn list_where( filter: FilterOperator, sort: Option<Vec<Sort>>, pagination: Option<&Pagination>, db: &Database, ) -> Result<PaginatedResult<Self>>

List records with filter, sorting, and pagination

Source

async fn query(builder: QueryBuilder, db: &Database) -> Result<Vec<Self>>

Execute a custom query

Source

async fn query_paginated( builder: QueryBuilder, pagination: &Pagination, db: &Database, ) -> Result<PaginatedResult<Self>>

Execute a custom query with pagination

Source

async fn aggregate( function: Aggregate, column: &str, filter: Option<FilterOperator>, db: &Database, ) -> Result<Option<f64>>

Get aggregate value

Source

fn row_to_map(row: &LibsqlRow) -> Result<HashMap<String, Value>>

Convert a database row to a HashMap

Source

fn value_to_libsql_value(value: &Value) -> LibsqlValue

Convert our Value type to crate::compat::LibsqlValue

Source

fn libsql_value_to_value(value: &LibsqlValue) -> Value

Convert crate::compat::LibsqlValue to our Value type

Source

fn log_info(message: &str)

Log an info message

Source

fn log_debug(message: &str)

Log a debug message

Source

fn log_warn(message: &str)

Log a warning message

Source

fn log_error(message: &str)

Log an error message

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§