v2
github.com/vcraescu/gorm-history/v2
go get github.com/vcraescu/gorm-history/v2
v2

github.com/vcraescu/gorm-history/v2

GORM History

by Viorel Craescu

v2.0.0 (see all)License:MIT
go get github.com/vcraescu/gorm-history/v2
Readme

gorm history Go Report Card Build Status Coverage Status License: 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()):
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)
    }
}
  1. Your model must implement history.Recordable interface:
func (Person) CreateHistory() interface{} {
    return PersonHistory{}
}
  1. Changes after calling Create, Save and Update will be recorded as long as you pass in the original object.
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.

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:

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:

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.

GitHub Stars

11

LAST COMMIT

2yrs ago

MAINTAINERS

0

CONTRIBUTORS

2

OPEN ISSUES

0

OPEN PRs

0
VersionTagPublished
v2.0.0
1yr ago
No alternatives found
No tutorials found
Add a tutorial