Skip to content

Plugin for GORM checking read data before updating for consistency

License

Notifications You must be signed in to change notification settings

ftomza/consistently

Repository files navigation

Consistently

GoDoc Go Report Card Build Status Coverage Status

Plugin for GORM checking read data before updating for consistency

Register GORM Callbacks

Consistently uses GORM callbacks to handle gorm:update and gorm:create, so you will need to register callbacks first:

import (
  "github.com/jinzhu/gorm"
  "github.com/ftomza/consistently
)

func main() {
  db, err := gorm.Open("sqlite3", "demo_db")

  consistently.RegisterCallbacks(db)
}

Usage

After callbacks are registered, an attempt to create or update any record checks all fields of the structure for the tag consistently:"version". If such a field is found, a new data version value will be assigned when the record is created, if it is updated, the version of the recorded data with the version in the database will be checked.

type User struct {
	gorm.Model
	Name string
	consistently.Consistently
}


func main() {
	
  db, err := gorm.Open("sqlite3", "demo_db")

  consistently.RegisterCallbacks(db)
  
  user := User{Name: "FtomZa"}
  
  db.Save(&user)
  
  fmt.printf("Version: %s\n", user.Version)
  
  user.Name = "FtomZa2"
  
  db.Update(&user)
  
  fmt.printf("Version: %s\n", user.Version)
}

Custom field for save version

If it is not possible to store version data in the version field by default, use the consistently:"version" tag to mark the field as a data version.

type User struct {
	gorm.Model
	Name string
	MyVersion string `consistently:"version"`
}

License

Released under the MIT License.

About

Plugin for GORM checking read data before updating for consistency

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages