The purpose of Gorm-Auditable project for solving users wants to know the difference between each change, also who made those changes. This project is inspired by Paper Trail. The difference from QOR audited is QOR audited only records who made the last change but cannot show the difference.
- Be able to record each new version of your tracked database records. (only support insert and update for now)
- Be able to track who did the change.
Add GORM plugin with config:
db.Use(auditable.New(auditable.Config{
CurrentUserIDKey: "current_user_id", // Current User ID Key from echo.Context, which is for plugin to get current operator id.
DB: Conn, // Database Connection
AutoMigrate: true, // Do you need *versions* table to be created automatically?
Tables: []string{ // All the tables you would like to track versions.
"User",
},
}))
In your gorm model, you need to add the auditable to the field, otherwise, it won't be recorded:
type User struct {
gorm.Model
Name string `gorm:"unique;auditable"`
}
If you are using echo framework, there already is a middleware that could inject scoped gorm object into Context
, which is used to set current_user_id
when inserting the Version
record.
e.Use(auditable.GormInjector(Conn))
Please remember, you need to add your authenticate middleware before it.
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// e.g. You need to set the current_user_id before Gorm Injector.
c.Set("current_user_id", "12344321")
return next(c)
}
})
e.Use(auditable.GormInjector(Conn))
conn := c.Get(auditable.GormDBKey).(*gorm.DB)
conn.Create(&User{Name: "Michael He"})
You could run go run example/example.go
to try the example. But make sure to change the database config.