pub trait Crud<'e, E>where
Self: 'e + Sized + Send + Unpin + for<'r> FromRow<'r, <E::Database as Database>::Row> + Schema,
<Self as Schema>::Id: Encode<'e, <E as Executor<'e>>::Database> + Type<<E as Executor<'e>>::Database>,
E: Executor<'e> + 'e,
<E::Database as HasArguments<'e>>::Arguments: IntoArguments<'e, <E as Executor<'e>>::Database>,{
// Required methods
fn insert_args(self) -> <E::Database as HasArguments<'e>>::Arguments;
fn update_args(self) -> <E::Database as HasArguments<'e>>::Arguments;
// Provided methods
fn create(self, pool: E) -> CrudFut<'e, Self> { ... }
fn all(pool: E) -> TryCollectFut<'e, Self> { ... }
fn by_id(pool: E, id: <Self as Schema>::Id) -> CrudFut<'e, Option<Self>> { ... }
fn update(self, pool: E) -> CrudFut<'e, Self> { ... }
fn delete(self, pool: E) -> CrudFut<'e, ()> { ... }
}
Expand description
Required Methods§
Sourcefn insert_args(self) -> <E::Database as HasArguments<'e>>::Arguments
fn insert_args(self) -> <E::Database as HasArguments<'e>>::Arguments
Returns an owned instance of sqlx::Arguments. self is consumed.
Values in the fields are moved in to the Arguments
instance.
Sourcefn update_args(self) -> <E::Database as HasArguments<'e>>::Arguments
fn update_args(self) -> <E::Database as HasArguments<'e>>::Arguments
Returns an owned instance of sqlx::Arguments. self is consumed.
Values in the fields are moved in to the Arguments
instance.
Provided Methods§
Sourcefn create(self, pool: E) -> CrudFut<'e, Self>
fn create(self, pool: E) -> CrudFut<'e, Self>
Returns a future that resolves to an insert or sqlx::Error
of the
current instance.
§Example
use sqlx_crud::{Crud, Schema};
let user = User { user_id: 1, name: "test".to_string() };
let user = user.create(&pool).await?;
assert_eq!("test", user.name);
Sourcefn all(pool: E) -> TryCollectFut<'e, Self>
fn all(pool: E) -> TryCollectFut<'e, Self>
Queries all records from the table and returns a future that returns
to a try_collect stream, which resolves to a Vec<Self>
or a
sqlx::Error
on error.
§Example
use sqlx_crud::Crud;
let all_users: Vec<User> = User::all(&pool).await?;
Sourcefn by_id(pool: E, id: <Self as Schema>::Id) -> CrudFut<'e, Option<Self>>
fn by_id(pool: E, id: <Self as Schema>::Id) -> CrudFut<'e, Option<Self>>
Looks up a row by ID and returns a future that resolves an
Option<Self>
. Returns None
if and a record with the corresponding ID
cannot be found and Some
if it exists.
§Example
use sqlx_crud::Crud;
let user: Option<User> = User::by_id(&pool, 1).await?;
assert!(user.is_some());
Sourcefn update(self, pool: E) -> CrudFut<'e, Self>
fn update(self, pool: E) -> CrudFut<'e, Self>
Updates the database with the current instance state and returns a
future that resolves to the new Self
returned from the database.
§Example
use sqlx_crud::Crud;
if let Some(mut user) = User::by_id(&pool, 1).await? {
assert_eq!("test", user.name);
// Update the record
user.name = "Harry".to_string();
let user = user.update(&pool).await?;
// Confirm the name changed
assert_eq!("Harry", user.name);
}
Sourcefn delete(self, pool: E) -> CrudFut<'e, ()>
fn delete(self, pool: E) -> CrudFut<'e, ()>
Deletes a record from the database by ID and returns a future that
resolves to ()
on success or sqlx::Error
on failure.
§Example
use sqlx_crud::Crud;
if let Some(user) = User::by_id(&pool, 1).await? {
user.delete(&pool).await?;
}
assert!(User::by_id(&pool, 1).await?.is_none());
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.