Production-ready SQLAlchemy toolkit for database session management, connection pooling, and common patterns.
Engine-kit is a convenience layer on top of SQLAlchemy that provides:
- โ Easy database session management with decorators
- โ Thread-safe singleton connection pooling
- โ Alembic migration integration
- โ Modular logging and monitoring
- โ Production-ready patterns and best practices
This is NOT a full ORM - it uses SQLAlchemy as the ORM and adds useful utilities on top.
from sqlalchemy_engine_kit import DatabaseManager, get_sqlite_config, with_session, Base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import Session
# 1. Define models
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String(255))
# 2. Initialize database
config = get_sqlite_config("myapp.db")
manager = DatabaseManager()
manager.initialize(config, auto_start=True)
manager.engine.create_tables(Base.metadata)
# 3. Use with decorators - simple and clean!
@with_session()
def create_user(session: Session, name: str) -> User:
user = User(name=name)
session.add(user)
session.flush()
return user
# Done!
user = create_user(name="John Doe")
print(f"Created user: {user.name}")| Feature | Description | Status |
|---|---|---|
| Session Management | Decorators and context managers for automatic session lifecycle | โ Ready |
| Connection Pooling | Configurable pool with health checks and graceful shutdown | โ Ready |
| Migrations | Alembic integration for schema versioning | โ Ready |
| Model Mixins | Timestamp, soft delete, and audit mixins | โ Ready |
| Bulk Operations | Efficient bulk insert, update, delete | โ Ready |
| Pagination | Query pagination with metadata | โ Ready |
| Eager Loading | Utilities to avoid N+1 queries | โ Ready |
| Feature | Description | Status |
|---|---|---|
| Thread Safety | All components are thread-safe | โ Ready |
| Error Handling | Comprehensive exception hierarchy | โ Ready |
| Logging | Modular logging - use yours or ours | โ Ready |
| Monitoring | Pluggable monitoring (Prometheus, Datadog, etc.) | โ Ready |
| Health Checks | Built-in database health monitoring | โ Ready |
| Retry Logic | Automatic retry on deadlock/timeout | โ Ready |
รalฤฑลan รถrnekler examples/ klasรถrรผnde:
basic_usage.py- Temel kullanฤฑm (CRUD iลlemleri, decorator'lar)transaction_example.py- Transaction yรถnetimi (atomic operations)flask_integration.py- Flask web framework entegrasyonumigration_example.py- Alembic migration kullanฤฑmฤฑ
Detaylar iรงin: examples/README.md
๐ Full Documentation Index - Complete documentation menu
| Document | Description |
|---|---|
| QUICKSTART.md | Get started in 5 minutes |
| API_REFERENCE.md | Complete API documentation |
| EXAMPLES.md | Real-world examples and integrations |
| BEST_PRACTICES.md | Best practices and guidelines |
| DEPLOYMENT.md | Production deployment guide |
| LOGGING_AND_MONITORING.md | Configure logging and monitoring |
| RUNBOOK.md | Troubleshooting common issues |
| ARCHITECTURE.md | Technical architecture guide |
| MIGRATION_GUIDE.md | Migration from SQLAlchemy |
- Web Applications (Flask, FastAPI, Django with custom ORM layer)
- Microservices needing consistent database access patterns
- Data Processing pipelines requiring reliable database connections
- APIs with high concurrency requirements
- Internal Tools wanting quick database setup
- Simple Scripts - might be overkill
- Projects Already Using Django ORM - stick with Django's ORM
- NoSQL Databases - this is for SQL databases only
- Python: 3.9+
- SQLAlchemy: 2.0+
- Databases: PostgreSQL 12+, MySQL 8+, SQLite 3.35+
# PostgreSQL
pip install psycopg2-binary
# MySQL
pip install pymysql
# Migrations
pip install alembic
# Monitoring
pip install prometheus-client
# Environment config
pip install python-dotenv# From PyPI (recommended)
pip install sqlalchemy-engine-kit
# With PostgreSQL support
pip install "sqlalchemy-engine-kit[postgres]"
# With MySQL support
pip install "sqlalchemy-engine-kit[mysql]"
# With all optional features
pip install "sqlalchemy-engine-kit[all]"
# From source (development)
pip install git+https://github.com/vidinsight/sqlalchemy-engine-kit.gitfrom sqlalchemy_engine_kit import DatabaseManager
# Initialize once at startup
manager = DatabaseManager()
manager.initialize(config, auto_start=True)
# Access anywhere in your application
manager = DatabaseManager() # Same instance!from sqlalchemy_engine_kit import with_session, with_transaction
@with_session() # Automatic session management
def read_user(session, user_id):
return session.query(User).get(user_id)
@with_transaction() # Automatic commit/rollback
def update_user(session, user_id, name):
user = session.query(User).get(user_id)
user.name = name
# Auto-commits on success, auto-rolls back on error@with_session()
def example(session):
# Direct SQLAlchemy queries
users = session.query(User).limit(10).all()
user = session.query(User).filter_by(id=1).first()
user.name = "New Name"
session.flush() # Auto-commits with @with_sessionimport logging
from sqlalchemy_engine_kit import LoggerAdapter, PrometheusMonitor
# Use your logger
LoggerAdapter.set_logger(logging.getLogger("myapp"))
# Use your monitoring
monitor = PrometheusMonitor()
manager.initialize(config, monitor=monitor)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Your Application โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Decorators (@with_session, @with_transaction) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ DatabaseManager (Singleton) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ DatabaseEngine (Connection Pool, Sessions) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ SQLAlchemy ORM โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Database Driver (psycopg2, pymysql, etc.) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Database (PostgreSQL, MySQL, SQLite) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# Run all tests
pytest tests/
# Run with coverage
pytest tests/ --cov=sqlalchemy_engine_kit --cov-report=html
# Run specific test file
pytest tests/integration/test_engine.py -v
# Run with different databases
DB_TYPE=postgresql pytest tests/Test Results: 158 passed, 81 skipped
- 158 comprehensive tests passing
- Thread-safe architecture
- Modular logging (integrate with your system)
- Modular monitoring (Prometheus, Datadog, CloudWatch)
- Connection pooling with health checks
- Comprehensive error handling
- Migration support (Alembic)
- Deployment documentation
- Runbook for common issues
- Set up pinned dependencies (
requirements.txt) - Configure logging (see LOGGING_AND_MONITORING.md)
- Set up monitoring/alerting
- Test in staging environment
- Configure backup strategy
- Review security checklist in DEPLOYMENT.md
- โ Credentials never logged
- โ Connection strings sanitized in logs
- โ Support for environment variable configuration
- โ No SQL injection vulnerabilities (uses SQLAlchemy ORM)
โ ๏ธ Use SSL/TLS for database connections in productionโ ๏ธ Rotate credentials regularlyโ ๏ธ Use secrets manager for production credentials
Contributions welcome! Please:
- Read the code - it's well-documented
- Add tests for new features
- Follow existing patterns
- Update documentation
# Development setup
git clone https://github.com/vidinsight/sqlalchemy-engine-kit.git
cd vidinsight-sqlalchemy-engine-kit
pip install -r requirements-dev.txt
pytest tests/This project is licensed under the MIT License - see the LICENSE file for details.
- Built on top of SQLAlchemy
- Migration support via Alembic
- Inspired by production needs at VidInsight
- Documentation: Check the docs in this repo
- Issues: Open an issue on GitHub
- Questions: Create a discussion on GitHub
- Security: Report security issues privately to [security@vidinsight.com]
- Async/await support (asyncio + asyncpg)
- Query caching layer
- More detailed metrics
- CLI tools for common operations
- Published to PyPI
- Comprehensive examples repository
- Video tutorials
- API documentation (Sphinx)
- Benchmarking suite
- Lines of Code: ~5,000
- Test Coverage: ~85%
- Python Version: 3.9+
- SQLAlchemy Version: 2.0+
- Maintenance Status: Active
If you find this useful, please star the repository! It helps others discover the project.
Made with โค๏ธ by the VidInsight team