mirror of
https://github.com/Phundrak/georm.git
synced 2025-11-30 19:03:59 +00:00
feat: enable transaction support via sqlx::Executor
This commit abstracts the database operations to use the generic `sqlx::Executor` trait instead of a concrete `&sqlx::PgPool`. This change allows all generated methods (find, create, update, delete, and relationships) to be executed within a `sqlx::Transaction`, in addition to a connection pool. This is a crucial feature for ensuring atomic operations and data consistency. The public-facing traits `Georm` and `Defaultable` have been updated to require `sqlx::Executor`, and the documentation has been updated to reflect this new capability.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use georm::Georm;
|
||||
use sqlx::types::BigDecimal;
|
||||
use sqlx::{Postgres, types::BigDecimal};
|
||||
|
||||
#[derive(Debug, Georm, PartialEq, Eq, Default)]
|
||||
#[georm(
|
||||
@@ -123,9 +123,12 @@ pub struct Product {
|
||||
|
||||
impl Product {
|
||||
#[allow(dead_code)]
|
||||
pub async fn find_by_name(name: String, pool: &sqlx::PgPool) -> ::sqlx::Result<Self> {
|
||||
pub async fn find_by_name<'e, E>(name: String, executor: E) -> ::sqlx::Result<Self>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = Postgres>,
|
||||
{
|
||||
::sqlx::query_as!(Self, "SELECT * FROM products WHERE name = $1", name)
|
||||
.fetch_one(pool)
|
||||
.fetch_one(executor)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user