use sqlx::{self, Executor, Pool, Postgres, Transaction};
pub type DB = Pool<Postgres>;
pub trait Queryer<'c>: Executor<'c, Database = sqlx::Postgres> {}
impl<'c> Queryer<'c> for &Pool<Postgres> {}
impl<'c> Queryer<'c> for &'c mut Transaction<'_, Postgres> {}
I can understand why: you can create transactions in services directly using something like:
let mut tx = pool.begin().await?;
// use tx
tx.commit().await?;
and even if this is not exactly what the Clean Architecture prescribes IMO (the services level goes into the details of persistence which is one level below) let's say I could accept it for "practicity of writing daily code".
But what I can't figure out is what I should change in the code to have at runtime the choice whether to persist on postgresql or MongoDB or in memory for example.
Much complex projects I have seen have a lot of traits in the service layer to describe the repository behavior.
And those traits are implemented for Postgresql's, MongoDB's and InMemory's one. Does it make sense to you?
Can you show (maybe in a separate branch so I can easily spot differences with git diff) what to change to start app either with Postgresql or another persistence adapter?
I imagine we need to have multiple directories under repository:
repository:
postgres
mod.rs
mongo
mod.rs
in_memory
mod.rs
And then?
Thank you very much in advance and congratulations again for this amazing project!
Dear @skerkour @skerkour-dev,
I switched from Go to Rust recently and I'm learning day by day.
Thank you for your project. It's really inspiring to see such a beautiful code. Thanks again.
I re-visited after some time this amazing project and I have a question, I'm hoping for an answer from you, if it's not too much trouble. :)
I have a similar code architecture in my project. But I'm struggling with DB transactions and different databases changed at runtime.
I see you're passing
DB
directly in services:with DB:
I can understand why: you can create transactions in services directly using something like:
and even if this is not exactly what the Clean Architecture prescribes IMO (the services level goes into the details of persistence which is one level below) let's say I could accept it for "practicity of writing daily code".
But what I can't figure out is what I should change in the code to have at runtime the choice whether to persist on postgresql or MongoDB or in memory for example.
Much complex projects I have seen have a lot of traits in the service layer to describe the repository behavior.
And those traits are implemented for Postgresql's, MongoDB's and InMemory's one. Does it make sense to you?
Can you show (maybe in a separate branch so I can easily spot differences with git diff) what to change to start app either with Postgresql or another persistence adapter?
I imagine we need to have multiple directories under
repository
:And then?
Thank you very much in advance and congratulations again for this amazing project!