Fork from and fully compatible with: jinzhu/gorm v1.9.16
go get github.com/Laisky/gorm v1.9.18
- support unique-index with soft-delete
- support filter specified stack by regexp in log
- support ignore specified SQL log by regexp
- generate SQL condition by tag
- Add
DeletedFlag
in model struct
(or embeded gorm.ModelSupportUnique
)
type Model struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
DeletedFlag uint `gorm:"type:INT UNSIGNED DEFAULT 0 NOT NULL"`
Foo string `gorm:"unique_index"`
}
- Run Migrate
will auto create unique index on foo,deleted_flag
- Soft Delete
mdl.ID = 1
Model.Delete(mdl)
will execute SQL like:
update model set deleted_at = NOW(), deleted_flag = id where id = 1
- Undelete
mdl.Undelete()
db.Save(mdl)
before create gorm db:
// stacks that match these regexps will be ignored
gorm.AddLogFileIgnoreStackPattern(
regexp.MustCompile("core/model/base/pagination_sql.go"),
regexp.MustCompile("utils/helpers.go"),
)
type Request struct {
HostID uint `form:"host_id" sql:"column:h.id;op:eq"`
HostName string `form:"host_name" sql:"column:h.name;op:like"`
}
req := new(Request)
db = gorm.ApplySQLCondition(db, req)
// equal to:
// db = db.Where("h.id = ?", req.HostID).
// Where("h.id LIKE %?%", req.HostName)
supported tags: eq
, lt
, lte
, gt
, gte
, in
, ints
, strs
, like
, like-bin
more details in tag.go