1
gorm-crud/deleter.go
2024-01-28 15:19:39 -06:00

31 lines
705 B
Go

// Copyright (C) 2024 Mya Pitzeruse
// SPDX-License-Identifier: MIT
package crud
import (
"context"
"gorm.io/gorm"
)
// DeleteFunc defines the signature of the function returned by Deleter. It simplifies the interface for deleting data
// using gorm.
type DeleteFunc func(ctx context.Context, filters ...map[string]interface{}) error
// Deleter returns a function that can delete data using gorm db.
func Deleter[T any](db *gorm.DB) DeleteFunc {
prototype := new(T)
return func(ctx context.Context, filters ...map[string]interface{}) error {
q := Transaction(ctx, db).
Model(prototype)
for _, filter := range filters {
q = q.Where(filter)
}
return q.Delete(prototype).Error
}
}