Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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.
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.