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:
2025-08-09 12:19:06 +02:00
parent 3307aa679d
commit 49c7d86102
19 changed files with 230 additions and 112 deletions

View File

@@ -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
}
}