What problem does it solve?
Provides a scoped, operation-level mechanism to pass metadata such as audit user IDs, tenant identifiers, or migration options into GORM operations and hooks so you do not rely on global state or ad-hoc context passing.
Core Features & Use Cases
- Set and Get values that propagate into all hooks, including nested association hooks, enabling audit trails and conditional hook behavior.
- InstanceSet and InstanceGet for statement-scoped values that do not propagate to association hooks, useful for per-model transformations or temporary values.
- Set table options during migrations to configure database engine or table comments for MySQL/MariaDB.
- Common uses include audit logging, tenant isolation, skipping or toggling hook logic, and passing migration configuration.
Quick Start
Set an audit_user using db.Set before creating a model so the BeforeCreate hook can read and record the audit_user.