tenancy

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DB

func DB(c *nhttp.Context) *lucid.DB

DB returns the tenant-scoped database from the request context.

func ID

func ID(c *nhttp.Context) string

ID returns just the tenant ID from context.

Types

type Config

type Config struct {
	ResolveBy      ResolveMethod
	HeaderName     string // for ResolveHeader (default: X-Tenant-ID)
	PathPrefix     string // for ResolvePath (default: first path segment)
	Strategy       Strategy
	DefaultDB      *lucid.DB                               // the main database connection
	DBResolver     func(tenant *Tenant) (*lucid.DB, error) // for StrategyDatabase
	CustomResolver func(r *http.Request) (string, error)   // for ResolveCustom
}

Config configures the tenancy system.

type GormStore

type GormStore struct {
	// contains filtered or unexported fields
}

GormStore implements TenantStore using GORM.

func NewGormStore

func NewGormStore(db *lucid.DB) *GormStore

NewGormStore creates a GORM-backed tenant store.

func (*GormStore) All

func (s *GormStore) All(_ context.Context) ([]*Tenant, error)

func (*GormStore) Delete

func (s *GormStore) Delete(_ context.Context, id string) error

func (*GormStore) FindByDomain

func (s *GormStore) FindByDomain(_ context.Context, domain string) (*Tenant, error)

func (*GormStore) FindByID

func (s *GormStore) FindByID(_ context.Context, id string) (*Tenant, error)

func (*GormStore) Save

func (s *GormStore) Save(_ context.Context, tenant *Tenant) error

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager handles tenant resolution and database scoping.

func New

func New(cfg Config) *Manager

New creates a new tenancy manager.

func (*Manager) Get

func (m *Manager) Get(id string) (*Tenant, bool)

Get returns a tenant by ID.

func (*Manager) Middleware

func (m *Manager) Middleware() router.Middleware

Middleware returns the tenancy resolution middleware.

func (*Manager) Register

func (m *Manager) Register(t *Tenant)

Register adds a tenant.

func (*Manager) Resolve

func (m *Manager) Resolve(r *http.Request) (string, error)

Resolve extracts the tenant ID from an HTTP request.

func (*Manager) ScopeDB

func (m *Manager) ScopeDB(tenant *Tenant) (*lucid.DB, error)

ScopeDB returns a tenant-scoped database connection.

func (*Manager) SetStore

func (m *Manager) SetStore(store TenantStore)

SetStore configures the tenant store.

type ResolveMethod

type ResolveMethod string

ResolveMethod defines how the tenant is identified from requests.

const (
	ResolveSubdomain ResolveMethod = "subdomain"
	ResolveHeader    ResolveMethod = "header"
	ResolvePath      ResolveMethod = "path"
	ResolveCustom    ResolveMethod = "custom"
)

type Strategy

type Strategy string

Strategy defines the tenancy isolation approach.

const (
	StrategyRow      Strategy = "row"      // shared DB, tenant_id column
	StrategySchema   Strategy = "schema"   // shared DB, per-tenant schema
	StrategyDatabase Strategy = "database" // separate DB per tenant
)

type Tenant

type Tenant struct {
	ID       string            `json:"id" gorm:"primaryKey"`
	Name     string            `json:"name"`
	Domain   string            `json:"domain,omitempty"`
	Schema   string            `json:"schema,omitempty"`
	DBName   string            `json:"db_name,omitempty"`
	Metadata map[string]string `json:"metadata,omitempty" gorm:"-"`
	Active   bool              `json:"active" gorm:"default:true"`
}

Tenant represents a single tenant.

func Current

func Current(c *nhttp.Context) *Tenant

Current returns the current tenant from the request context.

type TenantPlugin

type TenantPlugin struct {
	nimbus.BasePlugin
	Manager *Manager
}

TenantPlugin integrates multi-tenancy with Nimbus.

func NewPlugin

func NewPlugin(cfg Config) *TenantPlugin

NewPlugin creates a tenancy plugin.

func (*TenantPlugin) Boot

func (p *TenantPlugin) Boot(app *nimbus.App) error

func (*TenantPlugin) Middleware

func (p *TenantPlugin) Middleware() map[string]router.Middleware

func (*TenantPlugin) Register

func (p *TenantPlugin) Register(app *nimbus.App) error

func (*TenantPlugin) RegisterRoutes

func (p *TenantPlugin) RegisterRoutes(r *router.Router)

type TenantStore

type TenantStore interface {
	FindByID(ctx context.Context, id string) (*Tenant, error)
	FindByDomain(ctx context.Context, domain string) (*Tenant, error)
	All(ctx context.Context) ([]*Tenant, error)
	Save(ctx context.Context, tenant *Tenant) error
	Delete(ctx context.Context, id string) error
}

TenantStore is the interface for loading/saving tenants.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL