pub trait SelectRepository<M>: Repository<M>{
// 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 theModel
trait andFromRow
for the database’s row type.
§Required Methods
Implementing repositories must define:
get_all_query
- Creates a query to retrieve all recordsget_by_id_query
- Creates a query to retrieve a record by ID
§Provided Methods
These methods are automatically provided based on the required methods:
get_all_with_executor
- Execute the get_all query with a custom executorget_all
- Retrieve all records using the repository’s poolget_by_id_with_executor
- Execute the get_by_id query with a custom executorget_by_id
- Retrieve a record by ID using the repository’s pool
§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
- When implementing this trait, you only need to define:
- The execution methods are provided automatically based on these query methods
- Consider implementing pagination for
get_all
if the table may contain a large number of records - Use parameter binding to prevent SQL injection
- Consider caching strategies for frequently accessed data
- The trait supports using custom executors (like transactions) via the
_with_executor
methods
Required Methods§
Sourcefn get_all_query(&self) -> QueryAs<'_, M>
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 typeM
§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")
}
Sourcefn get_by_id_query(&self, id: impl Into<M::Id>) -> QueryAs<'_, M>
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 typeM
§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§
Sourceasync fn get_all_with_executor<E>(&self, tx: E) -> Result<Vec<M>>
async fn get_all_with_executor<E>(&self, tx: E) -> Result<Vec<M>>
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
crate::Result<Vec<M>>
- A Result containing a vector of all models if successful
§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.
Sourceasync fn get_all(&self) -> Result<Vec<M>>
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
crate::Result<Vec<M>>
- A Result containing a vector of all models if successful
§Warning
Be cautious with this method on large tables as it could consume significant memory and impact database performance. Consider implementing pagination instead.
Sourceasync fn get_by_id_with_executor<E>(
&self,
tx: E,
id: impl Into<M::Id>,
) -> Result<Option<M>>
async fn get_by_id_with_executor<E>( &self, tx: E, id: impl Into<M::Id>, ) -> Result<Option<M>>
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 queryid
- 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 foundNone
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
}
Sourceasync fn get_by_id(&self, id: impl Into<M::Id>) -> Result<Option<M>>
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 foundNone
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.