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.1.tar.gz (5.8 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.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

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

Hashes for tenori-1.1.1.tar.gz
Algorithm Hash digest
SHA256 8ceb102dce77a1422e6ade35679c98b844c0900bc0550b7a645e2b4e9f642160
MD5 1d4fb55e051c1ae0cb42566e1d36d551
BLAKE2b-256 57d9d17add3d31c0d6271197f903f3bf6ff76b05f396d5cc0ab3829b0b1dcf3d

See more details on using hashes here.

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

Hashes for tenori-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a896f00e4d47ff6ab02655b02f3b44d9f31c8caed9d3711b15e7b7ef36dc711e
MD5 514cb9874b1d7c3fdc612464331c05d6
BLAKE2b-256 b6a9d1b43b8eb96b9eff11bd4279760eb9ca189c7f62077d5a13c58c24a2468e

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