optimistic

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2025 License: MIT Imports: 13 Imported by: 0

README

Another optimistic locking plugin for GORM

Provides a Version type alias to uint64 with useful helper functions for enabling optimistic locking when using GORM.

This package also provides a gorm:after_update callback to ensure updates are handled appropriately. If not, ErrOptimisticLock is set to the db.Error.

Effort has been taken to ensure the logic is sound but I make no claims that this code is perfect and free from bugs. If you find any issues please raise a PR.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrOptimisticLock = errors.New("db record version mismatch")

Functions

func ExtractField

func ExtractField[T any, F any](slice []T, fieldPath string) ([]F, error)

ExtractField extracts the values of a specified field (including nested fields via "dot" notation) from a slice of structs or pointers to structs. It returns a slice of the specified field type.

func NewOptimisticLock

func NewOptimisticLock(opts ...Option) gorm.Plugin

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

func Disabled

func Disabled() Option

Disabled disables optimstic locking

func VersionFieldName

func VersionFieldName(versionFieldName string) Option

VersionFieldName sets the version field name in the options configuration using the provided string value.

Default value: `Version`

type Version

type Version uint64

func (*Version) CreateClauses

func (v *Version) CreateClauses(field *schema.Field) []clause.Interface

func (Version) Equal

func (v Version) Equal(val interface{}) bool

func (Version) MarshalJSON

func (v Version) MarshalJSON() ([]byte, error)

func (*Version) UnmarshalJSON

func (v *Version) UnmarshalJSON(bytes []byte) error

func (*Version) UpdateClauses

func (v *Version) UpdateClauses(field *schema.Field) []clause.Interface

func (Version) Value

func (v Version) Value() uint64

type VersionCreateClause

type VersionCreateClause struct {
	Field *schema.Field
}

func (VersionCreateClause) Build

func (VersionCreateClause) MergeClause

func (v VersionCreateClause) MergeClause(*clause.Clause)

func (VersionCreateClause) ModifyStatement

func (v VersionCreateClause) ModifyStatement(stmt *gorm.Statement)

func (VersionCreateClause) Name

func (v VersionCreateClause) Name() string

type VersionUpdateClause

type VersionUpdateClause struct {
	Field *schema.Field
}

func (VersionUpdateClause) Build

func (VersionUpdateClause) MergeClause

func (v VersionUpdateClause) MergeClause(*clause.Clause)

func (VersionUpdateClause) ModifyStatement

func (v VersionUpdateClause) ModifyStatement(stmt *gorm.Statement)

func (VersionUpdateClause) Name

func (v VersionUpdateClause) Name() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳