Skip to main content

Crate modkit_db

Crate modkit_db 

Source
Expand description

ModKit Database abstraction crate.

This crate provides a unified interface for working with different databases (SQLite, PostgreSQL, MySQL) through SQLx, with optional SeaORM integration. It emphasizes typed connection options over DSN string manipulation and implements strict security controls (e.g., SQLite PRAGMA whitelist).

§Features

  • pg, mysql, sqlite: enable SQLx backends
  • sea-orm: add SeaORM integration for type-safe operations

§New Architecture

The crate now supports:

  • Typed DbConnectOptions using sqlx ConnectOptions (no DSN string building)
  • Per-module database factories with configuration merging
  • SQLite PRAGMA whitelist for security
  • Environment variable expansion in passwords and DSNs

§Example (DbManager API)

use modkit_db::{DbManager, GlobalDatabaseConfig, DbConnConfig};
use figment::{Figment, providers::Serialized};
use std::path::PathBuf;
use std::sync::Arc;

// Create configuration using Figment
let figment = Figment::new()
    .merge(Serialized::defaults(serde_json::json!({
        "db": {
            "servers": {
                "main": {
                    "host": "localhost",
                    "port": 5432,
                    "user": "app",
                    "password": "${DB_PASSWORD}",
                    "dbname": "app_db"
                }
            }
        },
        "test_module": {
            "database": {
                "server": "main",
                "dbname": "module_db"
            }
        }
    })));

// Create DbManager
let home_dir = PathBuf::from("/app/data");
let db_manager = Arc::new(DbManager::from_figment(figment, home_dir).unwrap());

// Use in runtime with DbOptions::Manager(db_manager)
// Modules can then use: ctx.db_required_async().await?

Re-exports§

pub use advisory_locks::DbLockGuard;
pub use advisory_locks::LockConfig;
pub use config::DbConnConfig;
pub use config::GlobalDatabaseConfig;
pub use config::PoolCfg;
pub use manager::DbManager;
pub use options::ConnectionOptionsError;
pub use options::DbConnectOptions;
pub use options::build_db_handle;
pub use options::redact_credentials_in_dsn;

Modules§

advisory_locks
Advisory locks implementation with namespacing and retry policies.
config
Database configuration types.
manager
Database manager for per-module database connections.
odata
OData integration for SeaORM with security-scoped pagination.
options
Database connection options and configuration types.
secure
Secure ORM layer for scoped database access.

Structs§

ConnectOpts
Connection options. Extended to cover common sqlx pool knobs; each driver applies the subset it supports.
DbHandle
Main handle.

Enums§

DbEngine
Supported engines.
DbError
Typed error for the DB handle and helpers.
DbPool
One concrete sqlx pool.
DbTransaction
Database transaction wrapper (lifetime-bound to the pool).

Traits§

DbConnTrait
The generic API for a database connection that can perform query or execute statements. It abstracts database connection and transaction

Type Aliases§

Result
Library-local result type.