refactor: extract pg_repo macro and MapDomainError trait to reduce postgres adapter boilerplate
This commit is contained in:
@@ -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::{
|
||||
@@ -119,15 +119,7 @@ impl From<JobRow> for Job {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PostgresJobRepository {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl PostgresJobRepository {
|
||||
pub fn new(pool: PgPool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
pg_repo!(PostgresJobRepository);
|
||||
|
||||
#[async_trait]
|
||||
impl JobRepository for PostgresJobRepository {
|
||||
@@ -141,7 +133,7 @@ impl JobRepository for PostgresJobRepository {
|
||||
.bind(*id.as_uuid())
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
.map_pg()?;
|
||||
|
||||
Ok(row.map(Into::into))
|
||||
}
|
||||
@@ -157,7 +149,7 @@ impl JobRepository for PostgresJobRepository {
|
||||
)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
.map_pg()?;
|
||||
|
||||
Ok(row.map(Into::into))
|
||||
}
|
||||
@@ -173,7 +165,7 @@ impl JobRepository for PostgresJobRepository {
|
||||
.bind(*batch_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())
|
||||
}
|
||||
@@ -210,7 +202,7 @@ impl JobRepository for PostgresJobRepository {
|
||||
.bind(&job.error_message)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
.map_pg()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -261,15 +253,7 @@ impl From<BatchRow> for JobBatch {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PostgresJobBatchRepository {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl PostgresJobBatchRepository {
|
||||
pub fn new(pool: PgPool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
pg_repo!(PostgresJobBatchRepository);
|
||||
|
||||
#[async_trait]
|
||||
impl JobBatchRepository for PostgresJobBatchRepository {
|
||||
@@ -281,7 +265,7 @@ impl JobBatchRepository for PostgresJobBatchRepository {
|
||||
.bind(*id.as_uuid())
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
.map_pg()?;
|
||||
|
||||
Ok(row.map(Into::into))
|
||||
}
|
||||
@@ -304,7 +288,7 @@ impl JobBatchRepository for PostgresJobBatchRepository {
|
||||
.bind(batch_status_to_str(&batch.status))
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
.map_pg()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -351,15 +335,7 @@ impl From<PluginRow> for Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PostgresPluginRepository {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl PostgresPluginRepository {
|
||||
pub fn new(pool: PgPool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
pg_repo!(PostgresPluginRepository);
|
||||
|
||||
#[async_trait]
|
||||
impl PluginRepository for PostgresPluginRepository {
|
||||
@@ -371,7 +347,7 @@ impl PluginRepository for PostgresPluginRepository {
|
||||
.bind(*id.as_uuid())
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
.map_pg()?;
|
||||
|
||||
Ok(row.map(Into::into))
|
||||
}
|
||||
@@ -383,7 +359,7 @@ impl PluginRepository for PostgresPluginRepository {
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
.map_pg()?;
|
||||
|
||||
Ok(rows.into_iter().map(Into::into).collect())
|
||||
}
|
||||
@@ -405,7 +381,7 @@ impl PluginRepository for PostgresPluginRepository {
|
||||
.bind(structured_to_json(&plugin.configuration))
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
.map_pg()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -475,15 +451,7 @@ impl From<PipelineRow> for ProcessingPipeline {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PostgresPipelineRepository {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl PostgresPipelineRepository {
|
||||
pub fn new(pool: PgPool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
pg_repo!(PostgresPipelineRepository);
|
||||
|
||||
#[async_trait]
|
||||
impl PipelineRepository for PostgresPipelineRepository {
|
||||
@@ -495,7 +463,7 @@ impl PipelineRepository for PostgresPipelineRepository {
|
||||
.bind(*id.as_uuid())
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
.map_pg()?;
|
||||
|
||||
Ok(row.map(Into::into))
|
||||
}
|
||||
@@ -508,7 +476,7 @@ impl PipelineRepository for PostgresPipelineRepository {
|
||||
.bind(event)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
.map_pg()?;
|
||||
|
||||
Ok(rows.into_iter().map(Into::into).collect())
|
||||
}
|
||||
@@ -526,7 +494,7 @@ impl PipelineRepository for PostgresPipelineRepository {
|
||||
.bind(steps_to_json(&pipeline.steps))
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
.map_pg()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user