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.2.0.tar.gz (190.1 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.2.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlalchemy_tenants-0.2.0.tar.gz
  • Upload date:
  • Size: 190.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sqlalchemy_tenants-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2a7e560f109f7b0808dc990ba26c897ca798487c102774bcba231ce8a2745fd7
MD5 41e4490fe4a01d793a34ac3b48bef506
BLAKE2b-256 1125fd7b4dba6204b7b3fccc41a8e095c5cce3ea078091f3a9e9f3c637172cb8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqlalchemy_tenants-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sqlalchemy_tenants-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0222a6ccbe8713126b6edb04917d81d22e2ef4d9967f694b7001f61b7d1c2865
MD5 39abae65d9a755985c80a9c905c6b7ed
BLAKE2b-256 7895400744629f2a840a1585a5623933a07c1821096fff57a4c474658245bd10

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