pub trait ToqlApi {
type Row;
type Error: From<ToqlError>;
fn insert_one<'life0, 'life1, 'async_trait, T>(
&'life0 mut self,
entity: &'life1 mut T,
paths: Paths
) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait, Global>>
where
'life0: 'async_trait,
'life1: 'async_trait,
T: Insert + 'async_trait,
Self: 'async_trait;
fn insert_many<'life0, 'life1, 'async_trait, T, Q>(
&'life0 mut self,
entities: &'life1 mut [Q],
paths: Paths
) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait, Global>>
where
'life0: 'async_trait,
'life1: 'async_trait,
T: Insert + 'async_trait,
Q: BorrowMut<T> + Send + 'async_trait,
Self: 'async_trait;
fn update_one<'life0, 'life1, 'async_trait, T>(
&'life0 mut self,
entity: &'life1 mut T,
fields: Fields
) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait, Global>>
where
'life0: 'async_trait,
'life1: 'async_trait,
T: Update + Keyed + 'async_trait,
Self: 'async_trait;
fn update_many<'life0, 'life1, 'async_trait, T, Q>(
&'life0 mut self,
entities: &'life1 mut [Q],
fields: Fields
) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait, Global>>
where
'life0: 'async_trait,
'life1: 'async_trait,
T: Update + Keyed + 'async_trait,
Q: BorrowMut<T> + Send + Sync + 'async_trait,
Self: 'async_trait;
fn load_one<'life0, 'async_trait, T, B>(
&'life0 mut self,
query: B
) -> Pin<Box<dyn Future<Output = Result<T, Self::Error>> + Send + 'async_trait, Global>>
where
'life0: 'async_trait,
T: Load<Self::Row, Self::Error> + 'async_trait,
B: Borrow<Query<T>> + Send + Sync + 'async_trait,
Self: 'async_trait,
<T as Keyed>::Key: FromRow<Self::Row, Self::Error>;
fn load_many<'life0, 'async_trait, T, B>(
&'life0 mut self,
query: B
) -> Pin<Box<dyn Future<Output = Result<Vec<T, Global>, Self::Error>> + Send + 'async_trait, Global>>
where
'life0: 'async_trait,
T: Load<Self::Row, Self::Error> + 'async_trait,
B: Borrow<Query<T>> + Send + Sync + 'async_trait,
Self: 'async_trait,
<T as Keyed>::Key: FromRow<Self::Row, Self::Error>;
fn load_page<'life0, 'async_trait, T, B>(
&'life0 mut self,
query: B,
page: Page
) -> Pin<Box<dyn Future<Output = Result<(Vec<T, Global>, Option<PageCounts>), Self::Error>> + Send + 'async_trait, Global>>
where
'life0: 'async_trait,
T: Load<Self::Row, Self::Error> + 'async_trait,
B: Borrow<Query<T>> + Send + Sync + 'async_trait,
Self: 'async_trait,
<T as Keyed>::Key: FromRow<Self::Row, Self::Error>;
fn count<'life0, 'async_trait, T, B>(
&'life0 mut self,
query: B
) -> Pin<Box<dyn Future<Output = Result<u64, Self::Error>> + Send + 'async_trait, Global>>
where
'life0: 'async_trait,
T: Count + 'async_trait,
B: Borrow<Query<T>> + Send + Sync + 'async_trait,
Self: 'async_trait;
fn delete_one<'life0, 'async_trait, K>(
&'life0 mut self,
key: K
) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait, Global>>
where
'life0: 'async_trait,
K: Key + Into<Query<<K as Key>::Entity>> + Send + 'async_trait,
Self: 'async_trait,
<K as Key>::Entity: Send,
<K as Key>::Entity: Delete;
fn delete_many<'life0, 'async_trait, T, B>(
&'life0 mut self,
query: B
) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait, Global>>
where
'life0: 'async_trait,
T: Delete + 'async_trait,
B: Borrow<Query<T>> + Send + Sync + 'async_trait,
Self: 'async_trait;
}
Expand description
Every database provider implements this trait. This ensures that the user of Toql can easily switch databases.
The trait can also be used to write database independend code. This however requires more trait bounds and is usually less ergonomic than passing for a specific database type to the functions.
Example
The following code shows the function signature to load an entity MyUser
from a any database:
ⓘ
use toql_core::{error::ToqlError, toql_api::ToqlApi, load::Load, from_row::FromRow};
use toql_derive::Toql;
struct MyError;
#[derive(Toql)]
struct MyUser {
#[toql(key)]
id: u64,
name: String
}
async fn load_user<R, E, A>(toql: &mut A) -> std::result::Result<Vec<MyUser>, MyError>
where A: ToqlApi<Row=R, Error = E>,
E: From<ToqlError>,
MyUser: Load<R, E>,
<User as Keyed>::Key: FromRow<R, E>, // Needed until rust-lang/rfcs#2289 is resolved
MyError: From<E>
{
let users = toql.load_many().await?;
Ok(users)
}