models

package module
v0.0.0-...-2314a6b Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetUserID

func GetUserID(ctx context.Context) (int64, bool)

GetUserID retrieves user ID from context.

func GetXIDUserID

func GetXIDUserID(ctx context.Context) (xid.ID, bool)

func SetUserID

func SetUserID(ctx context.Context, userID int64) context.Context

SetUserID is a helper to set user ID in context for audit tracking.

func SetXIDUserID

func SetXIDUserID(ctx context.Context, userID xid.ID) context.Context

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.

func (*AuditModel) Restore

func (m *AuditModel) Restore()

Restore restores a soft-deleted record.

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

func (m *BaseModel) BeforeInsert(ctx context.Context, _ *hook.QueryContext) error

BeforeInsert hook - sets timestamps on insert.

func (*BaseModel) BeforeUpdate

func (m *BaseModel) BeforeUpdate(ctx context.Context, _ *hook.QueryContext) error

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

func (m *UUIDModel) BeforeInsert(ctx context.Context, _ *hook.QueryContext) error

BeforeInsert hook - generates UUID and sets timestamps.

func (*UUIDModel) BeforeUpdate

func (m *UUIDModel) BeforeUpdate(ctx context.Context, _ *hook.QueryContext) error

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

func (m *XIDModel) BeforeInsert(ctx context.Context, _ *hook.QueryContext) error

BeforeInsert hook - generates XID and sets timestamps.

func (*XIDModel) BeforeUpdate

func (m *XIDModel) BeforeUpdate(ctx context.Context, _ *hook.QueryContext) error

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.

Jump to

Keyboard shortcuts

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