adapter-sqlite: all repo implementations + wire fn + migrations copy
This commit is contained in:
92
crates/adapters/sqlite/src/provider_config.rs
Normal file
92
crates/adapters/sqlite/src/provider_config.rs
Normal file
@@ -0,0 +1,92 @@
|
||||
//! SQLite adapter for provider config (ProviderConfigCommand + ProviderConfigQuery).
|
||||
|
||||
use async_trait::async_trait;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use adapter_common::map_sqlx_error;
|
||||
use domain::{
|
||||
ports::provider_config::{ProviderConfigCommand, ProviderConfigQuery},
|
||||
DomainResult, ProviderConfigRow,
|
||||
};
|
||||
|
||||
pub struct SqliteProviderConfig {
|
||||
pool: SqlitePool,
|
||||
}
|
||||
|
||||
impl SqliteProviderConfig {
|
||||
pub fn new(pool: SqlitePool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ProviderConfigCommand for SqliteProviderConfig {
|
||||
async fn upsert(&self, row: &ProviderConfigRow) -> DomainResult<()> {
|
||||
sqlx::query(
|
||||
r#"INSERT INTO provider_configs (id, provider_type, config_json, enabled, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
provider_type = excluded.provider_type,
|
||||
config_json = excluded.config_json,
|
||||
enabled = excluded.enabled,
|
||||
updated_at = excluded.updated_at"#,
|
||||
)
|
||||
.bind(row.id())
|
||||
.bind(row.provider_type())
|
||||
.bind(row.config_json())
|
||||
.bind(row.enabled() as i64)
|
||||
.bind(row.updated_at())
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete(&self, id: &str) -> DomainResult<()> {
|
||||
sqlx::query("DELETE FROM provider_configs WHERE id = ?")
|
||||
.bind(id)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ProviderConfigQuery for SqliteProviderConfig {
|
||||
async fn get_all(&self) -> DomainResult<Vec<ProviderConfigRow>> {
|
||||
let rows: Vec<(String, String, String, i64, String)> = sqlx::query_as(
|
||||
"SELECT id, provider_type, config_json, enabled, updated_at FROM provider_configs",
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
Ok(rows
|
||||
.into_iter()
|
||||
.map(|(id, provider_type, config_json, enabled, updated_at)| {
|
||||
ProviderConfigRow::from_persistence(
|
||||
id,
|
||||
provider_type,
|
||||
config_json,
|
||||
enabled != 0,
|
||||
updated_at,
|
||||
)
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn get_by_id(&self, id: &str) -> DomainResult<Option<ProviderConfigRow>> {
|
||||
let row: Option<(String, String, String, i64, String)> = sqlx::query_as(
|
||||
"SELECT id, provider_type, config_json, enabled, updated_at FROM provider_configs WHERE id = ?",
|
||||
)
|
||||
.bind(id)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
Ok(row.map(|(id, provider_type, config_json, enabled, updated_at)| {
|
||||
ProviderConfigRow::from_persistence(id, provider_type, config_json, enabled != 0, updated_at)
|
||||
}))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user