pub struct DB;Expand description
Database facade - main entry point for database operations
Provides static methods for initializing and accessing the database connection. The connection is stored in the application container as a singleton.
§Example
use ferro_rs::{DB, DatabaseConfig, Config};
// Initialize (usually in bootstrap.rs)
Config::register(DatabaseConfig::from_env());
DB::init().await?;
// Use anywhere in your app
let conn = DB::connection()?;Implementations§
Source§impl DB
impl DB
Sourcepub async fn init() -> Result<(), FrameworkError>
pub async fn init() -> Result<(), FrameworkError>
Initialize the database connection
Reads configuration from DatabaseConfig (must be registered via Config system)
and establishes a connection pool. The connection is stored in the App container.
§Errors
Returns an error if:
DatabaseConfigis not registered- Connection to the database fails
§Example
// In bootstrap.rs
pub async fn register() {
DB::init().await.expect("Failed to connect to database");
}Sourcepub async fn init_with(config: DatabaseConfig) -> Result<(), FrameworkError>
pub async fn init_with(config: DatabaseConfig) -> Result<(), FrameworkError>
Sourcepub fn connection() -> Result<DbConnection, FrameworkError>
pub fn connection() -> Result<DbConnection, FrameworkError>
Get the database connection
Returns the connection from the App container. The connection is wrapped
in a DbConnection which provides convenient access to the underlying
SeaORM DatabaseConnection.
§Errors
Returns an error if DB::init() was not called.
§Example
let conn = DB::connection()?;
// Use with SeaORM queries
let users = User::find()
.all(conn.inner())
.await?;Sourcepub fn is_connected() -> bool
pub fn is_connected() -> bool
Sourcepub fn get() -> Result<DbConnection, FrameworkError>
pub fn get() -> Result<DbConnection, FrameworkError>
Get the database connection for use with SeaORM
This is a convenience alias for DB::connection(). The returned
DbConnection implements Deref<Target=DatabaseConnection>, so you
can use it directly with SeaORM methods.
§Example
use ferro_rs::database::DB;
use sea_orm::{Set, ActiveModelTrait};
let new_todo = todos::ActiveModel {
title: Set("My Todo".to_string()),
..Default::default()
};
// Use &* to dereference to &DatabaseConnection
let inserted = new_todo.insert(&*DB::get()?).await?;
// Or use .inner() method
let inserted = new_todo.insert(DB::get()?.inner()).await?;Auto Trait Implementations§
impl Freeze for DB
impl RefUnwindSafe for DB
impl Send for DB
impl Sync for DB
impl Unpin for DB
impl UnsafeUnpin for DB
impl UnwindSafe 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
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