Created transaction wrapper

This commit is contained in:
Lukas Jorg 2021-09-13 08:30:27 +02:00 committed by Gerhard Gruber
parent 0caca1a6f9
commit 98d691603d

11
main.go
View File

@ -522,6 +522,17 @@ func (s *DB) Rollback() *DB {
return s return s
} }
// WrapInTx wraps a method in a transaction
func (s *DB) WrapInTx(f func(tx *gorm.DB) (error)) error {
tx := s.Begin()
if err := f(tx); err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
// NewRecord check if value's primary key is blank // NewRecord check if value's primary key is blank
func (s *DB) NewRecord(value interface{}) bool { func (s *DB) NewRecord(value interface{}) bool {
return s.NewScope(value).PrimaryKeyZero() return s.NewScope(value).PrimaryKeyZero()