Skip to main content

Add your description here

Project description

sqlalchemy-tenants

Multi-tenancy with SQLAlchemy made easy.

GitHub Actions Workflow Status Codecov (with branch) Package version


Documentation: telemaco019.github.io/sqlalchemy-tenants

If you like the project please support it by leaving a star ✨


Overview

sqlalchemy-tenants makes it easy and safe to implement multi-tenancy in your application using SQLAlchemy. It enables secure, shared use of a single database across multiple tenants using Row-Level Security (RLS).

Key Features

  • 🔒 Strong Data Segregation via RLS: Automatic query and write scoping using Row-Level Security.
  • ⚙️ Straightforward Integration: Just a decorator and a session manager.
  • 📦 Full SQLAlchemy support: Compatible with both sync and async workflows.

Supported Databases

  • PostgreSQL only (support for more databases is planned).

Example Usage

from sqlalchemy_tenants import with_rls
from sqlalchemy_tenants.aio.managers import PostgresManager
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import select, insert

engine = create_async_engine("postgresql+asyncpg://user:password@localhost/dbname")
manager = PostgresManager.from_engine(engine, schema="public")

@with_rls
class MyTable(Base):
    __tablename__ = "my_table"

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column()
    tenant: Mapped[str] = mapped_column()  # Required tenant column


async with manager.new_tenant_session("tenant_1") as session:
    # ✅ Only returns tenant_1’s rows
    await session.execute(select(MyTable))  
    
    # ❌ Raises error: mismatched tenant
    await session.execute(  
        insert(MyTable).values(id=1, name="Example", tenant="tenant_2")
    )
    
    # ✅ Correct insert: use session.tenant for current tenant
    await session.execute(
        insert(MyTable).values(id=1, name="Example", tenant=session.tenant)
    )

Both sync and async versions are available.

🔍 Want more? Check out the documentation and the examples for additional use cases.

License

This project is licensed under the MIT license. See the LICENSE file for details.

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

sqlalchemy_tenants-0.1.1.tar.gz (170.7 kB view details)

Uploaded Source

Built Distribution

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

sqlalchemy_tenants-0.1.1-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_tenants-0.1.1.tar.gz.

File metadata

  • Download URL: sqlalchemy_tenants-0.1.1.tar.gz
  • Upload date:
  • Size: 170.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.14

File hashes

Hashes for sqlalchemy_tenants-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d4159c6200f50e3e7d1a3108c5f55140313a27de1d9976588feb51e025cb02ad
MD5 2bfb2bf8a1cf4f38f08dc52dab836a3f
BLAKE2b-256 cb9dac3be85d0502616b1ee0072bc1742e87ecf331427ad47e96c29f7d634696

See more details on using hashes here.

File details

Details for the file sqlalchemy_tenants-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_tenants-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 506cb8c8a1e613a9642da5c063107f96e0740b4afc9f7fab11ba3c6504ebea15
MD5 8e63a0515560c37fc215046f5c858a83
BLAKE2b-256 0453fb938406de8b70f2e5b38c38dc1d7c7adf20b04b084ef0df3bb727927b37

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