Skip to content

linclin/gorm2-loggable

 
 

Repository files navigation

Loggable

Loggable is used to helps tracking changes and history of your GORM models.

It use table named during register in your database and writes to all loggable models when they are changed.

More documentation is available in godoc.

Usage

  1. Register plugin using loggable.Register(db,tablename).
plugin, err := Register(database, "change_logs") // database is a *gorm.DB
if err != nil {
	panic(err)
}
  1. Add (embed) loggable.LoggableModel to your GORM model.
type User struct{
    Id        uint
    CreatedAt time.Time
    // some other stuff...
    
    loggable.LoggableModel
}
  1. Set user to scope
scope.Set(loggable.LoggableUserTag, *loggable.User{"name","id","class"})
  1. Changes after calling Create, Save, Update, Delete will be tracked.

Customization

You may add additional fields to change logs, that should be saved.
First, embed loggable.LoggableModel to your model wrapper or directly to GORM model.

type CreatedByLog struct {
	// Public field will be catches by GORM and will be saved to main table.
	CreatedBy     string
	// Hided field because we do not want to write this to main table,
	// only to change_logs.
	createdByPass string 
	loggable.LoggableModel
}

After that, shadow LoggableModel's Meta() method by writing your realization, that should return structure with your information.

type CreatedByLog struct {
	CreatedBy     string
	createdByPass string 
	loggable.LoggableModel
}

func (m CreatedByLog) Meta() interface{} {
	return struct { // You may define special type for this purposes, here we use unnamed one.
		CreatedBy     string
		CreatedByPass string // CreatedByPass is a public because we want to track this field. 
	}{
		CreatedBy:     m.CreatedBy,
		CreatedByPass: m.createdByPass,
	}
}

Options

LazyUpdate

Option LazyUpdate allows save changes only if they big enough to be saved.
Plugin compares the last saved object and the new one, but ignores changes was made in fields from provided list.

ComputeDiff

Option ComputeDiff allows to only save the changes into the RawDiff field. This options is only relevant during update operations. Only fields tagged with gorm-loggable:"disable" will not be logged. If the struct is extending another struct, the parents field will not be log as well.

e.g.

type Person struct {
	FirstName string `gorm-loggable:"disable"`
	LastName  string 
	Age       int    
}

Let's say you change person FirstName from John to Jack and its Age from 30 to 40. ChangeLog.RawDiff will be populated with the following:

{
  "Age": 40
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%