Skip to main content

A Flask package for easily adding multi-tenancy with dedicated databases

Project description

alt text

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
  • 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

Error Handling

The package provides custom exceptions for different scenarios:

  • MultiTenantError: Base exception class
  • DatabaseCreationError: Raised when database creation fails
  • InvalidConfigurationError: 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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. 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

tenori-1.1.3.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tenori-1.1.3-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file tenori-1.1.3.tar.gz.

File metadata

  • Download URL: tenori-1.1.3.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for tenori-1.1.3.tar.gz
Algorithm Hash digest
SHA256 8788c79117cef36e76fbf84aadbc8ee26bdbc4da2d3904001320f27c8f1a5e21
MD5 5ce7371670df031f43e816b12dd84f17
BLAKE2b-256 707bcc8bd2a605292153bb9cfbdd0c44e469a6978343b1034c03ffae33141d16

See more details on using hashes here.

File details

Details for the file tenori-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: tenori-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for tenori-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2e0f95cae487ecd3b24b4238b7f8edc04663bccc89f0b6920e113d07dd2c3187
MD5 0f6edd0e3a88928e0e875e6325f2e634
BLAKE2b-256 595de490f87b980c6d19eb0ad7c0a6b794707d044b25d8cce8e34905f90257f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page