Skip to content

Commit

Permalink
Merge pull request #47 from DrOctavius/main
Browse files Browse the repository at this point in the history
gorm delete
  • Loading branch information
DrOctavius authored Apr 7, 2023
2 parents 30d117c + 947e49e commit 39fc6f9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/clients/db/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ func DB() *gorm.DB {
return _db
}

func DBCtx(ctx context.Context) *gorm.DB {
return DB().WithContext(ctx)
}

// NewClient -> Get the default driver and generate a new client...
func NewClient(instanceName string) (*gorm.DB, error) {
cl := getDefaultDBClient()
Expand Down
85 changes: 85 additions & 0 deletions core/clients/db/dbdel/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package dbdel

import (
"context"
"github.com/google/uuid"
"github.com/kyaxcorp/go-core/core/clients/db"
"github.com/kyaxcorp/go-core/core/clients/db/helper"
"github.com/kyaxcorp/go-core/core/helpers/_struct"
"gorm.io/gorm"
"gorm.io/gorm/callbacks"
)

type DeleteInput struct {
UserID uuid.UUID
Ctx context.Context
Record interface{}
DB *gorm.DB
}

// We created these functions because GORM doesn't allow setting additional data When Calling Delete function!

func Delete(input DeleteInput) (_err error) {
var dbc *gorm.DB
if input.DB == nil {
dbc = db.DBCtx(input.Ctx)
} else {
dbc = input.DB
}

_struct.SetAny(input.Record, "DeletedByID", &input.UserID)
_struct.SetAny(input.Record, "DeletedAt", helper.DeletedAtNowP())

_err = dbc.Transaction(func(tx *gorm.DB) error {
var txErr error
if rr, ok := input.Record.(callbacks.BeforeDeleteInterface); ok {
txErr = rr.BeforeDelete(tx)
if txErr != nil {
return txErr
}
}
tx.Statement.SkipHooks = true
result := tx.Save(input.Record)
if result.Error != nil {
return result.Error
}
tx.Statement.SkipHooks = false
if rr, ok := input.Record.(callbacks.AfterDeleteInterface); ok {
txErr = rr.AfterDelete(tx)
if txErr != nil {
return txErr
}
}

return nil
})

return
}

func DeleteV2(input DeleteInput) (_err error) {
var dbc *gorm.DB
if input.DB == nil {
dbc = db.DBCtx(input.Ctx)
} else {
dbc = input.DB
}

_err = dbc.Transaction(func(tx *gorm.DB) error {
// Update this record!
tx.Statement.SkipHooks = true
result := tx.Model(input.Record).Update("deleted_by_id", input.UserID)
if result.Error != nil {
return result.Error
}
tx.Statement.SkipHooks = false
result = tx.Delete(input.Record)
if result.Error != nil {
return result.Error
}

return nil
})

return
}
5 changes: 5 additions & 0 deletions core/clients/db/helper/deleted_at.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ func DeletedAt(t *time.Time) gorm.DeletedAt {
return deletedAt
}

func DeletedAtP(t *time.Time) *gorm.DeletedAt {
d := DeletedAt(t)
return &d
}

func DeletedAtNow() gorm.DeletedAt {
deletedAt := gorm.DeletedAt{}
deletedAt.Time = time.Now()
Expand Down

0 comments on commit 39fc6f9

Please sign in to comment.