This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
plugin.go
78 lines (73 loc) · 2.29 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package loggable
import (
"github.com/jinzhu/gorm"
)
// Plugin is a hook for gorm.
type Plugin struct {
db *gorm.DB
opts options
}
// 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 Register(db *gorm.DB, opts ...Option) (Plugin, error) {
err := db.AutoMigrate(&ChangeLog{}).Error
if err != nil {
return Plugin{}, err
}
o := options{}
for _, option := range opts {
option(&o)
}
p := Plugin{db: db, opts: o}
callback := db.Callback()
callback.Query().After("gorm:after_query").Register("loggable:query", p.trackEntity)
callback.Create().After("gorm:after_create").Register("loggable:create", p.addCreated)
callback.Update().After("gorm:after_update").Register("loggable:update", p.addUpdated)
callback.Delete().After("gorm:after_delete").Register("loggable:delete", p.addDeleted)
return p, nil
}
// GetRecords returns all records by objectId.
// Flag prepare allows to decode content of Raw* fields to direct fields, e.g. RawObject to Object.
func (p *Plugin) GetRecords(objectId string, prepare bool) (changes []ChangeLog, err error) {
defer func() {
if prepare {
for i := range changes {
if t, ok := p.opts.metaTypes[changes[i].ObjectType]; ok {
err = changes[i].prepareMeta(t)
if err != nil {
return
}
}
if t, ok := p.opts.objectTypes[changes[i].ObjectType]; ok {
err = changes[i].prepareObject(t)
if err != nil {
return
}
}
}
}
}()
return changes, p.db.Where("object_id = ?", objectId).Find(&changes).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.
func (p *Plugin) GetLastRecord(objectId string, prepare bool) (change ChangeLog, err error) {
defer func() {
if prepare {
if t, ok := p.opts.metaTypes[change.ObjectType]; ok {
err := change.prepareMeta(t)
if err != nil {
return
}
}
if t, ok := p.opts.objectTypes[change.ObjectType]; ok {
err := change.prepareObject(t)
if err != nil {
return
}
}
}
}()
return change, p.db.Where("object_id = ?", objectId).Order("created_at DESC").Limit(1).Find(&change).Error
}