scopes

package
v5.10.1 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 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.

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