refactor: extract pg_repo macro and MapDomainError trait to reduce postgres adapter boilerplate

This commit is contained in:
2026-05-31 18:24:16 +02:00
parent 2fe0a4c245
commit c16c9d4581
9 changed files with 144 additions and 249 deletions

View File

@@ -1,4 +1,4 @@
use crate::db::PgPool;
use crate::helpers::{pg_repo, MapDomainError};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use domain::{
@@ -67,15 +67,7 @@ impl TryFrom<AssetRow> for Asset {
}
}
pub struct PostgresAssetRepository {
pool: PgPool,
}
impl PostgresAssetRepository {
pub fn new(pool: PgPool) -> Self {
Self { pool }
}
}
pg_repo!(PostgresAssetRepository);
#[async_trait]
impl AssetRepository for PostgresAssetRepository {
@@ -88,7 +80,7 @@ impl AssetRepository for PostgresAssetRepository {
.bind(*id.as_uuid())
.fetch_optional(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.map_pg()?;
row.map(TryInto::try_into).transpose()
}
@@ -102,7 +94,7 @@ impl AssetRepository for PostgresAssetRepository {
.bind(checksum.as_str())
.fetch_all(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.map_pg()?;
rows.into_iter().map(TryInto::try_into).collect()
}
@@ -125,7 +117,7 @@ impl AssetRepository for PostgresAssetRepository {
.bind(offset as i64)
.fetch_all(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.map_pg()?;
rows.into_iter().map(TryInto::try_into).collect()
}
@@ -157,7 +149,7 @@ impl AssetRepository for PostgresAssetRepository {
.bind(asset.created_at.as_datetime())
.execute(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.map_pg()?;
Ok(())
}
@@ -166,7 +158,7 @@ impl AssetRepository for PostgresAssetRepository {
.bind(*id.as_uuid())
.execute(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.map_pg()?;
Ok(())
}
}
@@ -253,15 +245,7 @@ impl From<AssetMetadataRow> for AssetMetadata {
}
}
pub struct PostgresAssetMetadataRepository {
pool: PgPool,
}
impl PostgresAssetMetadataRepository {
pub fn new(pool: PgPool) -> Self {
Self { pool }
}
}
pg_repo!(PostgresAssetMetadataRepository);
#[async_trait]
impl AssetMetadataRepository for PostgresAssetMetadataRepository {
@@ -273,7 +257,7 @@ impl AssetMetadataRepository for PostgresAssetMetadataRepository {
.bind(*asset_id.as_uuid())
.fetch_all(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.map_pg()?;
Ok(rows.into_iter().map(Into::into).collect())
}
@@ -291,7 +275,7 @@ impl AssetMetadataRepository for PostgresAssetMetadataRepository {
.bind(source_to_str(&source))
.fetch_optional(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.map_pg()?;
Ok(row.map(Into::into))
}
@@ -310,7 +294,7 @@ impl AssetMetadataRepository for PostgresAssetMetadataRepository {
.bind(metadata.updated_at.as_datetime())
.execute(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.map_pg()?;
Ok(())
}
@@ -324,7 +308,7 @@ impl AssetMetadataRepository for PostgresAssetMetadataRepository {
.bind(source_to_str(&source))
.execute(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.map_pg()?;
Ok(())
}
}
@@ -407,15 +391,7 @@ impl From<GroupRow> for DuplicateGroup {
}
}
pub struct PostgresDuplicateRepository {
pool: PgPool,
}
impl PostgresDuplicateRepository {
pub fn new(pool: PgPool) -> Self {
Self { pool }
}
}
pg_repo!(PostgresDuplicateRepository);
#[async_trait]
impl DuplicateRepository for PostgresDuplicateRepository {
@@ -427,7 +403,7 @@ impl DuplicateRepository for PostgresDuplicateRepository {
.bind(*id.as_uuid())
.fetch_optional(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.map_pg()?;
Ok(row.map(Into::into))
}
@@ -439,7 +415,7 @@ impl DuplicateRepository for PostgresDuplicateRepository {
)
.fetch_all(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.map_pg()?;
Ok(rows.into_iter().map(Into::into).collect())
}
@@ -452,7 +428,7 @@ impl DuplicateRepository for PostgresDuplicateRepository {
.bind(serde_json::json!([{"asset_id": asset_id.as_uuid()}]))
.fetch_all(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.map_pg()?;
Ok(rows.into_iter().map(Into::into).collect())
}
@@ -472,7 +448,7 @@ impl DuplicateRepository for PostgresDuplicateRepository {
.bind(candidates_to_json(&group.candidates))
.execute(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.map_pg()?;
Ok(())
}
}