Skip to content

Conversation

@arferreira
Copy link
Owner

Summary

First-class database integration for Rapina with SeaORM. Discovered and implemented through dogfooding with a real blog API.

Features:

  • DatabaseConfig - environment-aware configuration (DATABASE_URL, pool settings)
  • Db extractor - clean access to database connection in handlers
  • DbError wrapper with IntoApiError - reduces error mapping boilerplate
  • .with_database(config).await? - easy setup on Rapina builder
  • Feature flags: postgres, mysql, sqlite
  • Re-exports http, hyper, schemars, sea_orm so users don't need them in Cargo.toml

Usage:

use rapina::prelude::*;
use rapina::database::{DatabaseConfig, Db, DbError};
use rapina::sea_orm;
use sea_orm::{EntityTrait, ActiveModelTrait};

#[get("/posts")]
async fn list_posts(db: Db) -> Result<Json<Vec<Post>>> {
    let posts = Post::find().all(db.conn()).await.map_err(DbError::from)?;
    Ok(Json(posts))
}

#[tokio::main]
async fn main() -> std::io::Result<()> {
    let db_config = DatabaseConfig::from_env()?;

    Rapina::new()
        .with_database(db_config).await?
        .router(router)
        .listen("127.0.0.1:3000").await
}

Environment variables:

  • DATABASE_URL (required)
  • DATABASE_MAX_CONNECTIONS (default: 10)
  • DATABASE_MIN_CONNECTIONS (default: 1)
  • DATABASE_CONNECT_TIMEOUT (default: 30s)
  • DATABASE_IDLE_TIMEOUT (default: 600s)

Also includes macro fixes from #141 (cherry-picked).

Closes #86

Checklist

  • cargo fmt passes
  • cargo clippy has no warnings
  • Tests pass
  • Documentation updated (if needed)

@arferreira arferreira changed the title [WIP] Add database integration Add database integration Feb 11, 2026
Copy link
Collaborator

@uemuradevexe uemuradevexe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@arferreira arferreira merged commit 989c9ef into main Feb 12, 2026
2 checks passed
@arferreira arferreira deleted the add-database-integration branch February 12, 2026 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add SeaORM integration with connection pool

2 participants