- 🔐 Secure database creation with SQL injection prevention
- 🎯 Simple integration with existing Flask-SQLAlchemy applications
- ⚡ Automated tenant database management
- 🛠️ Flexible configuration options
- 📝 Type hints for better IDE support
pip install tenorifrom flask import Flask
from flask_sqlalchemy import SQLAlchemy
from tenori import MultiTenantManager
app = Flask(__name__)
db = SQLAlchemy(app)
# Initialize the multi-tenant manager
tenant_manager = MultiTenantManager(db)
# Create a new tenant database
@app.route('/signup', methods=['POST'])
def create_tenant():
try:
success = tenant_manager.create_tenant(user)
if success:
return {"status": "success"}
except Exception as e:
return {"status": "error", "message": str(e)}- Python 3.10+
- Flask
- Flask-SQLAlchemy
- SQLAlchemy
- MySQL Database
The package requires a properly configured Flask-SQLAlchemy instance. Your database user must have privileges to create new databases.
The main class for managing multi tenancy.
manager = MultiTenantManager(db_instance)-
create_tenant(user): Creates a new database for the specified user- Parameters:
user: User object (must have an 'id' attribute)
- Returns:
bool: True if successful, False otherwise
- Parameters:
-
get_tenant_connection(user): Gets the connection string for a tenant's database- Parameters:
user: User object (must have an 'id' attribute)
- Returns:
str: Database connection string
- Parameters:
The package provides custom exceptions for different scenarios:
MultiTenantError: Base exception classDatabaseCreationError: Raised when database creation failsInvalidConfigurationError: Raised when configuration is invalid
- Database names are automatically sanitized to prevent SQL injection
- Each tenant gets their own isolated database
- Database credentials are handled securely
- Connection strings are generated dynamically
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or need support, please open an issue on GitHub.
