Generic Db Pool for configurable app

I'm working on a web application / server using actix where I want to support multiple database types through a toml config for people deploying the application as well as simplify development by using a SQLite database locally and Postgres or MySQL in production (or SQLite if that's your thing).

My issue is that I need to wrap an r2d2 Pool in the actix state and I haven't quite wrapped my head around Traits and generics enough to see a way to clearly implementing this.

I have a state struct like so

pub struct AppState {
    pub template: tera::Tera,
    pub db: DbPool,

And DbPool is intented to provide a wrapper around the r2d2 pool connection (or other type of connection manager):

pub enum DbPoolType {

pub struct DbPool {
    pub pool_pg: Option<PgPool>,
    pub pool_sqlite: Option<SqlitePool>,
    pub pool_type: DbPoolType,

To speed things up for now (and so that I don't have to implement a translation layer between the different databases) i'm implementing traits on my models which handle the different database types, it's a lot of code to write but I don't have time to figure out how to translate between the different Db types at the moment (and I don't want to use an ORM / Diesel / Rustorm):

// models/
use traits::Queryable;

pub struct SomeModel {
    pub uuid: String,

impl Queryable<SqliteConnection> for SomeModel {
    type Result = Result<SomeModel, Error>;

    fn get_by_id(conn: SqliteConnection, id: String) -> Self::Result {
         // get the model

// api/
use models::SomeModel;
use traits::Queryable;

pub fn get_stuff_by_id(req: &HttpRequest<AppState>) -> Result<HttpResponse, Error> {
     let conn = req.state().db.get_conn();

     let result = SomeModel::get_by_id(&conn).unwrap();

     // Return the result

What I'm confused about is at the DbPool layer what the path forward should be there in order to allow places like the api endpoint above to just call get_conn to get a connection off the pool to use during the request.

I've tried implementing DbPool as a generic:

pub struct DbPool<T> {
    pub pool: T,

impl<T> DbPool<T> {
    pub fn get_conn(&self) -> ? {
         match self.pool_type {
               DbPoolType::Pg {
               _ => {}

But i'm sort of at a loss what to do here in order to accomplish what I want... Any pointers and help would be greatly appreciated or if there is an application with a similar structure where it supports multiple database types post-compilation.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.