crud

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: 0BSD Imports: 5 Imported by: 0

Documentation

Overview

Package crud simplifies the implementation of CRUD repositories.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Deleter

type Deleter[
	A ddd.AggregateRoot[I, S],
	I comparable,
	S ddd.Serialization[A],
] interface {
	// Delete the entity with the given ID from the repository.
	// If none is found, it returns an apperror.NotFound error.
	Delete(ctx context.Context, id I) error
}

Deleter abstracts deleting an entity from the repository.

type Getter

type Getter[
	A ddd.AggregateRoot[I, S],
	I comparable,
	S ddd.Serialization[A],
] interface {
	// Get returns the entity with the specified ID.
	// If none is found, it returns an apperror.NotFound error.
	Get(ctx context.Context, id I) (A, error)
}

Getter abstracts getting an entity by ID from a repository.

type InMemoryRepository

type InMemoryRepository[
	A ddd.AggregateRoot[I, S],
	I comparable,
	S ddd.Serialization[A],
] struct {
	Mutex          sync.RWMutex
	Serializations map[I]S

	// Configurable error message constructors.
	Errors struct {
		// NotFound constructs the error message when an item cannot
		// be found.
		NotFound func(id I) string

		// AlreadyExists constructs the error message when trying to
		// create an item when another item already exists with that ID.
		AlreadyExists func(id I) string
	}
}

InMemoryRepository provides a generic CRUD repository implementation for any aggregate root. Mainly meant to be used in in-memory databases for tests and prototyping.

Implements Repository.

func (*InMemoryRepository[A, I, S]) AlreadyExists

func (r *InMemoryRepository[A, I, S]) AlreadyExists(id I) error

func (*InMemoryRepository[A, I, S]) Delete

func (r *InMemoryRepository[A, I, S]) Delete(
	_ context.Context,
	id I,
) error

Delete the entity with the given ID from the repository. If none is found, it returns an apperror.NotFound error.

Provides Deleter.

func (*InMemoryRepository[A, I, S]) Get

func (r *InMemoryRepository[A, I, S]) Get(
	_ context.Context,
	id I,
) (A, error)

Get returns the entity with the specified ID. If none is found, it returns an apperror.NotFound error.

Provides Getter.

func (*InMemoryRepository[A, I, S]) Insert

func (r *InMemoryRepository[A, I, S]) Insert(
	_ context.Context,
	item A,
) error

Insert a new entity into the repository. If there is already a value with the same ID, it returns an apperror.Conflict error.

Provides Inserter.

func (*InMemoryRepository[A, I, S]) NotFound

func (r *InMemoryRepository[A, I, S]) NotFound(id I) error

func (*InMemoryRepository[A, I, S]) Reset

func (r *InMemoryRepository[A, I, S]) Reset()

Reset (re-)initializes the repository. It must be called before other methods.

func (*InMemoryRepository[A, I, S]) Update

func (r *InMemoryRepository[A, I, S]) Update(
	_ context.Context,
	id I,
	update func(x A) error,
) error

Update an entity already present in the repository. If none is found, it returns an apperror.NotFound error.

Provides Updater.

func (*InMemoryRepository[A, I, S]) Upsert

func (r *InMemoryRepository[A, I, S]) Upsert(
	_ context.Context,
	id I,
	insert func() (A, error),
	update func(x A) error,
) error

Upsert does one of two things:

  • If the entity with the specified ID does not exist, it inserts a new entity using the provided insert function.
  • If the entity with the specified ID exists, it updates the entity using the provided update function.

Provides Upserter.

type Inserter

type Inserter[
	A ddd.AggregateRoot[I, S],
	I comparable,
	S ddd.Serialization[A],
] interface {
	// Insert a new entity into the repository.
	// If there is already a value with the same ID,
	// it returns an apperror.Conflict error.
	Insert(ctx context.Context, x A) error
}

Inserter abstracts inserting a new entity into a repository.

type Repository

type Repository[
	A ddd.AggregateRoot[I, S],
	I comparable,
	S ddd.Serialization[A],
] interface {
	Getter[A, I, S]
	Inserter[A, I, S]
	Updater[A, I, S]
	Upserter[A, I, S]
	Deleter[A, I, S]
}

Repository defines the basic interface of a CRUD repository.

type Updater

type Updater[
	A ddd.AggregateRoot[I, S],
	I comparable,
	S ddd.Serialization[A],
] interface {
	// Update an entity already present in the repository.
	// If none is found, it returns an apperror.NotFound error.
	Update(ctx context.Context, id I, update func(x A) error) error
}

Updater abstracts updating an entity already present in a repository.

type Upserter

type Upserter[
	A ddd.AggregateRoot[I, S],
	I comparable,
	S ddd.Serialization[A],
] interface {
	// Upsert does one of two things:
	//   - If the entity with the specified ID does not exist,
	//     it inserts a new entity using the provided insert function.
	//   - If the entity with the specified ID exists,
	//     it updates the entity using the provided update function.
	Upsert(ctx context.Context,
		id I,
		insert func() (A, error),
		update func(x A) error,
	) error
}

Upserter abstracts inserting a value (if it is not already present in the repository) or updating it (if it is already present).

Jump to

Keyboard shortcuts

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