Code Monkey home page Code Monkey logo

gorm2-loggable's Introduction

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
}

gorm2-loggable's People

Contributors

sas1024 avatar libertusdio avatar vetcher avatar vcraescu avatar sonnt0309 avatar pkuebler avatar radutopala avatar shelnutt2 avatar colornote avatar andrewsafonov avatar

Stargazers

zbv avatar toni dy avatar

Watchers

James Cloos avatar  avatar

Forkers

isgasho linclin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.