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.0.0.tar.gz (4.1 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.0.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tenori-1.0.0.tar.gz
Algorithm Hash digest
SHA256 739668277d13dd1d5fe30bcfec5dbe37bf0d202ae7c41f1d147690b0eb5e1055
MD5 7410dd9f0a360df3b379ca8ee32b170d
BLAKE2b-256 e5322d1f96c0fa816b1b2f3c55270f458c0ee8068ccaae7a0675eb723c302abb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tenori-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f2730db28ebac5883bf4fe3a16fa38e14d9e9972092579ec8b44c35026965cf
MD5 dfa840fd4bd81030e23ecee1f6c83206
BLAKE2b-256 6c6b27242600baee6f0bb1c0923f8f82377715bce22dd8470b93f4db52cb7dc6

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