saddle-framework 0.1.1

The single business-facing facade for Saddle applications
Documentation
//! Business-facing managed database capability.
//!
//! Pool construction and shutdown are intentionally not exposed here. The
//! facade creates exactly one underlying pool and injects this capability into
//! Service implementations.

pub use saddle_db::{
    DbRow, DbValue, MAX_FIELD_BYTES, MAX_PARAMETER_BYTES, MAX_PARAMETERS,
    MAX_PARAMETERS_TOTAL_BYTES, MAX_QUERY_ROWS, MAX_RESULT_BYTES, MAX_SQL_BYTES, Statement,
    Transaction, TransactionFuture, WriteResult,
};

use crate::{CallContext, Result};

/// Cloneable handle to Saddle's single process-wide database pool.
#[derive(Clone)]
pub struct Database(pub(crate) saddle_db::Database);

impl Database {
    pub async fn query_all(
        &self,
        parent: &CallContext,
        statement: Statement,
    ) -> Result<Vec<DbRow>> {
        self.0.query_all(parent, statement).await
    }

    pub async fn query_optional(
        &self,
        parent: &CallContext,
        statement: Statement,
    ) -> Result<Option<DbRow>> {
        self.0.query_optional(parent, statement).await
    }

    pub async fn write(&self, parent: &CallContext, statement: Statement) -> Result<WriteResult> {
        self.0.write(parent, statement).await
    }

    pub async fn transaction<T, F>(
        &self,
        parent: &CallContext,
        operation: impl Into<crate::OperationId>,
        work: F,
    ) -> Result<T>
    where
        T: Send,
        F: for<'a> FnOnce(&'a mut Transaction) -> TransactionFuture<'a, T> + Send,
    {
        self.0.transaction(parent, operation, work).await
    }
}