Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ToSnakeCase = toSomeCase("_")
Functions ¶
func ToLowerFirst ¶
Types ¶
type ChangeLog ¶
type ChangeLog struct { // Primary key of change logs. ID uuid.UUID `gorm:"primary_key;"` // Timestamp, when change log was created. CreatedAt time.Time `sql:"DEFAULT:current_timestamp"` // Action type. // On write, supports only 'create', 'update', 'delete', // but on read can be anything. Action string // ID of tracking object. // By this ID later you can find all object (database row) changes. ObjectID string `gorm:"index"` // Reflect name of tracking object. // It does not use package or module name, so // it may be not unique when use multiple types from different packages but with the same name. ObjectType string `gorm:"index"` // Raw representation of tracking object. // todo(@sas1024): Replace with []byte, to reduce allocations. Would be major version. RawObject string `sql:"type:JSON"` // Raw representation of tracking object's meta. // todo(@sas1024): Replace with []byte, to reduce allocations. Would be major version. RawMeta string `sql:"type:JSON"` // Raw representation of diff's. // todo(@sas1024): Replace with []byte, to reduce allocations. Would be major version. RawDiff string `sql:"type:JSON"` // Free field to store something you want, e.g. who creates change log. // Not used field in gorm-loggable, but gorm tracks this field. CreatedBy string `gorm:"index"` // Field Object would contain prepared structure, parsed from RawObject as json. // Use RegObjectType to register object types. Object interface{} `sql:"-"` // Field Meta would contain prepared structure, parsed from RawMeta as json. // Use RegMetaType to register object's meta types. Meta interface{} `sql:"-"` }
ChangeLog is a main entity, which used to log changes. Commonly, ChangeLog is stored in 'change_logs' table.
func (ChangeLog) Diff ¶
func (l ChangeLog) Diff() (UpdateDiff, error)
Diff returns parsed to map[string]interface{} diff representation from field RawDiff. To unmarshal diff to own structure, manually use field RawDiff.
type Interface ¶
type Interface interface { // Meta should return structure, that can be converted to json. Meta() interface{} // enable/disable loggable Enable(v bool) // contains filtered or unexported methods }
Interface is used to get metadata from your models.
type LoggableModel ¶
type LoggableModel struct {
Disabled bool `sql:"-" json:"-"`
}
LoggableModel is a root structure, which implement Interface. Embed LoggableModel to your model so that Plugin starts tracking changes.
func (LoggableModel) Enable ¶
func (l LoggableModel) Enable(v bool)
func (LoggableModel) Meta ¶
func (LoggableModel) Meta() interface{}
type Option ¶
type Option func(options *options)
Option is a generic options pattern.
func ComputeDiff ¶
func ComputeDiff() Option
Option ComputeDiff allows you also write differences between objects on update operations. ComputeDiff not reads records from db, it used only as cache on plugin side. So it does not track changes outside plugin.
func LazyUpdate ¶
Option LazyUpdate allows you to skip update operations when nothing was changed. Parameter 'fields' is list of sql field names that should be ignored on updates.
func RegMetaType ¶
RegMetaType works like RegObjectType, but for field RawMeta. RegMetaType maps object to type name, that is used in field Type of ChangeLog struct. On read change log operations, if plugin finds registered object type, by its name from db, it unmarshal field RawMeta to Meta field via json.Unmarshal.
To access decoded object, e.g. `MyClientMeta`, use type casting: `changeLog.Meta.(MyClientMeta)`.
func RegObjectType ¶
RegObjectType maps object to type name, that is used in field Type of ChangeLog struct. On read change log operations, if plugin finds registered object type, by its name from db, it unmarshal field RawObject to Object field via json.Unmarshal.
To access decoded object, e.g. `ReallyFunnyClient`, use type casting: `changeLog.Object.(ReallyFunnyClient)`.
type Plugin ¶
type Plugin struct {
// contains filtered or unexported fields
}
Plugin is a hook for gorm.
func Register ¶
Register initializes Plugin for provided gorm.DB. There is also available some options, that should be passed there. Options cannot be set after initialization.
func (*Plugin) GetLastRecord ¶
GetLastRecord returns last by creation time (CreatedAt field) change log by provided object id. Flag prepare allows to decode content of Raw* fields to direct fields, e.g. RawObject to Object.
type UpdateDiff ¶
type UpdateDiff map[string]interface{}