Skip to main content

AppDatabaseExt

Trait AppDatabaseExt 

Source
pub trait AppDatabaseExt: Sized {
    // Required methods
    fn database<DB: Database>(
        self,
        url: &str,
    ) -> impl Future<Output = Result<Self, FletchError>> + Send;
    fn database_with<DB: Database>(
        self,
        url: &str,
        builder: PoolBuilder<DB>,
    ) -> impl Future<Output = Result<Self, FletchError>> + Send;
    fn health_check_db<DB: Database>(self) -> Self;
    fn migrate<DB>(
        self,
        path: &str,
    ) -> impl Future<Output = Result<Self, MigrateError>> + Send
       where DB: Database,
             <DB as Database>::Connection: Migrate;
}
Expand description

Extension trait that adds database connection and migration methods to App.

Required Methods§

Source

fn database<DB: Database>( self, url: &str, ) -> impl Future<Output = Result<Self, FletchError>> + Send

Connect to a database using the given URL and register the pool.

The fletch_orm::Pool<DB> is registered as a dependency and can be extracted in actions via Dep<fletch_orm::Pool<DB>>.

Source

fn database_with<DB: Database>( self, url: &str, builder: PoolBuilder<DB>, ) -> impl Future<Output = Result<Self, FletchError>> + Send

Connect to a database using the given URL and a pre-configured fletch_orm::PoolBuilder, then register the pool.

This allows customizing pool settings such as max connections, idle timeout, and acquire timeout.

Source

fn health_check_db<DB: Database>(self) -> Self

Register a GET /health/db route that checks database connectivity.

Returns 200 with pool stats when healthy, or 503 when unreachable.

Source

fn migrate<DB>( self, path: &str, ) -> impl Future<Output = Result<Self, MigrateError>> + Send
where DB: Database, <DB as Database>::Connection: Migrate,

Run SQLx migrations from the given directory path.

This registers a startup hook that runs all pending migrations when the app starts (via serve() or build_with_hooks()).

The path should point to a directory containing .sql migration files following SQLx naming conventions (e.g. 20230101000000_create_users.sql).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl AppDatabaseExt for App

Source§

async fn database<DB: Database>(self, url: &str) -> Result<Self, FletchError>

Source§

async fn database_with<DB: Database>( self, url: &str, builder: PoolBuilder<DB>, ) -> Result<Self, FletchError>

Source§

fn health_check_db<DB: Database>(self) -> Self

Source§

async fn migrate<DB>(self, path: &str) -> Result<Self, MigrateError>
where DB: Database, <DB as Database>::Connection: Migrate,

Implementors§