Skip to content

a631807682/zerofield

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)

About

gorm plugin for allow update zero value field

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages