pub struct Database { /* private fields */ }Expand description
A Toasty database handle with Phoenix deployment metadata.
Implementations§
Source§impl Database
impl Database
Sourcepub fn builder(models: ModelSet) -> DatabaseBuilder
pub fn builder(models: ModelSet) -> DatabaseBuilder
Start configuring a database with the application’s Toasty models.
Sourcepub async fn sqlite_memory(models: ModelSet) -> Result<Self, DatabaseError>
pub async fn sqlite_memory(models: ModelSet) -> Result<Self, DatabaseError>
Create a single-connection, isolated in-memory SQLite database.
The schema is not created until Self::initialize_schema is called.
§Errors
Returns an error when the SQLite driver cannot initialize.
Sourcepub async fn initialize_schema(&self) -> Result<(), DatabaseError>
pub async fn initialize_schema(&self) -> Result<(), DatabaseError>
Create all Toasty model tables in a new, empty database.
§Errors
Returns an error when Toasty cannot apply the compiled model schema.
Sourcepub const fn toasty_mut(&mut self) -> &mut Db
pub const fn toasty_mut(&mut self) -> &mut Db
Mutably borrow the native Toasty database handle.
Methods from Deref<Target = Db>§
Sourcepub async fn connection(&self) -> Result<Connection, Error>
pub async fn connection(&self) -> Result<Connection, Error>
Acquire a dedicated connection from the pool.
The returned Connection implements Executor and pins all
operations to the same physical connection. This is useful when
multiple statements must share connection-level state such as
temporary tables or session variables.
When the Connection is dropped it is returned to the pool for reuse.
Sourcepub async fn push_schema(&self) -> Result<(), Error>
pub async fn push_schema(&self) -> Result<(), Error>
Creates tables and indices defined in the schema on the database.
Sourcepub async fn reset_db(&self) -> Result<(), Error>
pub async fn reset_db(&self) -> Result<(), Error>
Drops the entire database and recreates an empty one without applying migrations.
Sourcepub fn driver(&self) -> &(dyn Driver + 'static)
pub fn driver(&self) -> &(dyn Driver + 'static)
Returns a reference to the underlying database driver.
Sourcepub fn schema(&self) -> &Arc<Schema> ⓘ
pub fn schema(&self) -> &Arc<Schema> ⓘ
Returns the compiled schema used by this database handle.
Sourcepub fn capability(&self) -> &Capability
pub fn capability(&self) -> &Capability
Returns the capability flags reported by the driver.
The query engine uses these to decide which operation types to generate (e.g., SQL vs. key-value).
Sourcepub async fn transaction(&mut self) -> Result<Transaction<'_>, Error>
pub async fn transaction(&mut self) -> Result<Transaction<'_>, Error>
Begin a transaction, acquiring a connection from the pool.
Takes &mut self so the Db handle is exclusively borrowed while the
transaction is open. This prevents accidentally running a statement
against the pool — which would execute on a separate connection and
bypass the transaction — when you meant to run it against &mut tx.
If you need a second handle while the transaction is open, clone the
Db before calling this method. Clones share the same pool.
Sourcepub fn transaction_builder(&mut self) -> TransactionBuilder<'_>
pub fn transaction_builder(&mut self) -> TransactionBuilder<'_>
Returns a TransactionBuilder that will acquire a connection from
the pool when begin is called.
Like transaction, this takes &mut self so the
Db handle stays locked for the lifetime of the transaction. Clone
the Db beforehand if you need a separate handle.