Documentation
¶
Overview ¶
Package models provides the embeddable base models that forge generate model scaffolds into your structs: identifiers, timestamps, soft deletes and audit columns, with the hooks that keep them current.
These were ported from the retired database extension, which built them on bun. The field layout and column names are unchanged, so a table created under the old models still matches. What changed is the tag namespace and the hook signature, because grove drives both differently.
Index ¶
- func GetUserID(ctx context.Context) (int64, bool)
- func GetXIDUserID(ctx context.Context) (xid.ID, bool)
- func SetUserID(ctx context.Context, userID int64) context.Context
- func SetXIDUserID(ctx context.Context, userID xid.ID) context.Context
- type AuditModel
- func (m *AuditModel) BeforeDelete(ctx context.Context, _ *hook.QueryContext) error
- func (m *AuditModel) BeforeInsert(ctx context.Context, _ *hook.QueryContext) error
- func (m *AuditModel) BeforeUpdate(ctx context.Context, _ *hook.QueryContext) error
- func (m *AuditModel) IsDeleted() bool
- func (m *AuditModel) Restore()
- type BaseModel
- type SoftDeleteModel
- func (m *SoftDeleteModel) BeforeDelete(ctx context.Context, _ *hook.QueryContext) error
- func (m *SoftDeleteModel) BeforeInsert(ctx context.Context, _ *hook.QueryContext) error
- func (m *SoftDeleteModel) BeforeUpdate(ctx context.Context, _ *hook.QueryContext) error
- func (m *SoftDeleteModel) IsDeleted() bool
- func (m *SoftDeleteModel) Restore()
- type TimestampModel
- type UUIDModel
- type UUIDSoftDeleteModel
- func (m *UUIDSoftDeleteModel) BeforeDelete(ctx context.Context, _ *hook.QueryContext) error
- func (m *UUIDSoftDeleteModel) BeforeInsert(ctx context.Context, _ *hook.QueryContext) error
- func (m *UUIDSoftDeleteModel) BeforeUpdate(ctx context.Context, _ *hook.QueryContext) error
- func (m *UUIDSoftDeleteModel) IsDeleted() bool
- func (m *UUIDSoftDeleteModel) Restore()
- type XIDAuditModel
- func (m *XIDAuditModel) BeforeDelete(ctx context.Context, _ *hook.QueryContext) error
- func (m *XIDAuditModel) BeforeInsert(ctx context.Context, _ *hook.QueryContext) error
- func (m *XIDAuditModel) BeforeUpdate(ctx context.Context, _ *hook.QueryContext) error
- func (m *XIDAuditModel) IsDeleted() bool
- func (m *XIDAuditModel) Restore()
- type XIDModel
- type XIDSoftDeleteModel
- func (m *XIDSoftDeleteModel) BeforeDelete(ctx context.Context, _ *hook.QueryContext) error
- func (m *XIDSoftDeleteModel) BeforeInsert(ctx context.Context, _ *hook.QueryContext) error
- func (m *XIDSoftDeleteModel) BeforeUpdate(ctx context.Context, _ *hook.QueryContext) error
- func (m *XIDSoftDeleteModel) IsDeleted() bool
- func (m *XIDSoftDeleteModel) Restore()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AuditModel ¶
type AuditModel struct {
ID int64 `grove:"id,pk,autoincrement" json:"id"`
CreatedAt time.Time `grove:"created_at,nullzero,notnull,default:current_timestamp" json:"created_at"`
CreatedBy *int64 `grove:"created_by" json:"created_by,omitempty"`
UpdatedAt time.Time `grove:"updated_at,nullzero,notnull,default:current_timestamp" json:"updated_at"`
UpdatedBy *int64 `grove:"updated_by" json:"updated_by,omitempty"`
DeletedAt *time.Time `grove:"deleted_at,soft_delete,nullzero" json:"deleted_at,omitempty"`
DeletedBy *int64 `grove:"deleted_by" json:"deleted_by,omitempty"`
}
AuditModel provides comprehensive audit trail with user tracking Use this when you need to track who created/updated records.
func (*AuditModel) BeforeDelete ¶
func (m *AuditModel) BeforeDelete(ctx context.Context, _ *hook.QueryContext) error
BeforeDelete hook - performs soft delete and tracks deleter.
func (*AuditModel) BeforeInsert ¶
func (m *AuditModel) BeforeInsert(ctx context.Context, _ *hook.QueryContext) error
BeforeInsert hook - sets timestamps and tracks creator.
func (*AuditModel) BeforeUpdate ¶
func (m *AuditModel) BeforeUpdate(ctx context.Context, _ *hook.QueryContext) error
BeforeUpdate hook - updates UpdatedAt and tracks updater.
func (*AuditModel) IsDeleted ¶
func (m *AuditModel) IsDeleted() bool
IsDeleted checks if the record is soft deleted.
type BaseModel ¶
type BaseModel struct {
ID int64 `grove:"id,pk,autoincrement" json:"id"`
CreatedAt time.Time `grove:"created_at,nullzero,notnull,default:current_timestamp" json:"created_at"`
UpdatedAt time.Time `grove:"updated_at,nullzero,notnull,default:current_timestamp" json:"updated_at"`
}
BaseModel provides common fields and hooks for all models Use this for models that need ID, timestamps, and standard hooks.
func (*BaseModel) BeforeInsert ¶
BeforeInsert hook - sets timestamps on insert.
func (*BaseModel) BeforeUpdate ¶
BeforeUpdate hook - updates UpdatedAt on every update.
type SoftDeleteModel ¶
type SoftDeleteModel struct {
ID int64 `grove:"id,pk,autoincrement" json:"id"`
CreatedAt time.Time `grove:"created_at,nullzero,notnull,default:current_timestamp" json:"created_at"`
UpdatedAt time.Time `grove:"updated_at,nullzero,notnull,default:current_timestamp" json:"updated_at"`
DeletedAt *time.Time `grove:"deleted_at,soft_delete,nullzero" json:"deleted_at,omitempty"`
}
SoftDeleteModel provides soft delete functionality with timestamps Soft-deleted records are not permanently removed, just marked as deleted.
func (*SoftDeleteModel) BeforeDelete ¶
func (m *SoftDeleteModel) BeforeDelete(ctx context.Context, _ *hook.QueryContext) error
BeforeDelete hook - performs soft delete.
func (*SoftDeleteModel) BeforeInsert ¶
func (m *SoftDeleteModel) BeforeInsert(ctx context.Context, _ *hook.QueryContext) error
BeforeInsert hook - sets timestamps.
func (*SoftDeleteModel) BeforeUpdate ¶
func (m *SoftDeleteModel) BeforeUpdate(ctx context.Context, _ *hook.QueryContext) error
BeforeUpdate hook - updates UpdatedAt.
func (*SoftDeleteModel) IsDeleted ¶
func (m *SoftDeleteModel) IsDeleted() bool
IsDeleted checks if the record is soft deleted.
func (*SoftDeleteModel) Restore ¶
func (m *SoftDeleteModel) Restore()
Restore restores a soft-deleted record.
type TimestampModel ¶
type TimestampModel struct {
CreatedAt time.Time `grove:"created_at,nullzero,notnull,default:current_timestamp" json:"created_at"`
UpdatedAt time.Time `grove:"updated_at,nullzero,notnull,default:current_timestamp" json:"updated_at"`
}
TimestampModel provides only timestamp fields without ID Use this when you want to add your own ID field type.
func (*TimestampModel) BeforeInsert ¶
func (m *TimestampModel) BeforeInsert(ctx context.Context, _ *hook.QueryContext) error
BeforeInsert hook - sets timestamps.
func (*TimestampModel) BeforeUpdate ¶
func (m *TimestampModel) BeforeUpdate(ctx context.Context, _ *hook.QueryContext) error
BeforeUpdate hook - updates UpdatedAt.
type UUIDModel ¶
type UUIDModel struct {
ID uuid.UUID `grove:"id,pk,type:uuid,default:gen_random_uuid()" json:"id"`
CreatedAt time.Time `grove:"created_at,nullzero,notnull,default:current_timestamp" json:"created_at"`
UpdatedAt time.Time `grove:"updated_at,nullzero,notnull,default:current_timestamp" json:"updated_at"`
}
UUIDModel provides UUID-based primary key with timestamps Use this for distributed systems or when you need globally unique IDs.
func (*UUIDModel) BeforeInsert ¶
BeforeInsert hook - generates UUID and sets timestamps.
func (*UUIDModel) BeforeUpdate ¶
BeforeUpdate hook - updates UpdatedAt.
type UUIDSoftDeleteModel ¶
type UUIDSoftDeleteModel struct {
ID uuid.UUID `grove:"id,pk,type:uuid,default:gen_random_uuid()" json:"id"`
CreatedAt time.Time `grove:"created_at,nullzero,notnull,default:current_timestamp" json:"created_at"`
UpdatedAt time.Time `grove:"updated_at,nullzero,notnull,default:current_timestamp" json:"updated_at"`
DeletedAt *time.Time `grove:"deleted_at,soft_delete,nullzero" json:"deleted_at,omitempty"`
}
UUIDSoftDeleteModel combines UUID primary key with soft delete.
func (*UUIDSoftDeleteModel) BeforeDelete ¶
func (m *UUIDSoftDeleteModel) BeforeDelete(ctx context.Context, _ *hook.QueryContext) error
BeforeDelete hook - performs soft delete.
func (*UUIDSoftDeleteModel) BeforeInsert ¶
func (m *UUIDSoftDeleteModel) BeforeInsert(ctx context.Context, _ *hook.QueryContext) error
BeforeInsert hook - generates UUID and sets timestamps.
func (*UUIDSoftDeleteModel) BeforeUpdate ¶
func (m *UUIDSoftDeleteModel) BeforeUpdate(ctx context.Context, _ *hook.QueryContext) error
BeforeUpdate hook - updates UpdatedAt.
func (*UUIDSoftDeleteModel) IsDeleted ¶
func (m *UUIDSoftDeleteModel) IsDeleted() bool
IsDeleted checks if the record is soft deleted.
func (*UUIDSoftDeleteModel) Restore ¶
func (m *UUIDSoftDeleteModel) Restore()
Restore restores a soft-deleted record.
type XIDAuditModel ¶
type XIDAuditModel struct {
ID xid.ID `grove:"id,pk,type:varchar(20)" json:"id"`
CreatedAt time.Time `grove:"created_at,nullzero,notnull,default:current_timestamp" json:"created_at"`
CreatedBy *xid.ID `grove:"created_by,type:varchar(20)" json:"created_by,omitempty"`
UpdatedAt time.Time `grove:"updated_at,nullzero,notnull,default:current_timestamp" json:"updated_at"`
UpdatedBy *xid.ID `grove:"updated_by,type:varchar(20)" json:"updated_by,omitempty"`
DeletedAt *time.Time `grove:"deleted_at,soft_delete,nullzero" json:"deleted_at,omitempty"`
DeletedBy *xid.ID `grove:"deleted_by,type:varchar(20)" json:"deleted_by,omitempty"`
}
XIDAuditModel provides comprehensive audit trail with XID primary key and user tracking Combines XID with full audit capabilities: tracks who created/updated/deleted records.
func (*XIDAuditModel) BeforeDelete ¶
func (m *XIDAuditModel) BeforeDelete(ctx context.Context, _ *hook.QueryContext) error
BeforeDelete hook - performs soft delete and tracks deleter.
func (*XIDAuditModel) BeforeInsert ¶
func (m *XIDAuditModel) BeforeInsert(ctx context.Context, _ *hook.QueryContext) error
BeforeInsert hook - generates XID, sets timestamps and tracks creator.
func (*XIDAuditModel) BeforeUpdate ¶
func (m *XIDAuditModel) BeforeUpdate(ctx context.Context, _ *hook.QueryContext) error
BeforeUpdate hook - updates UpdatedAt and tracks updater.
func (*XIDAuditModel) IsDeleted ¶
func (m *XIDAuditModel) IsDeleted() bool
IsDeleted checks if the record is soft deleted.
func (*XIDAuditModel) Restore ¶
func (m *XIDAuditModel) Restore()
Restore restores a soft-deleted record.
type XIDModel ¶
type XIDModel struct {
ID xid.ID `grove:"id,pk,type:varchar(20)" json:"id"`
CreatedAt time.Time `grove:"created_at,nullzero,notnull,default:current_timestamp" json:"created_at"`
UpdatedAt time.Time `grove:"updated_at,nullzero,notnull,default:current_timestamp" json:"updated_at"`
}
XIDModel provides XID primary key with timestamps XID is a globally unique, sortable, compact, URL-safe identifier It's shorter than UUID (20 bytes vs 36) and sortable by creation time.
func (*XIDModel) BeforeInsert ¶
BeforeInsert hook - generates XID and sets timestamps.
func (*XIDModel) BeforeUpdate ¶
BeforeUpdate hook - updates UpdatedAt.
type XIDSoftDeleteModel ¶
type XIDSoftDeleteModel struct {
ID xid.ID `grove:"id,pk,type:varchar(20)" json:"id"`
CreatedAt time.Time `grove:"created_at,nullzero,notnull,default:current_timestamp" json:"created_at"`
UpdatedAt time.Time `grove:"updated_at,nullzero,notnull,default:current_timestamp" json:"updated_at"`
DeletedAt *time.Time `grove:"deleted_at,soft_delete,nullzero" json:"deleted_at,omitempty"`
}
XIDSoftDeleteModel combines XID primary key with soft delete.
func (*XIDSoftDeleteModel) BeforeDelete ¶
func (m *XIDSoftDeleteModel) BeforeDelete(ctx context.Context, _ *hook.QueryContext) error
BeforeDelete hook - performs soft delete.
func (*XIDSoftDeleteModel) BeforeInsert ¶
func (m *XIDSoftDeleteModel) BeforeInsert(ctx context.Context, _ *hook.QueryContext) error
BeforeInsert hook - generates XID and sets timestamps.
func (*XIDSoftDeleteModel) BeforeUpdate ¶
func (m *XIDSoftDeleteModel) BeforeUpdate(ctx context.Context, _ *hook.QueryContext) error
BeforeUpdate hook - updates UpdatedAt.
func (*XIDSoftDeleteModel) IsDeleted ¶
func (m *XIDSoftDeleteModel) IsDeleted() bool
IsDeleted checks if the record is soft deleted.
func (*XIDSoftDeleteModel) Restore ¶
func (m *XIDSoftDeleteModel) Restore()
Restore restores a soft-deleted record.