Documentation
¶
Index ¶
- Constants
- type CreatedAt
- type DeletedAt
- type EventBus
- type EventHandler
- type Factory
- func (f *Factory) Count(count int) contractsorm.Factory
- func (f *Factory) Create(value any, attributes ...map[string]any) (any, error)
- func (f *Factory) CreateQuietly(value any, attributes ...map[string]any) (any, error)
- func (f *Factory) Make(value any, attributes ...map[string]any) (any, error)
- func (f *Factory) Table(table string) contractsorm.Factory
- type Model
- type Orm
- func (r *Orm) Close() error
- func (r *Orm) Connection(name string) contractsorm.Orm
- func (r *Orm) DB() (*sql.DB, error)
- func (r *Orm) DatabaseName() string
- func (r *Orm) DisableDebug()
- func (r *Orm) DisableQueryLog()
- func (r *Orm) EnableDebug()
- func (r *Orm) EnableQueryLog()
- func (r *Orm) Factory() contractsorm.Factory
- func (r *Orm) FlushQueryLog()
- func (r *Orm) GetQueryLog() []contractsorm.QueryLog
- func (r *Orm) IsDebug() bool
- func (r *Orm) Name() string
- func (r *Orm) Observe(model any, observer contractsorm.Observer)
- func (r *Orm) Query() contractsorm.Query
- func (r *Orm) Refresh()
- func (r *Orm) SetQuery(query contractsorm.Query)
- func (r *Orm) Transaction(txFunc func(tx contractsorm.Query) error, opts ...*sql.TxOptions) error
- func (r *Orm) WithContext(ctx context.Context) contractsorm.Orm
- type OrmOption
- type ShortID
- type SoftDeletes
- type Timestamps
- type UpdatedAt
Constants ¶
const ( EventCreating = "model.creating" EventCreated = "model.created" EventUpdating = "model.updating" EventUpdated = "model.updated" EventSaving = "model.saving" EventSaved = "model.saved" EventDeleting = "model.deleting" EventDeleted = "model.deleted" EventRestoring = "model.restoring" EventRestored = "model.restored" )
Event names for model lifecycle events
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CreatedAt ¶ added in v0.11.0
CreatedAt provides only the created timestamp for immutable models. Embed this when you need only created_at without updated_at (e.g., audit logs, event sourcing).
type DeletedAt ¶ added in v0.12.0
DeletedAt represents a soft delete trait using the "deleted_at" column (Laravel-compatible). Use this when your schema follows the Laravel Eloquent convention.
func (*DeletedAt) DeletedAtColumn
deprecated
added in
v0.12.0
func (*DeletedAt) SoftDeletedAtColumn ¶ added in v0.12.0
SoftDeletedAtColumn returns the soft delete column name used in database queries. Implements the SoftDeleteColumnNamer interface.
type EventBus ¶
type EventBus struct {
// contains filtered or unexported fields
}
EventBus is a lightweight internal event bus for model lifecycle events.
func (*EventBus) Listen ¶
func (e *EventBus) Listen(eventName string, handler EventHandler)
Listen registers a handler for an event name.
type EventHandler ¶
type EventHandler func(event any)
EventHandler is a function that handles an event.
type Factory ¶
type Factory struct {
// contains filtered or unexported fields
}
Factory implements the Factory interface for creating test data.
func (*Factory) Count ¶
func (f *Factory) Count(count int) contractsorm.Factory
Count sets the number of models that should be generated.
func (*Factory) Create ¶
Create creates a model and persists it to the database, returning the created instance(s).
func (*Factory) CreateQuietly ¶
CreateQuietly creates a model and persists it to the database without firing any model events, returning the created instance(s).
type Model ¶
type Model struct {
ID uint `json:"id"`
Timestamps
}
Model represents a base model with ID and timestamps.
type Orm ¶
type Orm struct {
// contains filtered or unexported fields
}
Orm represents the ORM instance for database operations.
func BuildOrm ¶
func BuildOrm(ctx context.Context, dbConfig *db.DBConfig, connection string, log log.Log, refresh func(), opts ...OrmOption) (*Orm, error)
BuildOrm builds and initializes a new Orm instance with the given configuration.
func BuildOrmFromDB ¶ added in v0.9.0
func BuildOrmFromDB(ctx context.Context, sqlDB *sql.DB, driverName contractsdb.Driver, connection string, dbConfig *db.DBConfig, log log.Log, refresh func()) (*Orm, error)
BuildOrmFromDB builds an Orm instance from an already-open *sql.DB. The caller retains ownership of sqlDB; connection pool settings are not modified.
func NewOrm ¶
func NewOrm( ctx context.Context, dbConfig *db.DBConfig, connection string, query contractsorm.Query, queries map[string]contractsorm.Query, log log.Log, modelToObserver []contractsorm.ModelToObserver, refresh func(), drivers map[string]driver.Driver, dbConnections map[string]*sql.DB, ) *Orm
NewOrm creates a new Orm instance.
func (*Orm) Close ¶
Close closes the database connection for this Orm instance only.
Only the connection matching r.connection is closed and cleaned up. This prevents a secondary Database (created via Connection()) from closing connections belonging to other Database instances that share the same dbConnections map.
For array-driver connections, Array.Cleanup() is called to remove populated/locks sync.Map entries — preventing unbounded memory growth in long-running services.
func (*Orm) Connection ¶
func (r *Orm) Connection(name string) contractsorm.Orm
func (*Orm) DatabaseName ¶
func (*Orm) DisableDebug ¶ added in v0.10.0
func (r *Orm) DisableDebug()
DisableDebug disables debug mode at runtime for all queries.
func (*Orm) DisableQueryLog ¶
func (r *Orm) DisableQueryLog()
func (*Orm) EnableDebug ¶ added in v0.10.0
func (r *Orm) EnableDebug()
EnableDebug enables debug mode at runtime for all queries.
func (*Orm) EnableQueryLog ¶
func (r *Orm) EnableQueryLog()
func (*Orm) Factory ¶
func (r *Orm) Factory() contractsorm.Factory
func (*Orm) FlushQueryLog ¶
func (r *Orm) FlushQueryLog()
func (*Orm) GetQueryLog ¶
func (r *Orm) GetQueryLog() []contractsorm.QueryLog
func (*Orm) Query ¶
func (r *Orm) Query() contractsorm.Query
func (*Orm) SetQuery ¶
func (r *Orm) SetQuery(query contractsorm.Query)
func (*Orm) Transaction ¶
Transaction runs a callback wrapped in a database transaction. It automatically commits the transaction if the callback returns nil. If the callback returns an error or a panic occurs, the transaction is rolled back.
func (*Orm) WithContext ¶
func (r *Orm) WithContext(ctx context.Context) contractsorm.Orm
type OrmOption ¶ added in v0.5.0
type OrmOption func(*ormOptions)
OrmOption is a function that configures Orm options.
func WithSkipPing ¶ added in v0.5.0
WithSkipPing sets whether to skip the database ping during initialization.
type ShortID ¶ added in v0.11.0
type ShortID struct {
ID string `json:"id" db:"id"`
}
ShortID provides a short string primary key field. Embed it in your model to opt into client-generated short IDs. No timestamps, no soft deletes — just the ID.
type SoftDeletes ¶
type SoftDeletes struct {
SoftDeletedAt sql.NullTime `json:"soft_deleted_at,omitempty" db:"soft_deleted_at"`
}
SoftDeletes represents a soft delete trait using the "soft_deleted_at" column. This is the default — use DeletedAt for Laravel-compatible "deleted_at" column.
func (*SoftDeletes) DeletedAtColumn
deprecated
added in
v0.12.0
func (sd *SoftDeletes) DeletedAtColumn() string
DeletedAtColumn returns the soft delete column name used in database queries.
Deprecated: Use SoftDeletedAtColumn() instead.
func (*SoftDeletes) SoftDeletedAtColumn ¶ added in v0.12.0
func (sd *SoftDeletes) SoftDeletedAtColumn() string
SoftDeletedAtColumn returns the soft delete column name used in database queries. Implements the SoftDeleteColumnNamer interface.