A Flask package for easily adding multi-tenancy with dedicated databases
Project description
Tenori
Flask Multi Tenant Package
A lightweight, secure Python package for easily adding multi tenancy to Flask applications using dedicated databases per tenant.Features
- 🔐 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
Installation
pip install tenori
Quick Start
from 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(current_user)
if success:
return {"status": "success"}
except Exception as e:
return {"status": "error", "message": str(e)}
Requirements
- Python 3.10+
- Flask
- Flask-SQLAlchemy
- SQLAlchemy
Configuration
The package requires a properly configured Flask-SQLAlchemy instance. Your database user must have privileges to create new databases.
API Reference
MultiTenantManager
The main class for managing multi tenancy.
manager = MultiTenantManager(db_instance)
Methods
-
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:
Error Handling
The package provides custom exceptions for different scenarios:
MultiTenantError: Base exception classDatabaseCreationError: Raised when database creation failsInvalidConfigurationError: Raised when configuration is invalid
Security Considerations
- 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
Contributing
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
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
If you encounter any issues or need support, please open an issue on GitHub.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tenori-1.1.1.tar.gz.
File metadata
- Download URL: tenori-1.1.1.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ceb102dce77a1422e6ade35679c98b844c0900bc0550b7a645e2b4e9f642160
|
|
| MD5 |
1d4fb55e051c1ae0cb42566e1d36d551
|
|
| BLAKE2b-256 |
57d9d17add3d31c0d6271197f903f3bf6ff76b05f396d5cc0ab3829b0b1dcf3d
|
File details
Details for the file tenori-1.1.1-py3-none-any.whl.
File metadata
- Download URL: tenori-1.1.1-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a896f00e4d47ff6ab02655b02f3b44d9f31c8caed9d3711b15e7b7ef36dc711e
|
|
| MD5 |
514cb9874b1d7c3fdc612464331c05d6
|
|
| BLAKE2b-256 |
b6a9d1b43b8eb96b9eff11bd4279760eb9ca189c7f62077d5a13c58c24a2468e
|