pub struct Database { /* private fields */ }Expand description
Async SQLite database backed by a deadpool-sqlite connection pool.
Implementations§
Source§impl Database
impl Database
Sourcepub fn pool(&self) -> &Pool
pub fn pool(&self) -> &Pool
Returns the underlying SQLite pool for advanced driver-specific use.
Sourcepub fn connect(path: impl Into<String>) -> Result<Self, OrmError>
pub fn connect(path: impl Into<String>) -> Result<Self, OrmError>
Opens a SQLite database with a pool size of four connections.
Sourcepub fn connect_with_pool_size(
path: impl Into<String>,
pool_size: usize,
) -> Result<Self, OrmError>
pub fn connect_with_pool_size( path: impl Into<String>, pool_size: usize, ) -> Result<Self, OrmError>
Opens a SQLite database with an explicit maximum pool size.
Sourcepub fn connect_in_memory() -> Result<Self, OrmError>
pub fn connect_in_memory() -> Result<Self, OrmError>
Opens an in-memory SQLite database with a single connection.
Sourcepub async fn execute(&self, ast: QueryAst) -> Result<ExecuteResult, OrmError>
pub async fn execute(&self, ast: QueryAst) -> Result<ExecuteResult, OrmError>
Executes a compiled AST query asynchronously.
Sourcepub async fn create_table<M: Model>(&self) -> Result<ExecuteResult, OrmError>
pub async fn create_table<M: Model>(&self) -> Result<ExecuteResult, OrmError>
Creates a model table and its indexes.
Sourcepub async fn insert<M: Model>(
&self,
model: &M,
) -> Result<ExecuteResult, OrmError>
pub async fn insert<M: Model>( &self, model: &M, ) -> Result<ExecuteResult, OrmError>
Inserts a model. Primary key and managed timestamp fields are omitted.
Sourcepub async fn fetch_all<M: Model + Send + 'static>(
&self,
query: SelectQuery<M>,
) -> Result<Vec<M>, OrmError>
pub async fn fetch_all<M: Model + Send + 'static>( &self, query: SelectQuery<M>, ) -> Result<Vec<M>, OrmError>
Fetches all rows matching a typed select query.
Sourcepub async fn fetch_one<M: Model + Send + 'static>(
&self,
query: SelectQuery<M>,
) -> Result<Option<M>, OrmError>
pub async fn fetch_one<M: Model + Send + 'static>( &self, query: SelectQuery<M>, ) -> Result<Option<M>, OrmError>
Fetches at most one row matching a typed select query.
Sourcepub async fn get<M: Model + Send + 'static>(
&self,
id: i64,
) -> Result<Option<M>, OrmError>
pub async fn get<M: Model + Send + 'static>( &self, id: i64, ) -> Result<Option<M>, OrmError>
Fetches a model by its generated integer primary key.
Sourcepub async fn all<M: Model + Send + 'static>(&self) -> Result<Vec<M>, OrmError>
pub async fn all<M: Model + Send + 'static>(&self) -> Result<Vec<M>, OrmError>
Fetches every row in a model’s table.
pub async fn count<M: Model + Send + 'static>(&self) -> Result<usize, OrmError>
pub async fn count_query<M: Model + Send + 'static>( &self, query: SelectQuery<M>, ) -> Result<usize, OrmError>
Sourcepub fn create<M: Model>(&self) -> CreateBuilder<'_, M>
pub fn create<M: Model>(&self) -> CreateBuilder<'_, M>
Starts a high-level insert builder.
Sourcepub fn update<M: Model>(&self, id: i64) -> UpdateBuilder<'_, M>
pub fn update<M: Model>(&self, id: i64) -> UpdateBuilder<'_, M>
Starts a high-level update builder scoped to one primary key.
Sourcepub async fn delete<M: Model + Send + 'static>(
&self,
id: i64,
) -> Result<bool, OrmError>
pub async fn delete<M: Model + Send + 'static>( &self, id: i64, ) -> Result<bool, OrmError>
Deletes one row by its generated integer primary key.
Sourcepub fn query<M: Model>(&self) -> DatabaseQuery<M>
pub fn query<M: Model>(&self) -> DatabaseQuery<M>
Starts a high-level typed select builder bound to this database.
Sourcepub async fn fetch_by<M, T, V>(
&self,
field: ModelField<M, T>,
value: V,
) -> Result<Vec<M>, OrmError>
pub async fn fetch_by<M, T, V>( &self, field: ModelField<M, T>, value: V, ) -> Result<Vec<M>, OrmError>
Fetches related rows by a typed model field.
This is the low-level building block for has_many and belongs_to:
pass the foreign-key field on the target model and the owner key.
Sourcepub async fn fetch_by_many<M, T, I, V>(
&self,
field: ModelField<M, T>,
values: I,
) -> 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>
Fetches rows whose typed field matches one of the supplied values.
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 a blocking SQLite closure in a pool worker transaction.