Trait SelectRepository

Source
pub trait SelectRepository<M>: Repository<M>
where M: Model + for<'r> FromRow<'r, <Database as DatabaseTrait>::Row> + Send + Unpin,
{ // Required methods fn get_all_query(&self) -> QueryAs<'_, M>; fn get_by_id_query(&self, id: impl Into<M::Id>) -> QueryAs<'_, M>; // Provided methods async fn get_all_with_executor<E>(&self, tx: E) -> Result<Vec<M>> where E: for<'c> Executor<'c, Database = Database> { ... } async fn get_all(&self) -> Result<Vec<M>> { ... } async fn get_by_id_with_executor<E>( &self, tx: E, id: impl Into<M::Id>, ) -> Result<Option<M>> where E: for<'c> Executor<'c, Database = Database> { ... } async fn get_by_id(&self, id: impl Into<M::Id>) -> Result<Option<M>> { ... } }
Expand description

Trait for repositories that can retrieve records from the database.

The SelectRepository trait extends the base Repository trait with methods for querying and retrieving records. It defines a standard interface for fetching both individual records by ID and collections of records.

§Type Parameters

  • M - The model type that this repository retrieves. Must implement the Model trait and FromRow for the database’s row type.

§Required Methods

Implementing repositories must define:

§Provided Methods

These methods are automatically provided based on the required methods:

§Examples

Basic implementation:

use sqlx_utils::prelude::QueryAs;
use sqlx_utils::traits::{Model, Repository, SelectRepository};

impl SelectRepository<User> for UserRepository {
    fn get_all_query(&self) -> QueryAs<User> {
        sqlx::query_as("SELECT * FROM users")
    }

    fn get_by_id_query(&self, id: impl Into<i32>) -> QueryAs<User> {
        let id = id.into();
        sqlx::query_as("SELECT * FROM users WHERE id = $1")
            .bind(id)
    }
}

// Usage
// Get a single user
let user = repo.get_by_id(1).await?;

// Get all users
let all_users = repo.get_all().await?;

§Implementation Notes

  1. When implementing this trait, you only need to define:
  2. The execution methods are provided automatically based on these query methods
  3. Consider implementing pagination for get_all if the table may contain a large number of records
  4. Use parameter binding to prevent SQL injection
  5. Consider caching strategies for frequently accessed data
  6. The trait supports using custom executors (like transactions) via the _with_executor methods

Required Methods§

Source

fn get_all_query(&self) -> QueryAs<'_, M>

Creates a query to retrieve all records of this model type from the database.

This is a required method that implementors must define. It creates a query that will select all records of the model type, without executing it.

§Returns
  • QueryAs<M> - A prepared query that maps rows to the model type M
§Warning

Be cautious with this method on large tables as it could consume significant memory and impact database performance. Consider implementing pagination or adding WHERE clauses to limit the result set.

§Implementation Example
fn get_all_query(&self) -> QueryAs<User> {
    sqlx::query_as("SELECT * FROM users")
}
fn get_all_query(&self) -> QueryAs<User> {
    sqlx::query_as!(User, "SELECT * FROM users")
}
Source

fn get_by_id_query(&self, id: impl Into<M::Id>) -> QueryAs<'_, M>

Creates a query to retrieve a single model instance by its ID.

This is a required method that implementors must define. It creates a query that will select a record with the specified ID, without executing it.

§Parameters
  • id - Any value that can be converted into the model’s ID type
§Returns
  • QueryAs<M> - A prepared query that maps rows to the model type M
§Implementation Example
fn get_by_id_query(&self, id: impl Into<User::Id>) -> QueryAs<User> {
    let id = id.into();
    sqlx::query_as("SELECT * FROM users WHERE id = $1")
        .bind(id)
}
fn get_by_id_query(&self, id: impl Into<User::Id>) -> QueryAs<User> {
    let id = id.into();
    sqlx::query_as!(User, "SELECT * FROM users WHERE id = $1", id)
}

Provided Methods§

Source

async fn get_all_with_executor<E>(&self, tx: E) -> Result<Vec<M>>
where E: for<'c> Executor<'c, Database = Database>,

Executes the get_all query with a custom executor.

This method is automatically provided based on your implementation of get_all_query. It allows you to execute the query using a custom executor, such as a transactions.

§Type Parameters
  • E - The executor type, such as a transactions or connection pool
§Parameters
  • tx - The executor to use for the query
§Returns
§Example
async fn get_users_in_transaction<'a>(&self, tx: &mut Transaction<'a, Database>) -> Result<Vec<User>> {
    self.get_all_with_executor(&mut *tx).await
}
§Warning

Be cautious with this method on large tables as it could consume significant memory and impact database performance. Consider implementing pagination instead.

Source

async fn get_all(&self) -> Result<Vec<M>>

Retrieves all records of this model type from the database.

This method is automatically provided and simply calls get_all_with_executor with the repository’s connection pool. It executes the query from get_all_query.

§Returns
§Warning

Be cautious with this method on large tables as it could consume significant memory and impact database performance. Consider implementing pagination instead.

Source

async fn get_by_id_with_executor<E>( &self, tx: E, id: impl Into<M::Id>, ) -> Result<Option<M>>
where E: for<'c> Executor<'c, Database = Database>,

Executes the get_by_id query with a custom executor.

This method is automatically provided based on your implementation of get_by_id_query. It allows you to execute the query using a custom executor, such as a transactions.

§Type Parameters
  • E - The executor type, such as a transactions or connection pool
§Parameters
  • tx - The executor to use for the query
  • id - Any value that can be converted into the model’s ID type
§Returns
  • crate::Result<Option<M>> - A Result containing either:
    • Some(model) if a record was found
    • None if no record exists with the given ID
§Example
async fn get_user_in_transaction<'a>(&self, tx: &mut Transaction<'a, Database>, id: i32) -> Result<Option<User>> {
    self.get_by_id_with_executor(&mut *tx, id).await
}
Source

async fn get_by_id(&self, id: impl Into<M::Id>) -> Result<Option<M>>

Retrieves a single model instance by its ID.

This method is automatically provided and simply calls get_by_id_with_executor with the repository’s connection pool. It executes the query from get_by_id_query.

§Parameters
  • id - Any value that can be converted into the model’s ID type
§Returns
  • crate::Result<Option<M>> - A Result containing either:
    • Some(model) if a record was found
    • None if no record exists with the given ID

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.

Implementors§