forked from sas1024/gorm-loggable
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.go
104 lines (94 loc) · 2.88 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package loggable
import (
"context"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
)
// Plugin is a hook for gorm.
type Plugin struct {
db *gorm.DB
opts options
Context Context
userKey string
}
type Context interface {
Value(interface{}) interface{}
}
// RegisterContext validates and adds a context to the plugin
func (p *Plugin) RegisterContext(ctx Context) error {
_, ok := ctx.Value(p.userKey).(string)
if !ok {
return errors.Errorf("missing %s on context", p.userKey)
}
p.Context = ctx
return nil
}
// defaultContext default context
func defaultContext() Context {
return context.WithValue(context.Background(), "user", "")
}
var defaultUserKey = "user"
// 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 nil, err
}*/
o := options{}
for _, option := range opts {
option(&o)
}
p := Plugin{db: db, opts: o, Context: defaultContext(), userKey: defaultUserKey}
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
}