As of December 1st, 2019 Buffalo, and all related packages, require Go Modules and the use of the
GOPATH
is no longer supported.
Please see this blog post for more information https://blog.gobuffalo.io/the-road-to-1-0-requiring-modules-5672c6b015e5.
Callbacks
Pop provides a means to execute code before and after database operations. This is done by defining specific methods for your models.
For example, to hash a user password you may want to define the following method:
type User struct {
ID uuid.UUID
Email string
Password string
}
func (u *User) BeforeCreate(tx *pop.Connection) error {
hash, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost)
if err != nil {
return errors.WithStack(err)
}
u.Password = string(hash)
return nil
}
In the above example, when the connection's Save
method is called with a User
, the BeforeCreate
method
will be called before writing to the database.
The available callbacks include:
- BeforeSave
- BeforeCreate
- BeforeUpdate
- BeforeDestroy
- BeforeValidate
- AfterSave
- AfterCreate
- AfterUpdate
- AfterDestroy
- AfterFind
Related Content
- Models - Define a database model.
Feedback
If you found an error or something which needs to be improved, and you want to contribute to fix it:
If you'd like to suggest something to improve this page: