pub struct Db { /* private fields */ }Expand description
The application’s database, registered as a singleton in providers().
Implementations§
Source§impl Db
impl Db
pub async fn connect(url: impl Into<String>) -> Result<Self>
Sourcepub async fn in_memory() -> Result<Self>
pub async fn in_memory() -> Result<Self>
An isolated in-memory SQLite database.
The pool is capped at one connection deliberately: SQLite gives every
connection its own :memory: database, so a larger pool would hand
out connections that cannot see each other’s tables.
pub fn from_connection(connection: DatabaseConnection) -> Self
pub fn connection(&self) -> &DatabaseConnection
Sourcepub async fn scope<T>(&self, work: impl Future<Output = T>) -> T
pub async fn scope<T>(&self, work: impl Future<Output = T>) -> T
Run work with this database ambient, so User::find(..) inside it
resolves. The request adapter wraps every action in one of these.
Sourcepub async fn rollback_scope<T>(
&self,
work: impl AsyncFnOnce() -> T,
) -> Result<T>
pub async fn rollback_scope<T>( &self, work: impl AsyncFnOnce() -> T, ) -> Result<T>
Run work inside a transaction that is always rolled back.
This is the harness behind #[luxid::test]: tests share one database,
run in parallel, and leave nothing behind — no truncation, no fixtures,
no ordering constraints.
Sourcepub async fn transaction<T>(
&self,
work: impl AsyncFnOnce() -> Result<T>,
) -> Result<T>
pub async fn transaction<T>( &self, work: impl AsyncFnOnce() -> Result<T>, ) -> Result<T>
Run work in a transaction, committing on Ok and rolling back on
Err.
Source§impl Db
impl Db
Sourcepub async fn migrate<M: MigratorTrait>(&self) -> Result<()>
pub async fn migrate<M: MigratorTrait>(&self) -> Result<()>
Apply every pending migration.
Sourcepub async fn migrate_steps<M: MigratorTrait>(&self, steps: u32) -> Result<()>
pub async fn migrate_steps<M: MigratorTrait>(&self, steps: u32) -> Result<()>
Apply at most steps pending migrations.
Sourcepub async fn rollback<M: MigratorTrait>(&self, steps: u32) -> Result<()>
pub async fn rollback<M: MigratorTrait>(&self, steps: u32) -> Result<()>
Roll back the last steps applied migrations.
Sourcepub async fn migrate_fresh<M: MigratorTrait>(&self) -> Result<()>
pub async fn migrate_fresh<M: MigratorTrait>(&self) -> Result<()>
Drop everything and migrate from scratch. Never run this against data you want to keep.
Sourcepub async fn migration_status<M: MigratorTrait>(
&self,
) -> Result<Vec<MigrationState>>
pub async fn migration_status<M: MigratorTrait>( &self, ) -> Result<Vec<MigrationState>>
Every migration and whether it has been applied, in declaration order.
Sourcepub async fn has_pending_migrations<M: MigratorTrait>(&self) -> Result<bool>
pub async fn has_pending_migrations<M: MigratorTrait>(&self) -> Result<bool>
Whether anything is waiting to run — worth checking at boot.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Db
impl !UnwindSafe for Db
impl Freeze for Db
impl Send for Db
impl Sync for Db
impl Unpin for Db
impl UnsafeUnpin for Db
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more