mysql

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const InitialVersion uint32 = 1

InitialVersion 默认的乐观锁版本号起点。

Variables

View Source
var ErrDuplicate = errors.New("duplicate record")

ErrDuplicate signals a DB-level uniqueness/duplicate-entry violation.

Functions

func IsDuplicateError

func IsDuplicateError(err error) bool

IsDuplicateError attempts to detect whether the provided error is caused by a unique-constraint / duplicate-entry violation. It is driver-aware but also falls back to message substring checks for SQLite and unknown drivers.

func NewDuplicateToTranslator

func NewDuplicateToTranslator(mapper func(error) error) func(error) error

NewDuplicateToTranslator returns a translator function that maps DB-level duplicate errors (detected by IsDuplicateError) into a business-level error produced by mapper. Non-duplicate errors are returned unchanged.

func UserIDFromContext

func UserIDFromContext(ctx context.Context) (meta.ID, bool)

UserIDFromContext extracts the authenticated user id from context when available.

func UserIDOrZero

func UserIDOrZero(ctx context.Context) meta.ID

UserIDOrZero returns the authenticated user id or 0 when not available.

Types

type AuditFields

type AuditFields struct {
	ID        meta.ID   `gorm:"primaryKey;type:bigint unsigned"`
	CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
	UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
	// Use pointer so GORM inserts NULL when no deleted time is set.
	DeletedAt *time.Time `gorm:"column:deleted_at;index"`
	CreatedBy meta.ID    `gorm:"column:created_by;type:bigint unsigned;default:0" json:"created_by"`
	UpdatedBy meta.ID    `gorm:"column:updated_by;type:bigint unsigned;default:0" json:"updated_by"`
	DeletedBy meta.ID    `gorm:"column:deleted_by;type:bigint unsigned;default:0" json:"deleted_by"`
	Version   uint32     `gorm:"column:version;type:int unsigned;not null;default:1;version" json:"version"`
}

AuditFields provides reusable columns for ID and audit timestamps.

func (*AuditFields) GetCreatedAt

func (a *AuditFields) GetCreatedAt() time.Time

func (*AuditFields) GetCreatedBy

func (a *AuditFields) GetCreatedBy() meta.ID

func (*AuditFields) GetDeletedAt

func (a *AuditFields) GetDeletedAt() *time.Time

func (*AuditFields) GetDeletedBy

func (a *AuditFields) GetDeletedBy() meta.ID

func (*AuditFields) GetID

func (a *AuditFields) GetID() meta.ID

func (*AuditFields) GetUpdatedAt

func (a *AuditFields) GetUpdatedAt() time.Time

func (*AuditFields) GetUpdatedBy

func (a *AuditFields) GetUpdatedBy() meta.ID

func (*AuditFields) GetVersion

func (a *AuditFields) GetVersion() uint32

func (*AuditFields) SetCreatedAt

func (a *AuditFields) SetCreatedAt(t time.Time)

func (*AuditFields) SetCreatedBy

func (a *AuditFields) SetCreatedBy(id meta.ID)

func (*AuditFields) SetDeletedAt

func (a *AuditFields) SetDeletedAt(t *time.Time)

func (*AuditFields) SetDeletedBy

func (a *AuditFields) SetDeletedBy(id meta.ID)

func (*AuditFields) SetID

func (a *AuditFields) SetID(id meta.ID)

func (*AuditFields) SetUpdatedAt

func (a *AuditFields) SetUpdatedAt(t time.Time)

func (*AuditFields) SetUpdatedBy

func (a *AuditFields) SetUpdatedBy(id meta.ID)

func (*AuditFields) SetVersion

func (a *AuditFields) SetVersion(v uint32)

type BaseRepository

type BaseRepository[T Syncable] struct {
	// contains filtered or unexported fields
}

BaseRepository provides common CRUD helpers for GORM repositories.

func NewBaseRepository

func NewBaseRepository[T Syncable](db *gorm.DB) BaseRepository[T]

NewBaseRepository constructs a repository wrapper for the provided DB.

func (*BaseRepository[T]) CountWithConditions

func (r *BaseRepository[T]) CountWithConditions(ctx context.Context, model interface{}, conditions map[string]string) (int64, error)

CountWithConditions returns the count for the supplied conditions.

func (*BaseRepository[T]) CreateAndSync

func (r *BaseRepository[T]) CreateAndSync(ctx context.Context, entity T, sync func(T)) error

CreateAndSync persists an entity and lets the caller sync generated fields back.

func (*BaseRepository[T]) DB

func (r *BaseRepository[T]) DB() *gorm.DB

DB exposes the underlying *gorm.DB for advanced usages.

func (*BaseRepository[T]) DeleteByID

func (r *BaseRepository[T]) DeleteByID(ctx context.Context, id uint64) error

DeleteByID removes records by primary key.

func (*BaseRepository[T]) ExistsByField

func (r *BaseRepository[T]) ExistsByField(ctx context.Context, model interface{}, field string, value interface{}) (bool, error)

ExistsByField checks uniqueness constraints against a field value.

func (*BaseRepository[T]) ExistsByID

func (r *BaseRepository[T]) ExistsByID(ctx context.Context, id uint64) (bool, error)

ExistsByID checks if a record exists for the given ID.

func (*BaseRepository[T]) FindByField

func (r *BaseRepository[T]) FindByField(ctx context.Context, model interface{}, field string, value interface{}) error

FindByField loads the first record matching the provided field condition.

func (*BaseRepository[T]) FindByID

func (r *BaseRepository[T]) FindByID(ctx context.Context, id uint64) (T, error)

FindByID retrieves a record by its identifier.

func (*BaseRepository[T]) FindList

func (r *BaseRepository[T]) FindList(ctx context.Context, models interface{}, conditions map[string]string, page, pageSize int) ([]T, error)

FindList queries paginated results while filling the consumer-provided model slice.

func (*BaseRepository[T]) FindWithConditions

func (r *BaseRepository[T]) FindWithConditions(ctx context.Context, conditions map[string]interface{}) ([]T, error)

FindWithConditions loads all matching records from the provided condition map.

func (*BaseRepository[T]) SetErrorTranslator

func (r *BaseRepository[T]) SetErrorTranslator(f func(error) error)

SetErrorTranslator registers a function to translate DB errors into domain/business errors. This allows repositories to map driver-specific messages (unique constraint, duplicate entry) to structured errors.

func (*BaseRepository[T]) UpdateAndSync

func (r *BaseRepository[T]) UpdateAndSync(ctx context.Context, entity T, sync func(T)) error

UpdateAndSync updates an entity and triggers the sync callback.

func (*BaseRepository[T]) WithContext

func (r *BaseRepository[T]) WithContext(ctx context.Context) *gorm.DB

WithContext attaches a context to the DB handle.

type Syncable

type Syncable interface {
	GetID() meta.ID
	GetCreatedAt() time.Time
	GetUpdatedAt() time.Time
	// DeletedAt is nullable; return pointer to allow distinguishing NULL
	GetDeletedAt() *time.Time
	GetCreatedBy() meta.ID
	GetUpdatedBy() meta.ID
	GetDeletedBy() meta.ID
	GetVersion() uint32
	SetID(meta.ID)
	SetCreatedAt(time.Time)
	SetUpdatedAt(time.Time)
	SetDeletedAt(*time.Time)
	SetCreatedBy(meta.ID)
	SetUpdatedBy(meta.ID)
	SetDeletedBy(meta.ID)
	SetVersion(uint32)
}

Syncable aggregates the behaviour required by persistence entities so that repositories can propagate auditing metadata back to domain models.

type UnitOfWork

type UnitOfWork struct {
	// contains filtered or unexported fields
}

UnitOfWork wraps a GORM DB to offer transactional execution helpers.

func NewUnitOfWork

func NewUnitOfWork(db *gorm.DB) *UnitOfWork

NewUnitOfWork constructs a UnitOfWork for the given *gorm.DB.

func (*UnitOfWork) WithinTransaction

func (u *UnitOfWork) WithinTransaction(ctx context.Context, fn func(tx *gorm.DB) error) error

WithinTransaction executes fn inside a database transaction.

Jump to

Keyboard shortcuts

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