loggable

package module
v2.0.1+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2018 License: MIT Imports: 11 Imported by: 0

README

Loggable

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

It creates change_logs table in your database and writes to all loggable models when they are changed.

Usage

  1. Register plugin using loggable.Register(db).
plugin, err := Register(database) // 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. 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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ToSnakeCase = toSomeCase("_")

Functions

func StringMap added in v1.1.1

func StringMap(strs []string, fn func(string) string) []string

func ToLowerFirst added in v1.1.1

func ToLowerFirst(s string) string

Types

type ChangeLog

type ChangeLog struct {
	ID         string    `gorm:"type:uuid;primary_key;"`
	CreatedAt  time.Time `sql:"DEFAULT:current_timestamp"`
	Action     string
	ObjectID   string      `gorm:"index"`
	ObjectType string      `gorm:"index"`
	RawObject  JSONB       `sql:"type:JSONB"`
	RawMeta    JSONB       `sql:"type:JSONB"`
	Object     interface{} `sql:"-"`
	Meta       interface{} `sql:"-"`
}

ChangeLog is a main entity, which used to log changes.

type Interface added in v1.1.1

type Interface interface {
	// Meta should return structure, that can be converted to json.
	Meta() interface{}
	// contains filtered or unexported methods
}

Interface is used to get metadata from your models.

type JSONB

type JSONB []byte

func (JSONB) Equals

func (j JSONB) Equals(j1 JSONB) bool

func (JSONB) IsNull

func (j JSONB) IsNull() bool

func (JSONB) MarshalJSON

func (j JSONB) MarshalJSON() ([]byte, error)

func (*JSONB) Scan

func (j *JSONB) Scan(value interface{}) error

func (*JSONB) UnmarshalJSON

func (j *JSONB) UnmarshalJSON(data []byte) error

func (JSONB) Value

func (j JSONB) Value() (driver.Value, error)

type LoggableModel

type LoggableModel struct{}

LoggableModel is a root structure, which implement Interface. Embed LoggableModel to your model so that Plugin starts tracking changes.

func (LoggableModel) Meta added in v1.1.1

func (LoggableModel) Meta() interface{}

type Option added in v1.1.1

type Option func(options *options)

func LazyUpdate added in v1.1.1

func LazyUpdate(fields ...string) Option

func RegMetaType added in v1.1.1

func RegMetaType(objectType string, metaType interface{}) Option

func RegObjectType added in v1.1.1

func RegObjectType(objectType string, objectStruct interface{}) Option

type Plugin added in v1.1.1

type Plugin struct {
	// contains filtered or unexported fields
}

func Register

func Register(db *gorm.DB, opts ...Option) (Plugin, error)

func (*Plugin) GetLastRecord added in v1.1.1

func (p *Plugin) GetLastRecord(objectId string, prepare bool) (change ChangeLog, err error)

func (*Plugin) GetRecords added in v1.1.1

func (p *Plugin) GetRecords(objectId string, prepare bool) (changes []ChangeLog, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL