zerofield

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2022 License: MIT Imports: 3 Imported by: 3

README

zerofield

gorm plugin for allow update zero value field.

go report card test status MIT license Go.Dev reference visitor badge

Desc

When update with struct, GORM will only update non-zero fields, you might want to use map to update attributes or use Select to specify fields to update Updates-multiple-columns

This works in most cases, but there are times when we just want to allow individual 0 values to be updated, and neither map[string]interface nor Select is very friendly to us.

Usage

  1. NewPlugin register plugin to gorm.DB

        db.Use(zerofield.NewPlugin())
    
  2. UpdateScopes update event it's zero field

    
        // ...
        user.Name = ""
        user.Age = 0
        user.Active = false
        user.Birthday = nil
    
        // will always update Name,Age even if it's zero field
        // Active,Birthday will not be saved
        db.Scopes(zerofield.UpdateScopes("Name","Age")).Updates(&user)
        // if cloumns is empty, all field will be save like db.Select("*"")
        db.Scopes(zerofield.UpdateScopes()).Updates(&user)
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPlugin

func NewPlugin() *plugins.ZeroFieldPlugin

NewPlugin gorm plugin for zero value field

func UpdateScopes

func UpdateScopes(cloumns ...string) func(db *gorm.DB) *gorm.DB

UpdateZeroFields allow update zero cloumns which specified. just work for db.Updates(&model) and db.Save(&model). if cloumns is empty, all field will be save like db.Select("*"")

Types

This section is empty.

Directories

Path Synopsis
tests module

Jump to

Keyboard shortcuts

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