Skip to content

kisrobot/audited

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Audited

Audited is used to record the last User who created and/or updated your GORM model. It does so using a CreatedBy and UpdatedBy field. While Audited can be used alone (with GORM), it integrates nicely with QOR to log and display this extra information.

GoDoc

Register GORM Callbacks

Audited utilizes GORM callbacks to log data, so you will need to register callbacks first:

import (
  "github.com/jinzhu/gorm"
  "github.com/kisrobot/audited"
)

db, err := gorm.Open("sqlite3", "demo_db")
audited.RegisterCallbacks(db)

Making a Model Auditable

Embed audited.AuditedModel into your model as an anonymous field to make the model auditable:

type Product struct {
  gorm.Model
  Name string
  audited.AuditedModel
}

Usage

import "github.com/kisrobot/audited"
import "github.com/jinzhu/gorm"

func main() {
  var db, err = gorm.Open("sqlite3", "demo_db")
  var currentUser = User{ID: 100}
  var product Product

  // Create will set product's `CreatedBy`, `UpdatedBy` to `currentUser`'s primary key if `audited:current_user` is a valid model
  db.Set("audited:current_user", currentUser).Create(&product)
  // product.CreatedBy => 100
  // product.UpdatedBy => 100

  // If it is not a valid model, then will set `CreatedBy`, `UpdatedBy` to the passed value
  db.Set("audited:current_user", "admin").Create(&product)
  // product.CreatedBy => "admin"
  // product.UpdatedBy => "admin"

  // When updating a record, it will update the `UpdatedBy` to `audited:current_user`'s value
  db.Set("audited:current_user", "dev").Model(&product).Update("Code", "L1212")
  // product.UpdatedBy => "dev"
}

QOR Demo: http://demo.getqor.com/admin

License

Released under the MIT License.

About

Audited is used to log last UpdatedBy and CreatedBy for your models

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%