Documentation ¶
Index ¶
- Variables
- func StringMap(strs []string, fn func(string) string) []string
- func ToLowerFirst(s string) string
- type ChangeLog
- type Changelogs
- type Interface
- type LoggableModel
- type Option
- type Plugin
- func (p *Plugin) GetLastRecord(objectId, objectType string, prepare bool) (change ChangeLog, err error)
- func (p *Plugin) GetRecords(objectId, objectType string, prepare bool) (changes []ChangeLog, err error)
- func (p *Plugin) GetRecordsBySecondaryIndex(objectId, objectType string, prepare bool) (changes []ChangeLog, err error)
- type UpdateDiff
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;" json:"id"` // Timestamp, when change log was created. CreatedAt time.Time `sql:"DEFAULT:current_timestamp" json:"created_at"` // Action type. // On write, supports only 'create', 'update', 'delete', // but on read can be anything. Action string `json:"action"` // ID of tracking object. // By this ID later you can find all object (database row) changes. ObjectID string `gorm:"index" json:"object_id"` ObjectID2 string `gorm:"index" json:"object_id2"` // 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" json:"object_type"` // Raw representation of tracking object. // todo(@sas1024): Replace with []byte, to reduce allocations. Would be major version. RawObject string `sql:"type:JSON" json:"raw_object"` // Raw representation of tracking object's meta. // todo(@sas1024): Replace with []byte, to reduce allocations. Would be major version. RawMeta string `sql:"type:JSON" json:"raw_meta"` // Raw representation of diff's. // todo(@sas1024): Replace with []byte, to reduce allocations. Would be major version. RawDiff string `sql:"type:JSON" json:"raw_diff"` // 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" json:"created_by"` // Field Object would contain prepared structure, parsed from RawObject as json. // Use RegObjectType to register object types. Object interface{} `sql:"-" json:"object"` // Field Meta would contain prepared structure, parsed from RawMeta as json. // Use RegMetaType to register object's meta types. Meta interface{} `sql:"-" json:"meta"` }
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 Changelogs ¶
type Changelogs []ChangeLog
type Interface ¶
type Interface interface { // Meta should return structure, that can be converted to json. Meta() interface{} // enable/disable loggable Enable(v bool) // SecondaryIndexValue() string // contains filtered or unexported methods }
Interface is used to get metadata from your models.
type LoggableModel ¶
type LoggableModel struct { Disabled bool `sql:"-" json:"-"` SecondaryIndex string `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{}
func (LoggableModel) SecondaryIndexValue ¶
func (l LoggableModel) SecondaryIndexValue() string
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)`.
func RegObjectTypes ¶
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 ¶
func (p *Plugin) GetLastRecord(objectId, objectType string, prepare bool) (change ChangeLog, err error)
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{}