Trait CrudOps

Source
pub trait CrudOps {
    // Required methods
    fn insert<'life0, 'async_trait, T>(
        &'life0 self,
        entity: T,
    ) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + Send + 'async_trait>>
       where T: SqlQuery + SqlParams + Send + Sync + 'static + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait;
    fn update<'life0, 'async_trait, T>(
        &'life0 self,
        entity: T,
    ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
       where T: SqlQuery + UpdateParams + Send + Sync + 'static + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait;
    fn delete<'life0, 'async_trait, T>(
        &'life0 self,
        entity: T,
    ) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + Send + 'async_trait>>
       where T: SqlQuery + SqlParams + Send + Sync + 'static + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait;
    fn fetch<'life0, 'async_trait, T>(
        &'life0 self,
        params: T,
    ) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + 'async_trait>>
       where T: SqlQuery + FromRow + SqlParams + Send + Sync + 'static + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait;
    fn fetch_all<'life0, 'async_trait, T>(
        &'life0 self,
        params: T,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<T>, Error>> + Send + 'async_trait>>
       where T: SqlQuery + FromRow + SqlParams + Send + Sync + 'static + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait;
    fn select<'life0, 'async_trait, T, F, R>(
        &'life0 self,
        entity: T,
        to_model: F,
    ) -> Pin<Box<dyn Future<Output = Result<R, Error>> + Send + 'async_trait>>
       where T: SqlQuery + SqlParams + Send + Sync + 'static + 'async_trait,
             F: Fn(&Row) -> Result<R, Error> + Send + Sync + 'static + 'async_trait,
             R: Send + 'static + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait;
    fn select_all<'life0, 'async_trait, T, F, R>(
        &'life0 self,
        entity: T,
        to_model: F,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<R>, Error>> + Send + 'async_trait>>
       where T: SqlQuery + SqlParams + Send + Sync + 'static + 'async_trait,
             F: Fn(&Row) -> R + Send + Sync + 'static + 'async_trait,
             R: Send + 'static + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn get<'life0, 'async_trait, T>(
        &'life0 self,
        params: T,
    ) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + 'async_trait>>
       where T: SqlQuery + FromRow + SqlParams + Send + Sync + 'static + 'async_trait,
             Self: Sync + 'async_trait,
             'life0: 'async_trait { ... }
    fn get_all<'life0, 'async_trait, T>(
        &'life0 self,
        params: T,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<T>, Error>> + Send + 'async_trait>>
       where T: SqlQuery + FromRow + SqlParams + Send + Sync + 'static + 'async_trait,
             Self: Sync + 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

A trait for extending PostgreSQL client with CRUD operations.

This trait provides extension methods for tokio_postgres::Client to perform common database CRUD operations in a more ergonomic way.

Required Methods§

Source

fn insert<'life0, 'async_trait, T>( &'life0 self, entity: T, ) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + Send + 'async_trait>>
where T: SqlQuery + SqlParams + Send + Sync + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Inserts a new record into the database.

§Arguments
  • entity - Data object to be inserted (must implement SqlQuery and SqlParams traits)
§Return Value
  • Result<u64, Error> - On success, returns the number of inserted records; on failure, returns Error
§Example
#[derive(Insertable, SqlParams)]
#[table("users")]
struct InsertUser {
    name: String,
    email: String,
}

let user = InsertUser {
    name: "John".to_string(),
    email: "john@example.com".to_string(),
};

let id = client.insert(user).await?;
Source

fn update<'life0, 'async_trait, T>( &'life0 self, entity: T, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where T: SqlQuery + UpdateParams + Send + Sync + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Updates an existing record in the database.

§Arguments
  • entity - Data object containing the update information (must implement SqlQuery and UpdateParams traits)
§Return Value
  • Result<bool, Error> - On success, returns true if at least one record was updated; on failure, returns Error
§Example
#[derive(Updateable, UpdateParams)]
#[table("users")]
#[update("name, email")]
#[where_clause("id = $")]
struct UpdateUser {
    id: i64,
    name: String,
    email: String,
}

let user = UpdateUser {
    id: 1,
    name: "John Smith".to_string(),
    email: "john.smith@example.com".to_string(),
};

let updated = client.update(user).await?;
Source

fn delete<'life0, 'async_trait, T>( &'life0 self, entity: T, ) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + Send + 'async_trait>>
where T: SqlQuery + SqlParams + Send + Sync + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Deletes a record from the database.

§Arguments
  • entity - Data object containing delete conditions (must implement SqlQuery and SqlParams traits)
§Return Value
  • Result<u64, Error> - On success, returns the number of deleted records; on failure, returns Error
§Example
#[derive(Deletable, SqlParams)]
#[table("users")]
#[where_clause("id = $")]
struct DeleteUser {
    id: i64,
}

let user = DeleteUser { id: 1 };

let deleted = client.delete(user).await?;
Source

fn fetch<'life0, 'async_trait, T>( &'life0 self, params: T, ) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + 'async_trait>>
where T: SqlQuery + FromRow + SqlParams + Send + Sync + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Retrieves a single record from the database and converts it to a struct.

§Arguments
  • params - Data object containing query parameters (must implement SqlQuery, FromRow, and SqlParams traits)
§Return Value
  • Result<T, Error> - On success, returns the retrieved record as a struct; on failure, returns Error
§Example
#[derive(Queryable, FromRow, SqlParams, Debug)]
#[table("users")]
#[where_clause("id = $")]
struct GetUser {
    id: i64,
    name: String,
    email: String,
}

let query = GetUser {
    id: 1,
    name: Default::default(),
    email: Default::default(),
};

let user = client.fetch(query).await?;
Source

fn fetch_all<'life0, 'async_trait, T>( &'life0 self, params: T, ) -> Pin<Box<dyn Future<Output = Result<Vec<T>, Error>> + Send + 'async_trait>>
where T: SqlQuery + FromRow + SqlParams + Send + Sync + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Retrieves multiple records from the database and converts them to a vec of structs.

§Arguments
  • params - Data object containing query parameters (must implement SqlQuery, FromRow, and SqlParams traits)
§Return Value
  • Result<Vec<T>, Error> - On success, returns a vector of retrieved records; on failure, returns Error
§Example
#[derive(Queryable, FromRow, SqlParams, Debug)]
#[table("users")]
#[where_clause("state = $")]
struct GetActiveUsers {
    id: i64,
    name: String,
    email: String,
    state: i16,
}

let query = GetActiveUsers {
    id: 0,
    name: Default::default(),
    email: Default::default(),
    state: 1, // active users
};

let users = client.fetch_all(query).await?;
Source

fn select<'life0, 'async_trait, T, F, R>( &'life0 self, entity: T, to_model: F, ) -> Pin<Box<dyn Future<Output = Result<R, Error>> + Send + 'async_trait>>
where T: SqlQuery + SqlParams + Send + Sync + 'static + 'async_trait, F: Fn(&Row) -> Result<R, Error> + Send + Sync + 'static + 'async_trait, R: Send + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Executes a custom SELECT query and converts the results using the provided function.

§Arguments
  • entity - Data object containing query parameters (must implement SqlQuery and SqlParams traits)
  • to_model - Function to convert a row to the desired type
§Return Value
  • Result<R, Error> - On success, returns the converted record; on failure, returns Error
§Example
#[derive(Queryable, SqlParams)]
#[table("users")]
#[select("SELECT u.*, p.role FROM users u JOIN profiles p ON u.id = p.user_id")]
#[where_clause("u.state = $")]
struct UserQuery {
    state: i16,
}

struct UserWithRole {
    id: i64,
    name: String,
    role: String,
}

fn convert_row(row: &Row) -> Result<UserWithRole, tokio_postgres::Error> {
    Ok(UserWithRole {
        id: row.try_get("id")?,
        name: row.try_get("name")?,
        role: row.try_get("role")?,
    })
}

let query = UserQuery { state: 1 };

let user = client.select(query, convert_row).await?;
Source

fn select_all<'life0, 'async_trait, T, F, R>( &'life0 self, entity: T, to_model: F, ) -> Pin<Box<dyn Future<Output = Result<Vec<R>, Error>> + Send + 'async_trait>>
where T: SqlQuery + SqlParams + Send + Sync + 'static + 'async_trait, F: Fn(&Row) -> R + Send + Sync + 'static + 'async_trait, R: Send + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Executes a custom SELECT query and converts all the results using the provided function.

§Arguments
  • entity - Data object containing query parameters (must implement SqlQuery and SqlParams traits)
  • to_model - Function to convert a row to the desired type
§Return Value
  • Result<Vec<R>, Error> - On success, returns a vector of converted records; on failure, returns Error
§Example
#[derive(Queryable, SqlParams)]
#[table("users")]
#[select("SELECT u.*, p.role FROM users u JOIN profiles p ON u.id = p.user_id")]
#[where_clause("u.state = $")]
struct UserQuery {
    state: i16,
}

struct UserWithRole {
    id: i64,
    name: String,
    role: String,
}

fn convert_row(row: &Row) -> UserWithRole {
    UserWithRole {
        id: row.get("id"),
        name: row.get("name"),
        role: row.get("role"),
    }
}

let query = UserQuery { state: 1 };

let users = client.select_all(query, convert_row).await?;

Provided Methods§

Source

fn get<'life0, 'async_trait, T>( &'life0 self, params: T, ) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + 'async_trait>>
where T: SqlQuery + FromRow + SqlParams + Send + Sync + 'static + 'async_trait, Self: Sync + 'async_trait, 'life0: 'async_trait,

👎Deprecated since 0.2.0: Renamed to fetch. Please use fetch function instead.
Source

fn get_all<'life0, 'async_trait, T>( &'life0 self, params: T, ) -> Pin<Box<dyn Future<Output = Result<Vec<T>, Error>> + Send + 'async_trait>>
where T: SqlQuery + FromRow + SqlParams + Send + Sync + 'static + 'async_trait, Self: Sync + 'async_trait, 'life0: 'async_trait,

👎Deprecated since 0.2.0: Renamed to fetch_all. Please use fetch_all function instead.

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.

Implementations on Foreign Types§

Source§

impl<'a> CrudOps for Transaction<'a>

Implementation of the CrudOps trait for Transactions

This implementation allows using the CrudOps trait methods directly on Transaction<'a> objects, similar to how they are used on Client objects. This provides a consistent API for both regular client operations and transaction operations.

§Examples

let transaction = client.transaction().await?;

// Using CrudOps trait method directly on transaction
let user = InsertUser {
    name: "John".to_string(),
    email: "john@example.com".to_string(),
};

let rows_affected = transaction.insert(user).await?;
println!("Rows affected: {}", rows_affected);

transaction.commit().await?;
Source§

fn insert<'life0, 'async_trait, T>( &'life0 self, entity: T, ) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + Send + 'async_trait>>
where T: SqlQuery + SqlParams + Send + Sync + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Source§

fn update<'life0, 'async_trait, T>( &'life0 self, entity: T, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where T: SqlQuery + UpdateParams + Send + Sync + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Source§

fn delete<'life0, 'async_trait, T>( &'life0 self, entity: T, ) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + Send + 'async_trait>>
where T: SqlQuery + SqlParams + Send + Sync + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Source§

fn fetch<'life0, 'async_trait, T>( &'life0 self, params: T, ) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + 'async_trait>>
where T: SqlQuery + FromRow + SqlParams + Send + Sync + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Source§

fn fetch_all<'life0, 'async_trait, T>( &'life0 self, params: T, ) -> Pin<Box<dyn Future<Output = Result<Vec<T>, Error>> + Send + 'async_trait>>
where T: SqlQuery + FromRow + SqlParams + Send + Sync + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get<'life0, 'async_trait, T>( &'life0 self, params: T, ) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + 'async_trait>>
where T: SqlQuery + FromRow + SqlParams + Send + Sync + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

👎Deprecated since 0.2.0: Renamed to fetch. Please use fetch function instead.
Source§

fn get_all<'life0, 'async_trait, T>( &'life0 self, params: T, ) -> Pin<Box<dyn Future<Output = Result<Vec<T>, Error>> + Send + 'async_trait>>
where T: SqlQuery + FromRow + SqlParams + Send + Sync + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

👎Deprecated since 0.2.0: Renamed to fetch_all. Please use fetch_all function instead.
Source§

fn select<'life0, 'async_trait, T, F, R>( &'life0 self, entity: T, to_model: F, ) -> Pin<Box<dyn Future<Output = Result<R, Error>> + Send + 'async_trait>>
where T: SqlQuery + SqlParams + Send + Sync + 'static + 'async_trait, F: Fn(&Row) -> Result<R, Error> + Send + Sync + 'static + 'async_trait, R: Send + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Source§

fn select_all<'life0, 'async_trait, T, F, R>( &'life0 self, entity: T, to_model: F, ) -> Pin<Box<dyn Future<Output = Result<Vec<R>, Error>> + Send + 'async_trait>>
where T: SqlQuery + SqlParams + Send + Sync + 'static + 'async_trait, F: Fn(&Row) -> R + Send + Sync + 'static + 'async_trait, R: Send + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Implementors§