gorm-shadow
is a plugin for GORM that allows you to shadow changes to a model's fields.
go get github.com/EelisK/gorm-shadow
To use gorm-shadow
, you need to import it and register it with your GORM instance.
Example:
package main
import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"github.com/EelisK/gorm-shadow"
)
func main() {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
})
if err != nil {
panic("failed to connect database")
}
db.Use(gormshadow.Plugin{})
// Your code here
}
After registering the plugin, all the models that specify the ShadowTable
function receiver will have their changes shadowed.
Example:
package main
import (
"gorm.io/gorm"
"github.com/EelisK/gorm-shadow"
)
type User struct {
gorm.Model
Name string
}
func (u User) ShadowTable() string {
return "shadow_users"
}
func main() {
// Your code here
}
To automatically query the shadow table when querying the main table, you can provide a TimeMachine
implementation to the plugin.
Example:
type TimeMachine struct {}
func (t *TimeMachine) GetTime(ctx context.Context) time.Time {
return ctx.Value("time").(time.Time)
}
func main() {
db.Use(gormshadow.Plugin{
TimeMachine: &TimeMachine{},
})
// Your code here
}