Skip to main content

Database

Struct Database 

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

Async database using the backend selected by the crate feature.

Implementations§

Source§

impl Database

Source

pub fn connect_lazy(url: impl Into<String>) -> Result<Database, OrmError>

Creates a pool without opening a connection. Configuration is validated immediately; connectivity is checked on first use. Useful for offline schema/API metadata discovery, which must not execute database queries.

Source

pub fn connect_lazy_with_pool_size( url: impl Into<String>, size: usize, ) -> Result<Database, OrmError>

Creates an unconnected pool with an explicit positive connection limit. Like Self::connect_lazy, this checks configuration, not connectivity.

Source

pub async fn connect(url: impl Into<String>) -> Result<Database, OrmError>

Source

pub async fn connect_with_pool_size( url: impl Into<String>, size: usize, ) -> Result<Database, OrmError>

Source

pub async fn connect_in_memory() -> Result<Database, OrmError>

Source

pub async fn execute(&self, ast: QueryAst) -> Result<ExecuteResult, OrmError>

Source

pub fn with_model_runtime(self, runtime: Arc<dyn ModelRuntime>) -> Database

Bind model-aware behavior to this database handle and its transactions.

Source

pub fn model_role<M>(&self) -> Option<ModelRole>
where M: Model,

Source

pub async fn execute_sql( &self, sql: impl Into<String>, params: Vec<DatabaseValue>, ) -> Result<ExecuteResult, OrmError>

Executes explicitly supplied SQL with bound values. SQL syntax is backend-specific.

Source

pub async fn query_sql( &self, sql: impl Into<String>, params: Vec<DatabaseValue>, ) -> Result<Vec<Row>, OrmError>

Source

pub async fn create_table<M>(&self) -> Result<ExecuteResult, OrmError>
where M: Model,

Source

pub async fn insert<M>(&self, model: &M) -> Result<ExecuteResult, OrmError>
where M: Model,

Inserts supplied UUID keys, omitting generated integer keys and managed timestamps.

Source

pub async fn save<M>(&self, model: &M) -> Result<Option<M>, OrmError>
where M: Model + Send + 'static,

Updates an existing row by its primary key using all ordinary model fields.

Preserves the stored primary key and auto_now_add fields, refreshes auto_now, and returns the row produced by RETURNING. The supplied object is not modified. Returns None if no row matches; never inserts. Models without writable fields or managed updates return EmptyUpdate. Configured model runtimes capture this write just like update. All ordinary fields are written, including unchanged values; use update().set(...) to update only selected fields of a stale object.

Source

pub async fn fetch_all<M>( &self, query: SelectQuery<M>, ) -> Result<Vec<M>, OrmError>
where M: Model + Send + 'static,

Source

pub async fn fetch_one<M>( &self, query: SelectQuery<M>, ) -> Result<Option<M>, OrmError>
where M: Model + Send + 'static,

Source

pub async fn get<M>( &self, id: <M as Model>::PrimaryKey, ) -> Result<Option<M>, OrmError>
where M: Model + Send + 'static,

Source

pub async fn all<M>(&self) -> Result<Vec<M>, OrmError>
where M: Model + Send + 'static,

Source

pub async fn count<M>(&self) -> Result<usize, OrmError>
where M: Model + Send + 'static,

Source

pub async fn count_query<M>( &self, query: SelectQuery<M>, ) -> Result<usize, OrmError>
where M: Model + Send + 'static,

Source

pub fn create<M>(&self) -> CreateBuilder<'_, M>
where M: Model,

Source

pub fn update<M>(&self, id: <M as Model>::PrimaryKey) -> UpdateBuilder<'_, M>
where M: Model,

Source

pub async fn delete<M>( &self, id: <M as Model>::PrimaryKey, ) -> Result<bool, OrmError>
where M: Model + Send + 'static,

Source

pub fn query<M>(&self) -> BoundQuery<'_, DatabaseQuery<M>>
where M: Model,

Builds a query bound to this database; execute it with .all(), .first() or .count() without passing the database again.

Source

pub async fn fetch_by<M, T, V>( &self, field: ModelField<M, T>, value: V, ) -> Result<Vec<M>, OrmError>
where M: Model + Send + 'static, V: QueryValue<T>,

Source

pub async fn fetch_by_many<M, T, I, V>( &self, field: ModelField<M, T>, values: I, ) -> Result<Vec<M>, OrmError>
where M: Model + Send + 'static, I: IntoIterator<Item = V>, V: QueryValue<T>,

Source

pub async fn transaction<T, F>(&self, action: F) -> Result<T, OrmError>
where T: Send + 'static, F: for<'a> FnOnce(&'a Transaction) -> Pin<Box<dyn Future<Output = Result<T, OrmError>> + Send + 'a>> + Send,

Runs one serializable async ORM transaction. Unfinished transactions are rolled back before a cancelled transaction’s connection is recycled. Use |tx| Box::pin(async move { ... }) and await transaction methods.

Source

pub async fn transaction_with_isolation<T, F>( &self, isolation: IsolationLevel, action: F, ) -> Result<T, OrmError>
where T: Send + 'static, F: for<'a> FnOnce(&'a Transaction) -> Pin<Box<dyn Future<Output = Result<T, OrmError>> + Send + 'a>> + Send,

SQLite uses its serializable snapshot for every requested isolation level.

Source

pub async fn transaction_result<T, E, F>(&self, action: F) -> Result<T, E>
where T: Send + 'static, E: From<OrmError>, F: for<'a> FnOnce(&'a Transaction) -> Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'a>> + Send,

Transaction with an application error type; every error rolls back.

Source

pub async fn raw_transaction<T, F>(&self, action: F) -> Result<T, OrmError>
where T: Send + 'static, F: FnOnce(&Connection) -> Result<T, Error> + Send + 'static,

SQLite-only escape hatch for direct driver operations.

Source

pub async fn migrate(&self, graph: MigrationGraph) -> Result<usize, OrmError>

Source

pub async fn migrate_to( &self, graph: MigrationGraph, target: Option<MigrationId>, ) -> Result<usize, OrmError>

Source

pub async fn migration_history( &self, ) -> Result<HashMap<(String, String), String>, OrmError>

Trait Implementations§

Source§

impl Clone for Database

Source§

fn clone(&self) -> Database

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§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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