scopes

package
v3.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithTenantSchema

func WithTenantSchema(tenant string) func(db *gorm.DB) *gorm.DB

WithTenantSchema alters the table name to prefix it with the tenant schema. Intended to be used with a postgres database.

The table name is retrieved from the statement if set manually, otherwise attempts to get it from the model or destination.

Example with table name set manually:

type Book struct { ... } // does not implement Tabler interface, must set TableName manually

db.Table("books").Scopes(scopes.WithTenantSchema("tenant1")).Find(&Book{})
// SELECT * FROM tenant1.books;

Example with Tabler interface:

type Book struct { ... }

func (u *Book) TableName() string { return "books" } // implements Tabler interface, no need to set TableName manually

db.Scopes(scopes.WithTenantSchema("tenant2")).Find(&Book{})
// SELECT * FROM tenant2.books;

Example with model set manually:

type Book struct { ... }

db.Model(&Book{}).Scopes(scopes.WithTenantSchema("tenant3")).Find(&Book{}) // model is set manually.
// SELECT * FROM tenant3.books;

Types

This section is empty.

Jump to

Keyboard shortcuts

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