multitenancy

package module
v6.1.0 Latest Latest
Warning

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

Go to latest
Published: May 23, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

README

gorm-multitenancy

Go Reference Release Go Report Card Coverage Status Build GitHub issues License FOSSA Status

GORM Multitenancy

Photo by Ashley McNamara, via ashleymcnamara/gophers (CC BY-NC-SA 4.0)

Introduction

Gorm-multitenancy is a Go package that provides a framework for implementing multitenancy in applications using GORM.

Multitenancy Approaches

There are three common approaches to multitenancy in a database:

  • Shared database, shared schema
  • Shared database, separate schemas
  • Separate databases

This package adopts the 'shared database, separate schemas' approach, providing custom drivers for seamless integration with your existing database setup.

Features

  • GORM Integration: Leverages the gorm ORM to manage the database, facilitating easy integration with your existing GORM setup.
  • Custom Database Drivers: Provides drop-in replacements for existing drivers, enabling multitenancy without the need for initialization reconfiguration.
  • HTTP Middleware: Offers middleware for seamless integration with popular routers, making it easy to manage tenant context in your application.

Database compatibility

The following databases are currently supported. Contributions for other drivers are welcome.

  • PostgreSQL

Router Integration

This package includes middleware that can be used with the routers listed below for seamless integration with the database drivers. While not a requirement, these routers are fully compatible with the provided middleware. Contributions for other routers are welcome.

  • Echo
  • Net/HTTP

Installation

go get -u github.com/bartventer/gorm-multitenancy/v6

Getting Started

Drivers
Middleware

Contributing

All contributions are welcome! Open a pull request to request a feature or submit a bug report.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

FOSSA Status

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 (t *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 (b *Book) TableName() string {return "books"} // Note the lack of prefix

// Implement the TenantTabler interface
func (b *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 Migrator

type Migrator = gorm.Migrator

Migrator is an alias for gorm.Migrator.

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.

Jump to

Keyboard shortcuts

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