pub struct Database { /* private fields */ }Expand description
The main entry point for database connection and management.
It handles connection pooling, driver detection, and schema operations.
Implementations§
Source§impl Database
impl Database
Sourcepub async fn connect(url: &str) -> Result<Self, Error>
pub async fn connect(url: &str) -> Result<Self, Error>
Connects to the database using a connection string (Database URL).
It automatically identifies the driver (Postgres, MySQL, SQLite) based on the URL prefix.
§Arguments
url- The database connection string (e.g.,postgres://user:pass@localhost/db).
§Example
ⓘ
let db = Database::connect("postgres://user:password@localhost/mydb").await?;Sourcepub fn migrator(&self) -> Migrator<'_>
pub fn migrator(&self) -> Migrator<'_>
Creates a Migrator instance to manage schema migrations.
Sourcepub async fn create_table<T: Model>(&self) -> Result<&Self, Error>
pub async fn create_table<T: Model>(&self) -> Result<&Self, Error>
Creates the table for model T if it does not exist.
This method generates SQL for columns, primary keys, indexes, and constraints.
Sourcepub async fn assign_foreign_keys<T: Model>(&self) -> Result<&Self, Error>
pub async fn assign_foreign_keys<T: Model>(&self) -> Result<&Self, Error>
Checks for and assigns Foreign Keys for model T.
This function verifies if the constraint already exists before attempting to create it to avoid duplication errors.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Database
impl !RefUnwindSafe for Database
impl Send for Database
impl Sync for Database
impl Unpin for Database
impl !UnwindSafe for Database
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
Mutably borrows from an owned value. Read more
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>
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 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>
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