Skip to main content

Multi-tenancy

Every query needs a tenant condition.

Filter every query by tenant​

A global filter applies to every query of a model, including joins, Include, Has, subqueries and bulk writes. It reads the tenant from the request's context when the query runs.

type tenantKey struct{}

func WithTenant(ctx context.Context, id int64) context.Context {
return context.WithValue(ctx, tenantKey{}, id)
}

func (Invoice) Configure(b *ant.ModelBuilder[Invoice]) {
b.Filter("tenant", func(ctx context.Context) ant.Cond {
id, ok := ctx.Value(tenantKey{}).(int64)
if !ok {
return ant.FailedCond(ErrNoTenant) // fail closed
}

return ant.Eq(&Invoices.TenantID, id)
})
}

Set the tenant where the request starts and pass that context to every call:

ctx = WithTenant(ctx, 7)

invoices, err := db.Invoices.Where(ant.Gt(&Invoices.Total, 100)).All(ctx)
// SELECT ... FROM "invoices" WHERE "invoices"."total" > $1 AND "invoices"."tenant_id" = $2
// -- $1 = 100, $2 = 7

_, err = db.Invoices.Where(ant.Eq(&Invoices.ID, 1)).Update(ctx, ant.Set(&Invoices.Total, 0))
// UPDATE "invoices" SET "total" = $1 WHERE "invoices"."id" = $2 AND "invoices"."tenant_id" = $3

Fail closed​

With no tenant in the context, the filter returns ant.FailedCond and the query fails with ErrNoTenant before any SQL is sent. An empty condition would show every tenant's rows to code that forgot to set one.

_, err := db.Invoices.All(context.Background())
// err is ErrNoTenant

Add the column, filter and insert value to every model​

An embedded struct with ConfigureMixin brings the column, the filter and the insert value together.

type Tenanted struct {
TenantID int64
}

func (Tenanted) ConfigureMixin(m *ant.Mixin) {
m.Filter("tenant", func(ctx context.Context) ant.Cond {
id, err := TenantOf(ctx)
if err != nil {
return ant.FailedCond(err)
}

return m.Eq("tenant_id", id)
})
}

func (t *Tenanted) BeforeInsert(ctx context.Context) error {
id, err := TenantOf(ctx)
t.TenantID = id

return err
}

func TenantOf(ctx context.Context) (int64, error) {
id, ok := ctx.Value(tenantKey{}).(int64)
if !ok {
return 0, ErrNoTenant
}

return id, nil
}

A model embeds it:

type Bill struct {
Tenanted
ID int64
Total int64
}

Reads and writes carry the tenant. An insert without one is refused:

err := db.Bills.Insert(WithTenant(ctx, 7), &Bill{Total: 5})
// INSERT INTO "bills" ("tenant_id", "total") VALUES ($1, $2) RETURNING "id"
// -- $1 = 7, $2 = 5

err = db.Bills.Insert(ctx, &Bill{Total: 5})
// err is ErrNoTenant: BeforeInsert fails, and the insert is rolled back

n, err := db.Bills.Where(ant.Eq(&Bills.ID, 1)).Delete(WithTenant(ctx, 7))
// DELETE FROM "bills" WHERE "bills"."id" = $1 AND "bills"."tenant_id" = $2

The ledger example is built this way.

What a filter does not cover​

func (Member) Configure(b *ant.ModelBuilder[Member]) {
b.Index(&Members.TenantID).And(&Members.Email).Unique()
}

Enforce it in the database​

A filter is not a security boundary. PostgreSQL's row-level security enforces the same rule in the database. Set the tenant per transaction with SET LOCAL, inside db.Tx.