scopes

package
v6.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package scopes provides a set of predefined GORM scopes for managing multi-tenant applications using the gorm-multitenancy library.

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(WithTenantSchema("tenant2")).Find(&Book{})
// SELECT * FROM "tenant2"."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(WithTenantSchema("tenant1")).Find(&Book{})
// SELECT * FROM "tenant1"."books"

Example with model set manually:

type Book struct { ... }

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

Example with destination set to a pointer to a struct:

type Book struct { ... }

db.Scopes(WithTenantSchema("tenant1")).Find(&Book{}) // destination is set to a pointer to a struct.
// SELECT * FROM "tenant1"."books"

Example with destination set to a pointer to an array/slice:

type Book struct { ... }

db.Scopes(WithTenantSchema("tenant1")).Find(&[]Book{}) // destination is set to a pointer to an array/slice.
// SELECT * FROM "tenant1"."books"

Types

This section is empty.

Jump to

Keyboard shortcuts

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