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

27 lines
624 B
Go

// Copyright (C) 2024 Mya Pitzeruse
// SPDX-License-Identifier: MIT
package crud
import (
"context"
"gorm.io/gorm"
)
// CreateFunc defines the method signature of the function returned by the Creator method. It simplifies the operation
// of creating records in the database.
type CreateFunc[T any] func(ctx context.Context, record *T) error
// Creator returns a function that can create data using gorm db.
func Creator[T any](db *gorm.DB) CreateFunc[T] {
prototype := new(T)
return func(ctx context.Context, record *T) error {
return Transaction(ctx, db).
Model(prototype).
Create(record).
Error
}
}