diff --git a/README.md b/README.md index f2e3159..9f104b5 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,4 @@ It creates `change_logs` table in your database and writes to all loggable model ## Usage 1. Register plugin using `loggable.Register(db)` 2. Add `loggable.LoggableModel` to your GORM model - 3. If you want to set user, who makes changes, use `loggable.SetUser("username")`. + 3. If you want to set user, who makes changes, and place, where it happened, use `loggable.SetUserAndFrom("username", "London")`. diff --git a/callbacks.go b/callbacks.go index 3eff39a..52e7dd3 100644 --- a/callbacks.go +++ b/callbacks.go @@ -10,8 +10,11 @@ import ( ) type LoggablePlugin interface { + // Deprecated: Use SetUserAndWhere instead. SetUser(user string) *gorm.DB - SetFrom(from string) *gorm.DB + // Deprecated: Use SetUserAndWhere instead. + SetWhere(from string) *gorm.DB + SetUserAndWhere(user, where string) *gorm.DB GetRecords(objectId string) ([]*ChangeLog, error) } @@ -42,6 +45,7 @@ func (r *loggablePlugin) GetRecords(objectId string) ([]*ChangeLog, error) { return changes, nil } +// Deprecated: Use SetUserAndWhere instead. func (r *loggablePlugin) SetUser(user string) *gorm.DB { r.mu.Lock() db := r.db.Set("loggable:user", user) @@ -49,13 +53,20 @@ func (r *loggablePlugin) SetUser(user string) *gorm.DB { return db } -func (r *loggablePlugin) SetFrom(from string) *gorm.DB { +// Deprecated: Use SetUserAndWhere instead. +func (r *loggablePlugin) SetWhere(where string) *gorm.DB { r.mu.Lock() - db := r.db.Set("loggable:from", from) + db := r.db.Set("loggable:where", where) r.mu.Unlock() return db } +func (r *loggablePlugin) SetUserAndWhere(user, where string) *gorm.DB { + r.mu.Lock() + defer r.mu.Unlock() + return r.db.Set("loggable:user", user).Set("loggable:where", where) +} + func (r *loggablePlugin) addRecord(scope *gorm.Scope, action string) error { var jsonObject JSONB j, err := json.Marshal(scope.Value) @@ -70,19 +81,19 @@ func (r *loggablePlugin) addRecord(scope *gorm.Scope, action string) error { if !ok { user = "" } - from, ok := scope.DB().Get("loggable:from") + where, ok := scope.DB().Get("loggable:where") if !ok { - from = "" + where = "" } cl := ChangeLog{ - ID: uuid.NewV4().String(), - ChangedBy: user.(string), - ChangedFrom: from.(string), - Action: action, - ObjectID: scope.PrimaryKeyValue().(string), - ObjectType: scope.GetModelStruct().ModelType.Name(), - Object: jsonObject, + ID: uuid.NewV4().String(), + ChangedBy: user.(string), + ChangedWhere: where.(string), + Action: action, + ObjectID: scope.PrimaryKeyValue().(string), + ObjectType: scope.GetModelStruct().ModelType.Name(), + Object: jsonObject, } err = scope.DB().Create(&cl).Error if err != nil { diff --git a/loggable.go b/loggable.go index 93acb19..33d7a3d 100644 --- a/loggable.go +++ b/loggable.go @@ -8,14 +8,14 @@ import ( ) type ChangeLog struct { - ID string `gorm:"type:uuid;primary_key;"` - CreatedAt time.Time `sql:"DEFAULT:current_timestamp"` - ChangedBy string `gorm:"index"` - ChangedFrom string `gorm:"index"` - Action string - ObjectID string `gorm:"index"` - ObjectType string `gorm:"index"` - Object JSONB `sql:"type:JSONB"` + ID string `gorm:"type:uuid;primary_key;"` + CreatedAt time.Time `sql:"DEFAULT:current_timestamp"` + ChangedBy string `gorm:"index"` + ChangedWhere string `gorm:"index"` + Action string + ObjectID string `gorm:"index"` + ObjectType string `gorm:"index"` + Object JSONB `sql:"type:JSONB"` } type loggableInterface interface {