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§
Sourcefn table_name() -> &'static str
fn table_name() -> &'static str
Get the table name for this model
Sourcefn get_primary_key(&self) -> Option<i64>
fn get_primary_key(&self) -> Option<i64>
Get the primary key value
Sourcefn set_primary_key(&mut self, id: i64)
fn set_primary_key(&mut self, id: i64)
Set the primary key value
Sourcefn migration_sql() -> String
fn migration_sql() -> String
Generate SQL for creating the table
Provided Methods§
Sourcefn primary_key() -> &'static str
fn primary_key() -> &'static str
Get the primary key column name
Sourceasync fn create_or_update(&self, db: &Database) -> Result<Self>
async fn create_or_update(&self, db: &Database) -> Result<Self>
Create or update a record based on whether it has a primary key
Sourceasync fn upsert(&self, unique_columns: &[&str], db: &Database) -> Result<Self>
async fn upsert(&self, unique_columns: &[&str], db: &Database) -> Result<Self>
Create or update a record based on unique constraints
Sourceasync fn bulk_create(models: &[Self], db: &Database) -> Result<Vec<Self>>
async fn bulk_create(models: &[Self], db: &Database) -> Result<Vec<Self>>
Create multiple records in the database
Sourceasync fn find_by_id(id: i64, db: &Database) -> Result<Option<Self>>
async fn find_by_id(id: i64, db: &Database) -> Result<Option<Self>>
Find a record by its primary key
Sourceasync fn find_one(filter: FilterOperator, db: &Database) -> Result<Option<Self>>
async fn find_one(filter: FilterOperator, db: &Database) -> Result<Option<Self>>
Find a single record by a specific condition
Sourceasync fn find_where(filter: FilterOperator, db: &Database) -> Result<Vec<Self>>
async fn find_where(filter: FilterOperator, db: &Database) -> Result<Vec<Self>>
Find records with a filter
Sourceasync fn find_paginated(
pagination: &Pagination,
db: &Database,
) -> Result<PaginatedResult<Self>>
async fn find_paginated( pagination: &Pagination, db: &Database, ) -> Result<PaginatedResult<Self>>
Find records with pagination
Sourceasync fn find_where_paginated(
filter: FilterOperator,
pagination: &Pagination,
db: &Database,
) -> Result<PaginatedResult<Self>>
async fn find_where_paginated( filter: FilterOperator, pagination: &Pagination, db: &Database, ) -> Result<PaginatedResult<Self>>
Find records with filter and pagination
Sourceasync fn search(
search_filter: &SearchFilter,
pagination: Option<&Pagination>,
db: &Database,
) -> Result<PaginatedResult<Self>>
async fn search( search_filter: &SearchFilter, pagination: Option<&Pagination>, db: &Database, ) -> Result<PaginatedResult<Self>>
Search records with text search
Sourceasync fn count_where(filter: FilterOperator, db: &Database) -> Result<u64>
async fn count_where(filter: FilterOperator, db: &Database) -> Result<u64>
Count records with a filter
Sourceasync fn bulk_update(models: &[Self], db: &Database) -> Result<Vec<Self>>
async fn bulk_update(models: &[Self], db: &Database) -> Result<Vec<Self>>
Update multiple records
Sourceasync fn delete_where(filter: FilterOperator, db: &Database) -> Result<u64>
async fn delete_where(filter: FilterOperator, db: &Database) -> Result<u64>
Delete records with a filter
Sourceasync fn list(
sort: Option<Vec<Sort>>,
pagination: Option<&Pagination>,
db: &Database,
) -> Result<PaginatedResult<Self>>
async fn list( sort: Option<Vec<Sort>>, pagination: Option<&Pagination>, db: &Database, ) -> Result<PaginatedResult<Self>>
List records with optional sorting and pagination
Sourceasync fn list_where(
filter: FilterOperator,
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>>
List records with filter, sorting, and pagination
Sourceasync fn query(builder: QueryBuilder, db: &Database) -> Result<Vec<Self>>
async fn query(builder: QueryBuilder, db: &Database) -> Result<Vec<Self>>
Execute a custom query
Sourceasync fn query_paginated(
builder: QueryBuilder,
pagination: &Pagination,
db: &Database,
) -> Result<PaginatedResult<Self>>
async fn query_paginated( builder: QueryBuilder, pagination: &Pagination, db: &Database, ) -> Result<PaginatedResult<Self>>
Execute a custom query with pagination
Sourceasync fn aggregate(
function: Aggregate,
column: &str,
filter: Option<FilterOperator>,
db: &Database,
) -> Result<Option<f64>>
async fn aggregate( function: Aggregate, column: &str, filter: Option<FilterOperator>, db: &Database, ) -> Result<Option<f64>>
Get aggregate value
Sourcefn row_to_map(row: &LibsqlRow) -> Result<HashMap<String, Value>>
fn row_to_map(row: &LibsqlRow) -> Result<HashMap<String, Value>>
Convert a database row to a HashMap
Sourcefn value_to_libsql_value(value: &Value) -> LibsqlValue
fn value_to_libsql_value(value: &Value) -> LibsqlValue
Convert our Value type to crate::compat::LibsqlValue
Sourcefn libsql_value_to_value(value: &LibsqlValue) -> Value
fn libsql_value_to_value(value: &LibsqlValue) -> Value
Convert crate::compat::LibsqlValue to our Value type
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".