Documentation
¶
Overview ¶
Package gormtenant adds tenant-aware query, mutation, preload, raw SQL, and delete guardrails to GORM.
Index ¶
- Constants
- Variables
- func BulkCreate(ctx context.Context, db *gorm.DB, values interface{}) *gorm.DB
- func HardDelete(ctx context.Context, db *gorm.DB, value interface{}, conds ...interface{}) *gorm.DB
- func SafeExec(ctx context.Context, db *gorm.DB, sqlText string, values ...interface{}) *gorm.DB
- func SafeRaw(ctx context.Context, db *gorm.DB, sqlText string, values ...interface{}) *gorm.DB
- func TenantScope(ctx context.Context) func(*gorm.DB) *gorm.DB
- type Config
- type DeletePolicy
- type MySQLSoftDeleteUniqueIndex
- type Plugin
- type Scopes
Constants ¶
const (
// DefaultMySQLSoftDeleteMarkerField is a non-nullable delete marker suitable for MySQL unique indexes.
DefaultMySQLSoftDeleteMarkerField = "deleted_flag"
)
Variables ¶
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 ¶
BulkCreate creates a slice after applying the tenant context through the plugin callbacks.
func HardDelete ¶
HardDelete physically deletes rows and requires explicit host context.
Types ¶
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 ¶
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 (*Plugin) Initialize ¶
Initialize registers tenant callbacks.