added dao.WithoutHooks() helper

This commit is contained in:
Gani Georgiev
2023-06-01 15:42:38 +03:00
parent dcb00a3917
commit 4c2dcac61a
2 changed files with 84 additions and 1 deletions

View File

@@ -29,7 +29,8 @@ func NewMultiDB(concurrentDB, nonconcurrentDB dbx.Builder) *Dao {
}
// Dao handles various db operations.
// Think of Dao as a repository and service layer in one.
//
// You can think of Dao as a repository and service layer in one.
type Dao struct {
// in a transaction both refer to the same *dbx.TX instance
concurrentDB dbx.Builder
@@ -43,6 +44,7 @@ type Dao struct {
// This field has no effect if an explicit query context is already specified.
ModelQueryTimeout time.Duration
// write hooks
BeforeCreateFunc func(eventDao *Dao, m models.Model) error
AfterCreateFunc func(eventDao *Dao, m models.Model)
BeforeUpdateFunc func(eventDao *Dao, m models.Model) error
@@ -81,6 +83,21 @@ func (dao *Dao) Clone() *Dao {
return &clone
}
// WithoutHooks returns a new Dao with the same configuration options
// as the current one, but without create/update/delete hooks.
func (dao *Dao) WithoutHooks() *Dao {
new := dao.Clone()
new.BeforeCreateFunc = nil
new.AfterCreateFunc = nil
new.BeforeUpdateFunc = nil
new.AfterUpdateFunc = nil
new.BeforeDeleteFunc = nil
new.AfterDeleteFunc = nil
return new
}
// ModelQuery creates a new preconfigured select query with preset
// SELECT, FROM and other common fields based on the provided model.
func (dao *Dao) ModelQuery(m models.Model) *dbx.SelectQuery {