Skip to main content

Crate reinhardt_db

Crate reinhardt_db 

Source
Expand description

§Reinhardt Database

Django-style database layer for Reinhardt framework.

This crate provides a unified database abstraction that combines:

  • Database Backends: Low-level database operations
  • Connection Pooling: Advanced connection pool management
  • ORM: Django-style ORM for database queries
  • Migrations: Database schema migration system
  • Hybrid Types: Common database type abstractions
  • Associations: Relationship management between models

Equivalent to Django’s django.db package.

§Features

§Database Backends (backends module)

  • Schema Editor Abstraction: Unified BaseDatabaseSchemaEditor trait
  • Database-Specific Implementations: PostgreSQL, MySQL, SQLite support
  • DDL Operations: CREATE TABLE, ALTER TABLE, CREATE INDEX, etc.
  • Query Builder: Type-safe query construction

§Connection Pooling (pool module)

  • Advanced Pooling: SQLAlchemy-inspired connection pool management
  • Dependency Injection: Integration with Reinhardt DI system
  • Event Listeners: Connection lifecycle hooks
  • Pool Configuration: Fine-grained control over pool behavior

§ORM (orm module)

  • Django-style Models: Define database models with structs
  • QuerySet API: Chainable query builder
  • Field Types: Rich set of field types with validation
  • Relationships: ForeignKey, ManyToMany, OneToOne

§Migrations (migrations module)

  • Schema Migrations: Track and apply database schema changes
  • Auto-detection: Automatically detect model changes
  • Migration Files: Generate migration files from model changes
  • Rollback Support: Reverse migrations when needed
  • MigrationStateLoader: Django-style approach for building ProjectState
    • Replays applied migrations to reconstruct schema state
    • Enables accurate change detection without database introspection
    • Used internally by makemigrations command
  • Composite Primary Key Detection: Autodetector emits CreateCompositePrimaryKey when a model adds a primary_key constraint covering 2+ columns
  • Sequence Reset Detection: Autodetector emits SetAutoIncrementValue when a model adds or changes the sequence_reset option

§Available Database Backends

The backends crate provides multiple database backend implementations:

  • PostgreSQL: Full support with connection pooling
  • MySQL: Full support with connection pooling
  • SQLite: Full support with connection pooling
  • CockroachDB: Distributed transaction support

§Optimization Features ✅

  • Connection Pool Optimization: Idle timeout, dynamic sizing, health checks
  • Query Caching: LRU cache with TTL for prepared statements and results
  • Batch Operations: Efficient bulk insert, update, and delete operations

§Enhanced Migration Tools ✅

  • Schema Diff Detection: Automatic detection of schema changes between DB and models
  • Auto-Migration Generation: Generate migration files from detected differences
  • Migration Validation: Pre-execution validation with data loss warnings
  • Rollback Script Generation: Automatic rollback operations for safe migrations

§Quick Start

§Using Schema Editor

use reinhardt_db::backends::schema::factory::{SchemaEditorFactory, DatabaseType};
use reinhardt_query::prelude::{PostgresQueryBuilder, QueryStatementBuilder};

let factory = SchemaEditorFactory::new_postgres(pool);
let editor = factory.create_for_database(DatabaseType::PostgreSQL);

let stmt = editor.create_table_statement("users", &[
    ("id", "INTEGER PRIMARY KEY"),
    ("name", "VARCHAR(100)"),
]);
let sql = stmt.to_string(PostgresQueryBuilder);

§Using Connection Pool

use reinhardt_db::pool::{ConnectionPool, PoolConfig};

let pool = ConnectionPool::new_postgres("postgres://localhost/mydb", PoolConfig::default()).await?;
let conn = pool.acquire().await?;

§Architecture

Key modules in this crate:

  • backends: Low-level database operations, schema editor, DDL generation
  • backends_pool: Connection pool management with lifecycle hooks
  • pool: High-level pool abstraction for ConnectionPool
  • orm: Django-style model definitions, QuerySet, and field types
  • migrations: Schema migration system with auto-detection and rollback
  • hybrid: Cross-database compatible type system
  • associations: Relationship management (ForeignKey, ManyToMany)
  • [contenttypes]: Generic foreign key support

§Feature Flags

FeatureDefaultDescription
backendsenabledDatabase backend abstractions and schema editor
poolenabledConnection pooling support
ormenabledORM model definitions and QuerySet API
migrationsenabledDatabase migration system
hybridenabledCross-database hybrid type system
associationsenabledModel relationship management
postgresenabledPostgreSQL backend
sqlitedisabledSQLite backend
mysqldisabledMySQL backend
all-databasesdisabledEnable all database backends
backends-pooldisabledConnection pool backend abstractions
contenttypesdisabledGeneric foreign key support
nosqldisabledNoSQL/BSON type support
didisabledDependency injection integration
database-fulldisabledEnable all database features

Re-exports§

pub use backends::DatabaseBackend;
pub use backends::DatabaseError;
pub use orm::DatabaseConnection;
pub use pool::ConnectionPool;
pub use pool::PoolConfig;
pub use pool::PoolError;

Modules§

associations
Association proxies for Reinhardt
backends
Reinhardt Database Backends
backends_pool
Connection pooling with advanced lifecycle management
hybrid
Hybrid properties for Reinhardt
m2m_naming
Default naming convention for ManyToMany intermediate tables.
migrations
Reinhardt Migrations
naming
Identifier-naming helpers shared by the migration autodetector and the ORM runtime.
orm
Reinhardt ORM
pool
Database connection pooling for Reinhardt
prelude
Prelude module for convenient imports

Macros§

hybrid_property
Macro for defining hybrid properties