consistently

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2019 License: MIT Imports: 4 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrVersionNotValid Update value not valid version current value
	ErrVersionNotValid = errors.New("version not valid")
)

Functions

func RegisterCallbacks

func RegisterCallbacks(db *gorm.DB)

RegisterCallbacks register callback into GORM DB

Types

type Consistently

type Consistently struct {
	Version string `consistently:"version"`
}

Consistently is a struct, embed it in your struct to enable check version for the struct

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL