https://github.com/vcraescu/gorm-history
GORM History
https://github.com/vcraescu/gorm-history
golang gorm gorm-orm plugin
Last synced: about 1 year ago
JSON representation
GORM History
- Host: GitHub
- URL: https://github.com/vcraescu/gorm-history
- Owner: vcraescu
- License: mit
- Created: 2020-03-01T14:53:06.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-24T06:40:59.000Z (about 3 years ago)
- Last Synced: 2025-04-01T05:51:05.431Z (over 1 year ago)
- Topics: golang, gorm, gorm-orm, plugin
- Language: Go
- Size: 19.5 KB
- Stars: 14
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: history.go
- License: LICENSE
Awesome Lists containing this project
README
# gorm history [](https://goreportcard.com/report/github.com/vcraescu/gorm-history) [](https://travis-ci.com/vcraescu/gorm-history) [](https://coveralls.io/github/vcraescu/gorm-history?branch=master) [](https://opensource.org/licenses/MIT)
You can use the plugin to keep a history of your GORM models changes. Basically it keeps a ledger of your model state.
Each model must be associated with a history model which will be a copy of the modified record.
## Install
```
go get github.com/vcraescu/gorm-history
```
## Usage
1. Register the plugin using `db.Use(history.New())`:
```go
type Person struct {
gorm.Model
FirstName string
LastName string
}
type PersonHistory struct {
gorm.Model
Entry
}
func main() {
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
panic(err)
}
db = db.Session(&gorm.Session{})
err = db.AutoMigrate(Person{}, PersonHistory{})
if err != nil {
panic(err)
}
plugin := New()
if err := db.Use(plugin); err != nil {
return
}
db = SetUser(db, history.User{
ID: 123,
Email: "john@doe.com",
})
db = SetSource(db, history.Source{
ID: "1c059a03-3b14-4017-ae33-5337860ec35f",
Type: "ampq",
})
p := Person{
FirstName: "John",
LastName: "Doe",
}
if err := db.Save(&p).Error; err != nil {
panic(err)
}
}
```
2. Your model must implement `history.Recordable` interface:
```go
func (Person) CreateHistory() interface{} {
return PersonHistory{}
}
```
3. Changes after calling Create, Save and Update will be recorded as long as you pass in the original object.
```go
if err := db.Model(&p).Update("first_name", "Jane").Error; err != nil {
panic(err)
}
```
## Configuration
### Versioning
By default, the plugin generates a new ULID for each history record. You can implement your own versioning function.
```go
type PersonHistory struct {
gorm.Model
history.Entry
}
// or
type PersonHistory struct {
gorm.Model
Version Version `gorm-history:"version"`
ObjectID uint `gorm:"index" gorm-history:"objectID"`
Action Action `gorm:"type: string" gorm-history:"action"`
}
```
You can change the versioning function when you register the plugin:
```go
if err := db.Use(history.New(history.WithVersionFunc(MyVersionFunc))); err != nil {
panic(err)
}
```
### Copying
* `history.DefaultCopyFunc` - copies all the values of the recordable model to history model.
You can change the copy function when you register the plugin if you defined your own copying function:
```go
func myCopyFunc(r Recordable, history interface{}) error {
// ...
}
//...
if err := db.Use(history.New(history.WithCopyFunc(myCopyFunc))); err != nil {
panic(err)
}
```
## License
gorm-history is licensed under the [MIT License](LICENSE).