repository

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2025 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GenericRepository

type GenericRepository[T any] struct {
	// contains filtered or unexported fields
}

GenericRepository is a GORM-based implementation of the Repository interface.

func NewGenericRepository

func NewGenericRepository[T any](db *gorm.DB, logger *logrus.Logger) *GenericRepository[T]

NewGenericRepository creates a new GenericRepository instance using the provided GORM DB.

func (*GenericRepository[T]) Create

func (r *GenericRepository[T]) Create(model *T) error

Create inserts a new model instance into the database within a transaction. It automatically sets the CreatedAt and UpdatedAt fields.

func (*GenericRepository[T]) Delete

func (r *GenericRepository[T]) Delete(id interface{}) error

Delete removes a model instance identified by id within a transaction.

func (*GenericRepository[T]) GetAll

func (r *GenericRepository[T]) GetAll(scopes ...ScopeWithLog) ([]T, error)

GetAll returns all model instances.

func (*GenericRepository[T]) GetByID

func (r *GenericRepository[T]) GetByID(id interface{}) (T, error)

GetByID retrieves a model instance by its identifier.

func (*GenericRepository[T]) Update

func (r *GenericRepository[T]) Update(model *T) error

Update modifies an existing model instance in the database within a transaction. It automatically sets the UpdatedAt field.

type Repository

type Repository[T any] interface {
	// GetByID retrieves a model instance by its identifier.
	GetByID(id interface{}) (T, error)
	// GetAll returns all model instances that match the provided filter.
	// The filter is a map of field names to their expected values.
	GetAll(scopes ...ScopeWithLog) ([]T, error)
	// Create inserts a new model instance into the database.
	Create(model *T) error
	// Update modifies an existing model instance in the database.
	Update(model *T) error
	// Delete removes a model instance identified by id.
	Delete(id interface{}) error
}

Repository is a generic interface that abstracts data storage operations for a model of type T.

type ScopeWithLog

type ScopeWithLog func(db *gorm.DB, logger *logrus.Logger) *gorm.DB

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL