callback

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ParseError       = errors.New("error parsing model")
	NilDatabaseErr   = errors.New("nil db provided")
	InvalidNameErr   = errors.New("invalid callback name: empty string")
	NilCallbackErr   = errors.New("invalid callback: nil function provided")
	TypeNotStructErr = errors.New("invalid type: T must be a struct type")
)

Functions

func RegisterCallback

func RegisterCallback[M Model, T OnCreate | OnUpdate | OnDelete](
	db *gorm.DB,
	callbackName string,
	callback func(M),
) error

RegisterCallback registers a callback function to be called on specific database events. `trigger` can be OnCreate, OnUpdate or OnDelete. The model will be passed to the callback function.

In case the database mutation was passed a slice or an array of T, then the callback function will be called N times, where N is the number of models.

T must be a struct.

`callbackName` must be unique.

Will generate an error if a callback with the same name and the same trigger type exists.

Technical limitations

  • Gorm does not invoke the OnUpdate callback when saving models using save and an array of models. Use Save, or Updates, for every item to update.

func RemoveCallback

func RemoveCallback[T OnCreate | OnUpdate | OnDelete](db *gorm.DB, callbackName string) error

Types

type Model

type Model interface {
	// implements gorm Tabler interface
	// gorm offers no ref to interface unfortunately
	TableName() string
}

Model should be implemented by a struct.

type OnCreate

type OnCreate struct{}

OnCreate is used to specify that the callback should be invoked when an insert operation is done on a model. Bulk inserts are supported.

type OnDelete

type OnDelete struct{}

OnDelete is used to specify that the callback should be invoked when a delete operation is done on a model. Bulk deletions are supported.

type OnUpdate

type OnUpdate struct{}

OnUpdate is used to specify that the callback should be invoked when an update operation is done on a model. Bulk updates will not invoke the callback when registering with OnUpdate. This is a limitation from GORM.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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