repository

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Repository

type Repository[E any, K comparable] interface {
	// Create creates an entity in a persistence layer.
	Create(ctx context.Context, entity E) (E, error)
	// CreateBulk creates a set of entities in a persistence layer.
	CreateBulk(ctx context.Context, entities []E) ([]E, error)
	// Get returns an entity from a persistence layer identified by its ID. It returns an error if the entity doesn't exist.
	Get(ctx context.Context, id K) (E, error)
	// Find returns a set of entities from a persistence layer identified by their ID. It returns
	// an empty slice if no records were found.
	Find(ctx context.Context, ids []K) ([]E, error)
	// Update updates an entity.
	Update(ctx context.Context, id K, entity E) (E, error)
	// UpdateBulk updates multiple entities with values of entity.
	UpdateBulk(ctx context.Context, ids []K, entity E) ([]E, error)
	// Remove removes the given id from the persistence layer.
	Remove(ctx context.Context, id K) (E, error)
	// RemoveBulk removes a set of elements from the persistence layer.
	RemoveBulk(ctx context.Context, ids []K) ([]E, error)
}

Repository contains a set of methods to interact with a persistence layer. The E parameter represents an entity model. The K parameter represent the primary key.

func NewRepositorySQL

func NewRepositorySQL[E any, K comparable](db *gorm.DB) Repository[E, K]

NewRepositorySQL initializes a new implementation of Repository using an SQL ORM: gorm.

type SQL

type SQL[E any, K comparable] struct {
	// contains filtered or unexported fields
}

SQL implements Repository using gorm.

func (*SQL[E, K]) Create

func (r *SQL[E, K]) Create(ctx context.Context, entity E) (E, error)

Create creates an entity in a persistence layer.

func (*SQL[E, K]) CreateBulk

func (r *SQL[E, K]) CreateBulk(ctx context.Context, entities []E) ([]E, error)

CreateBulk creates a set of entities in a persistence layer.

func (*SQL[E, K]) Find

func (r *SQL[E, K]) Find(ctx context.Context, ids []K) ([]E, error)

Find returns a set of entities from a persistence layer identified by their IDs. It returns an empty slice if no records were found.

func (*SQL[E, K]) Get

func (r *SQL[E, K]) Get(ctx context.Context, id K) (E, error)

Get returns an entity from a persistence layer identified by its ID. It returns an error if the entity doesn't exist.

func (*SQL[E, K]) Remove

func (r *SQL[E, K]) Remove(ctx context.Context, id K) (E, error)

Remove removes the given id from the persistence layer.

func (*SQL[E, K]) RemoveBulk

func (r *SQL[E, K]) RemoveBulk(ctx context.Context, ids []K) ([]E, error)

RemoveBulk removes a set of elements from the persistence layer.

func (*SQL[E, K]) Update

func (r *SQL[E, K]) Update(ctx context.Context, id K, entity E) (E, error)

Update updates an entity.

func (*SQL[E, K]) UpdateBulk

func (r *SQL[E, K]) UpdateBulk(ctx context.Context, ids []K, entity E) ([]E, error)

UpdateBulk updates multiple entities with values of entity.

Jump to

Keyboard shortcuts

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