Skip to content

Class tigrbl.orm.mixins.tenant_bound.TenantBound

tigrbl.orm.mixins.tenant_bound.TenantBound

Bases: _RowBound

Plug-and-play tenant isolation.

• tenant_id column is defined per subclass (declared_attr) so the schema builder sees the right flags before caching. • _RowBound’s read/list filters work because we implement is_visible.

tenant_id

tenant_id()
Source code in tigrbl/orm/mixins/tenant_bound.py
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
@declared_attr
def tenant_id(cls) -> Mapped[UUID]:
    pol = getattr(cls, TIGRBL_TENANT_POLICY_ATTR, TenantPolicy.CLIENT_SET)
    schema = _infer_schema(cls, default="public")

    in_verbs = (
        ("create", "update", "replace")
        if pol == TenantPolicy.CLIENT_SET
        else ("create",)
    )
    io = IO(
        in_verbs=in_verbs,
        out_verbs=("read", "list"),
        mutable_verbs=in_verbs,
    )

    spec = ColumnSpec(
        storage=S(
            type_=PgUUID(as_uuid=True),
            fk=ForeignKeySpec(target=f"{schema}.tenants.id"),
            nullable=False,
            index=True,
        ),
        field=F(py_type=UUID),
        io=io,
    )
    return acol(spec=spec)

is_visible staticmethod

is_visible(obj, ctx)
Source code in tigrbl/orm/mixins/tenant_bound.py
110
111
112
@staticmethod
def is_visible(obj, ctx) -> bool:
    return getattr(obj, "tenant_id", None) == _ctx_tenant_id(ctx)