gormtenant

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultMySQLSoftDeleteMarkerField is a non-nullable delete marker suitable for MySQL unique indexes.
	DefaultMySQLSoftDeleteMarkerField = "deleted_flag"
)

Variables

View Source
var (
	// ErrTenantFieldNotFound reports that a model does not expose the configured tenant field.
	ErrTenantFieldNotFound = errors.New("gotenancy/gorm: tenant field not found")

	// ErrTenantMismatch reports that a model already contains a different tenant ID.
	ErrTenantMismatch = gotenancy.ErrTenantMismatch

	// ErrUnscopedRequiresHost reports that Unscoped is forbidden in tenant context.
	ErrUnscopedRequiresHost = errors.New("gotenancy/gorm: unscoped requires host context")

	// ErrRawRequiresHost reports that raw SQL requires explicit host context.
	ErrRawRequiresHost = errors.New("gotenancy/gorm: raw SQL requires host context")
)

Functions

func BulkCreate

func BulkCreate(ctx context.Context, db *gorm.DB, values interface{}) *gorm.DB

BulkCreate creates a slice after applying the tenant context through the plugin callbacks.

func HardDelete

func HardDelete(ctx context.Context, db *gorm.DB, value interface{}, conds ...interface{}) *gorm.DB

HardDelete physically deletes rows and requires explicit host context.

func SafeExec

func SafeExec(ctx context.Context, db *gorm.DB, sqlText string, values ...interface{}) *gorm.DB

SafeExec executes raw SQL only with explicit host context.

func SafeRaw

func SafeRaw(ctx context.Context, db *gorm.DB, sqlText string, values ...interface{}) *gorm.DB

SafeRaw executes raw SQL only with explicit host context.

func TenantScope

func TenantScope(ctx context.Context) func(*gorm.DB) *gorm.DB

TenantScope adds the current tenant filter with default configuration.

Types

type Config

type Config struct {
	TenantField        string
	SoftDeleteField    string
	IncludeSoftDeleted bool
}

Config controls the GORM tenant plugin.

type DeletePolicy

type DeletePolicy int

DeletePolicy describes the physical delete behavior for tenant-aware models.

const (
	// DeleteSoft lets GORM soft-delete support keep rows and mark them deleted.
	DeleteSoft DeletePolicy = iota

	// DeleteHard physically removes rows.
	DeleteHard
)

type MySQLSoftDeleteUniqueIndex

type MySQLSoftDeleteUniqueIndex struct {
	TenantField string
	Fields      []string
	MarkerField string
}

MySQLSoftDeleteUniqueIndex describes a MySQL-compatible unique index for soft-delete tables.

func NewMySQLSoftDeleteUniqueIndex

func NewMySQLSoftDeleteUniqueIndex(tenantField string, businessFields []string, markerField string) (MySQLSoftDeleteUniqueIndex, error)

NewMySQLSoftDeleteUniqueIndex creates a MySQL-safe unique-index column plan.

MySQL does not support partial indexes equivalent to UNIQUE (...) WHERE deleted_at IS NULL. Use a non-nullable marker column or generated column instead of putting nullable deleted_at directly in the unique index.

func (MySQLSoftDeleteUniqueIndex) Columns

func (index MySQLSoftDeleteUniqueIndex) Columns() []string

Columns returns the ordered unique-index columns.

type Plugin

type Plugin struct {
	// contains filtered or unexported fields
}

Plugin injects tenant isolation callbacks into GORM.

func New

func New(config Config) *Plugin

New creates a GORM tenant plugin.

func (*Plugin) Initialize

func (plugin *Plugin) Initialize(db *gorm.DB) error

Initialize registers tenant callbacks.

func (*Plugin) Name

func (plugin *Plugin) Name() string

Name returns the GORM plugin name.

type Scopes

type Scopes struct {
	// contains filtered or unexported fields
}

Scopes creates reusable GORM scopes for tenant-aware operations.

func NewScopes

func NewScopes(config Config) Scopes

NewScopes creates tenant-aware GORM scopes.

func (Scopes) TenantScope

func (scopes Scopes) TenantScope(ctx context.Context) func(*gorm.DB) *gorm.DB

TenantScope adds the current tenant filter to a GORM query. Host context is a no-op.

Jump to

Keyboard shortcuts

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