Documentation
¶
Overview ¶
Package multitenancy provides a framework for implementing multitenancy in Go applications using GORM.
Example (PostgreSQL):
package main
import (
"gorm.io/gorm"
"github.com/bartventer/gorm-multitenancy/v6/drivers/postgres"
"github.com/bartventer/gorm-multitenancy/v6/drivers/postgres/scopes"
)
// Tenant is a public model
type Tenant struct {
gorm.Model
postgres.TenantModel // Embed the TenantModel
}
// Implement the gorm.Tabler interface
func (Tenant) TableName() string {return "public.tenants"} // Note the public. prefix
// Book is a tenant specific model
type Book struct {
gorm.Model
Title string
TenantSchema string `gorm:"column:tenant_schema"`
Tenant Tenant `gorm:"foreignKey:TenantSchema;references:SchemaName"`
}
// Implement the gorm.Tabler interface
func (Book) TableName() string {return "books"} // Note the lack of prefix
// Implement the TenantTabler interface
func (Book) IsTenantTable() bool {return true} // This classifies the model as a tenant specific model
func main(){
// Open a connection to the database
db, err := gorm.Open(postgres.New(postgres.Config{
DSN: "host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable",
}), &gorm.Config{})
if err != nil {
panic(err)
}
// Register models
if err := postgres.RegisterModels(db, &Tenant{}, &Book{}); err != nil {
panic(err)
}
// Migrate the public schema
if err := postgres.MigratePublicSchema(db); err != nil {
panic(err)
}
// Create a tenant
tenant := &Tenant{
TenantModel: postgres.TenantModel{
DomainURL: "tenant1.example.com",
SchemaName: "tenant1",
},
}
if err := db.Create(tenant).Error; err != nil {
panic(err)
}
// Create the schema for the tenant
if err := postgres.CreateSchemaForTenant(db, tenant.SchemaName); err != nil {
panic(err)
}
// Create a book for the tenant
b := &Book{
Title: "Book 1",
TenantSchema: tenant.SchemaName,
}
if err := db.Scopes(scopes.WithTenantSchema(tenant.SchemaName)).Create(b).Error; err != nil {
panic(err)
}
// Drop the schema for the tenant
if err := postgres.DropSchemaForTenant(db, tenant.SchemaName); err != nil {
panic(err)
}
}
Learn more about the package from the README.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type TenantTabler ¶
type TenantTabler interface {
// IsTenantTable returns true if the table is a tenant table
IsTenantTable() bool
}
TenantTabler is the interface for tenant tables.
Directories
¶
| Path | Synopsis |
|---|---|
|
drivers
|
|
|
postgres
Package postgres provides a [PostgreSQL] driver for [GORM], offering tools to facilitate the construction and management of multi-tenant applications.
|
Package postgres provides a [PostgreSQL] driver for [GORM], offering tools to facilitate the construction and management of multi-tenant applications. |
|
postgres/schema
Package schema provides utilities for managing PostgreSQL schemas in a multi-tenant application.
|
Package schema provides utilities for managing PostgreSQL schemas in a multi-tenant application. |
|
internal
|
|
|
testutil
Package testutil provides internal testing utilities for the application.
|
Package testutil provides internal testing utilities for the application. |
|
middleware
|
|
|
echo
Package echo provides a middleware for the [Echo] framework, which adds multi-tenancy support.
|
Package echo provides a middleware for the [Echo] framework, which adds multi-tenancy support. |
|
nethttp
Package nethttp provides a middleware for the net/http package, which adds multi-tenancy support.
|
Package nethttp provides a middleware for the net/http package, which adds multi-tenancy support. |
|
Package scopes provides a set of predefined GORM scopes for managing multi-tenant applications using the gorm-multitenancy library.
|
Package scopes provides a set of predefined GORM scopes for managing multi-tenant applications using the gorm-multitenancy library. |
|
Package tenantcontext provides context keys for the tenant and migration options.
|
Package tenantcontext provides context keys for the tenant and migration options. |
Click to show internal directories.
Click to hide internal directories.