Skip to main content

Database

Struct Database 

Source
pub struct Database { /* private fields */ }
Expand description

A Toasty database handle with Phoenix deployment metadata.

Implementations§

Source§

impl Database

Source

pub fn builder(models: ModelSet) -> DatabaseBuilder

Start configuring a database with the application’s Toasty models.

Source

pub async fn sqlite_memory(models: ModelSet) -> Result<Database, 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.

Source

pub const fn backend(&self) -> Backend

Return the configured database backend.

Source

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.

Source

pub const fn toasty(&self) -> &Db

Borrow the native Toasty database handle.

Source

pub const fn toasty_mut(&mut self) -> &mut Db

Mutably borrow the native Toasty database handle.

Methods from Deref<Target = Db>§

Source

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.

Source

pub async fn push_schema(&self) -> Result<(), Error>

Creates tables and indices defined in the schema on the database.

Source

pub async fn reset_db(&self) -> Result<(), Error>

Drops the entire database and recreates an empty one without applying migrations.

Source

pub fn driver(&self) -> &(dyn Driver + 'static)

Returns a reference to the underlying database driver.

Source

pub fn schema(&self) -> &Arc<Schema>

Returns the compiled schema used by this database handle.

Source

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).

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Database

Source§

fn clone(&self) -> Database

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Database

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Deref for Database

Source§

type Target = Db

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<Database as Deref>::Target

Dereferences the value.
Source§

impl DerefMut for Database

Source§

fn deref_mut(&mut self) -> &mut <Database as Deref>::Target

Mutably dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more