pub struct Database { /* private fields */ }Expand description
Async database using the backend selected by the crate feature.
Implementations§
Source§impl Database
impl Database
Sourcepub fn connect_lazy(url: impl Into<String>) -> Result<Database, OrmError>
pub fn connect_lazy(url: impl Into<String>) -> Result<Database, OrmError>
Creates a pool without opening a connection. Configuration is validated immediately; connectivity is checked on first use. Useful for offline schema/API metadata discovery, which must not execute database queries.
Sourcepub fn connect_lazy_with_pool_size(
url: impl Into<String>,
size: usize,
) -> Result<Database, OrmError>
pub fn connect_lazy_with_pool_size( url: impl Into<String>, size: usize, ) -> Result<Database, OrmError>
Creates an unconnected pool with an explicit positive connection limit.
Like Self::connect_lazy, this checks configuration, not connectivity.
pub async fn connect(url: impl Into<String>) -> Result<Database, OrmError>
pub async fn connect_with_pool_size( url: impl Into<String>, size: usize, ) -> Result<Database, OrmError>
pub async fn connect_in_memory() -> Result<Database, OrmError>
pub async fn execute(&self, ast: QueryAst) -> Result<ExecuteResult, OrmError>
Sourcepub fn with_model_runtime(self, runtime: Arc<dyn ModelRuntime>) -> Database
pub fn with_model_runtime(self, runtime: Arc<dyn ModelRuntime>) -> Database
Bind model-aware behavior to this database handle and its transactions.
pub fn model_role<M>(&self) -> Option<ModelRole>where
M: Model,
Sourcepub async fn execute_sql(
&self,
sql: impl Into<String>,
params: Vec<DatabaseValue>,
) -> Result<ExecuteResult, OrmError>
pub async fn execute_sql( &self, sql: impl Into<String>, params: Vec<DatabaseValue>, ) -> Result<ExecuteResult, OrmError>
Executes explicitly supplied SQL with bound values. SQL syntax is backend-specific.
pub async fn query_sql( &self, sql: impl Into<String>, params: Vec<DatabaseValue>, ) -> Result<Vec<Row>, OrmError>
pub async fn create_table<M>(&self) -> Result<ExecuteResult, OrmError>where
M: Model,
Sourcepub async fn insert<M>(&self, model: &M) -> Result<ExecuteResult, OrmError>where
M: Model,
pub async fn insert<M>(&self, model: &M) -> Result<ExecuteResult, OrmError>where
M: Model,
Inserts supplied UUID keys, omitting generated integer keys and managed timestamps.
Sourcepub async fn save<M>(&self, model: &M) -> Result<Option<M>, OrmError>
pub async fn save<M>(&self, model: &M) -> Result<Option<M>, OrmError>
Updates an existing row by its primary key using all ordinary model fields.
Preserves the stored primary key and auto_now_add fields, refreshes
auto_now, and returns the row produced by RETURNING. The supplied
object is not modified. Returns None if no row matches; never inserts.
Models without writable fields or managed updates return EmptyUpdate.
Configured model runtimes capture this write just like update.
All ordinary fields are written, including unchanged values; use
update().set(...) to update only selected fields of a stale object.
pub async fn fetch_all<M>( &self, query: SelectQuery<M>, ) -> Result<Vec<M>, OrmError>
pub async fn fetch_one<M>( &self, query: SelectQuery<M>, ) -> Result<Option<M>, OrmError>
pub async fn get<M>( &self, id: <M as Model>::PrimaryKey, ) -> Result<Option<M>, OrmError>
pub async fn all<M>(&self) -> Result<Vec<M>, OrmError>
pub async fn count<M>(&self) -> Result<usize, OrmError>
pub async fn count_query<M>( &self, query: SelectQuery<M>, ) -> Result<usize, OrmError>
pub fn create<M>(&self) -> CreateBuilder<'_, M>where
M: Model,
pub fn update<M>(&self, id: <M as Model>::PrimaryKey) -> UpdateBuilder<'_, M>where
M: Model,
pub async fn delete<M>( &self, id: <M as Model>::PrimaryKey, ) -> Result<bool, OrmError>
Sourcepub fn query<M>(&self) -> BoundQuery<'_, DatabaseQuery<M>>where
M: Model,
pub fn query<M>(&self) -> BoundQuery<'_, DatabaseQuery<M>>where
M: Model,
Builds a query bound to this database; execute it with .all(),
.first() or .count() without passing the database again.
pub async fn fetch_by<M, T, V>( &self, field: ModelField<M, T>, value: V, ) -> Result<Vec<M>, OrmError>
pub async fn fetch_by_many<M, T, I, V>( &self, field: ModelField<M, T>, values: I, ) -> Result<Vec<M>, OrmError>
Sourcepub async fn transaction<T, F>(&self, action: F) -> Result<T, OrmError>
pub async fn transaction<T, F>(&self, action: F) -> Result<T, OrmError>
Runs one serializable async ORM transaction. Unfinished transactions are
rolled back before a cancelled transaction’s connection is recycled.
Use |tx| Box::pin(async move { ... }) and await transaction methods.
Sourcepub async fn transaction_with_isolation<T, F>(
&self,
isolation: IsolationLevel,
action: F,
) -> Result<T, OrmError>
pub async fn transaction_with_isolation<T, F>( &self, isolation: IsolationLevel, action: F, ) -> Result<T, OrmError>
SQLite uses its serializable snapshot for every requested isolation level.
Sourcepub async fn transaction_result<T, E, F>(&self, action: F) -> Result<T, E>
pub async fn transaction_result<T, E, F>(&self, action: F) -> Result<T, E>
Transaction with an application error type; every error rolls back.
Sourcepub async fn raw_transaction<T, F>(&self, action: F) -> Result<T, OrmError>
pub async fn raw_transaction<T, F>(&self, action: F) -> Result<T, OrmError>
SQLite-only escape hatch for direct driver operations.