Skip to main content

Db

Struct Db 

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

The certified arcature-db engine, re-exported so downstream code targets the Arcature-pinned types through this crate. Db is the explicit ownership handle every API in this crate borrows. The Arcature database handle: one PostgreSQL connection pool with two first-class data paths.

Db holds exactly one sqlx::PgPool and derives the SeaORM DatabaseConnection over the same pool via SqlxPostgresConnector::from_sqlx_postgres_pool. There is never a second pool (Phase 4 spec §4).

Db is Clone + Send + Sync + 'static so it works as normal Axum state (Phase 4 spec §8):

#[derive(Clone)]
struct AppState { db: Db }

There is no DbExtractor, service container, database registry, global singleton, TypeId/Any map, thread-local, or task-local (Phase 4 spec §8, §20). Use Db::sqlx for raw SQL access and Db::orm for SeaORM access — both over the same pool.

Implementations§

Source§

impl Db

Source

pub async fn close(self)

Close the one connection pool.

This closes the underlying PgPool. All cloned Db handles share the same pool (it is Arc-backed internally), so closing from one handle closes the pool for all handles (Phase 4 spec §29). The SeaORM DatabaseConnection is also closed because it operates over the same pool.

After close, Db::ping returns DbHealthError::Closed and queries fail with a pool-closed error.

Source§

impl Db

Source

pub async fn connect(config: DbConfig) -> Result<Db, DbConnectError>

Connect to PostgreSQL using resolved configuration.

Validates the configuration (min > max, zero durations) before any expensive async work runs (Phase 4 spec §19), then builds one PgPool and derives the SeaORM DatabaseConnection over it.

§Errors

Returns DbConnectError::Config if the configuration is internally inconsistent, or DbConnectError::Pool if the pool cannot be established (network, auth, server unavailable).

Source

pub fn from_pool(pool: Pool<Postgres>) -> Db

Construct a Db from an existing PgPool.

This is the database escape hatch (Phase 4 spec §9): an expert can build and configure the pool themselves, then hand it to Arcature. The SeaORM connection is derived over the same pool — no second pool is created.

Source§

impl Db

Source

pub fn orm(&self) -> &DatabaseConnection

Get the underlying SeaORM DatabaseConnection for ORM access.

This is the first-class SeaORM path (Phase 4 spec §5). The connection is derived over the same PgPool — there is no second pool.

SeaORM entities are application-defined (annotated with #[derive(sea_orm::DeriveEntityModel)]), so this example is ignored here. See examples/basic_query.rs for a runnable program using the SQLx path; the SeaORM path follows the same one-pool structure:

let users = user::Entity::find()
    .filter(user::Column::Active.eq(true))
    .all(db.orm())
    .await?;
Source§

impl Db

Source

pub async fn ping(&self) -> Result<(), DbHealthError>

Check database liveness by executing SELECT 1.

Uses the configured pool acquire timeout; it does not hide a 30-second timeout (Phase 4 spec §28). A closed pool returns DbHealthError::Closed. The ping error preserves the upstream sqlx::Error for source chaining. Credentials are never included in the failure (Phase 4 spec §28).

Source§

impl Db

Source

pub fn sqlx(&self) -> &Pool<Postgres>

Get the underlying sqlx::PgPool for raw SQLx access.

This is the first-class SQLx escape hatch (Phase 4 spec §5). Use it with sqlx::query! / sqlx::query_as! for compile-time checked SQL, or with the runtime sqlx::query_as for dynamically-built queries:

use arcature_db::sqlx;

#[derive(sqlx::FromRow)]
struct UserRow { id: i32, email: String }

let row = sqlx::query_as::<_, UserRow>("SELECT id, email FROM users WHERE id = $1")
    .bind(42)
    .fetch_one(db.sqlx())
    .await?;

Trait Implementations§

Source§

impl Clone for Db

Source§

fn clone(&self) -> Db

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

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> 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<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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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<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