gormstore

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 10 Imported by: 1

Documentation

Overview

Package gormstore provides a GORM-based implementation of the store interface. This package leverages GORM, a popular ORM library for Golang, to offer a convenient and powerful way of performing CRUD operations and more on various data stores.

The package defines a generic Store type that integrates with GORM's functionalities, offering an abstraction over GORM's native methods to align with the store.Entity interface. This allows for operations like Get, List, Create, Update, and Delete, to be performed in a type-safe and efficient manner.

The Store type also supports advanced features such as transaction management through the TransactionScope, custom query building, batch operations, and conflict handling during upsert operations. It is designed to be flexible and extensible, making it suitable for a wide range of applications that require data persistence.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option[Entity store.Entity[ID], DTO store.Entity[ID], ID comparable] func(*Store[Entity, DTO, ID])

Option is a function that modifies the store. It is used to set various configuration options for the Store at the time of its creation.

func WithBatchSize

func WithBatchSize[
	Entity store.Entity[ID],
	DTO store.Entity[ID],
	ID comparable,
](
	batchSize int,
) Option[Entity, DTO, ID]

WithBatchSize sets the batch size for batch operations in the store. batchSize specifies the number of records to be processed in a single batch during batch operations.

func WithConverter

func WithConverter[
	Entity store.Entity[ID],
	DTO store.Entity[ID],
	ID comparable,
](
	converter converter.Converter[Entity, DTO, ID],
) Option[Entity, DTO, ID]

WithConverter sets the converter used for transforming between entity and DTO types. converter is an instance of a converter that can convert between the entity and DTO types.

func WithScopeBuilderOption

func WithScopeBuilderOption[
	Entity store.Entity[ID],
	DTO store.Entity[ID],
	ID comparable,
](
	options ...gormquery.Option,
) Option[Entity, DTO, ID]

WithScopeBuilderOption sets the scope builder options for the store. options are a variadic list of options that configure the behavior of the scope builder.

type Store

type Store[Entity store.Entity[ID], DTO store.Entity[ID], ID comparable] struct {
	OpScope      *gormopscope.TransactionScope
	Converter    converter.Converter[Entity, DTO, ID]
	ScopeBuilder *gormquery.ScopeBuilder
	BatchSize    int
}

Store represents a storage mechanism using GORM for database operations. It supports CRUD operations and is designed to be generic for any Entity and DTO types.

Entity: The domain model type. DTO: The data transfer object type, representing the database model. ID: The type of the unique identifier for the entity.

func New

func New[Entity store.Entity[ID], DTO store.Entity[ID], ID comparable](
	opScope *gormopscope.TransactionScope,
	options ...Option[Entity, DTO, ID],
) *Store[Entity, DTO, ID]

New initializes a new Store instance for handling CRUD operations on entities. It accepts an operation scope and a variable number of options to customize the store behavior. The function returns a pointer to the initialized Store.

Entity and DTO are types that must implement the store.Entity interface. ID is the type of the identifier for the entities.

func (*Store[Entity, DTO, ID]) Count

func (s *Store[Entity, DTO, ID]) Count(ctx context.Context, params ...query.Param) (int64, error)

Count returns the number of entities that satisfy the provided query parameters. The count is returned along with an error if the operation fails.

func (*Store[Entity, DTO, ID]) Create

func (s *Store[Entity, DTO, ID]) Create(ctx context.Context, entity Entity) (ID, error)

Create adds a new entity to the store and returns its ID. Returns an error if the creation fails.

func (*Store[Entity, DTO, ID]) CreateMany

func (s *Store[Entity, DTO, ID]) CreateMany(ctx context.Context, entities []Entity) error

CreateMany performs batch creation of entities. The BatchSize field of the store determines the number of entities in each batch. Returns an error if the operation fails.

func (*Store[Entity, DTO, ID]) Delete

func (s *Store[Entity, DTO, ID]) Delete(ctx context.Context, params ...query.Param) error

Delete removes entities from the store based on the provided query parameters. Returns an error if the deletion operation fails.

func (*Store[Entity, DTO, ID]) Exists

func (s *Store[Entity, DTO, ID]) Exists(ctx context.Context, params ...query.Param) (bool, error)

Exists checks for the existence of at least one entity that matches the query parameters. Returns true if such an entity exists, false otherwise.

func (*Store[Entity, DTO, ID]) Get

func (s *Store[Entity, DTO, ID]) Get(ctx context.Context, params ...query.Param) (Entity, error)

Get retrieves a single entity based on provided query parameters. It returns the entity if found, otherwise an error.

func (*Store[Entity, DTO, ID]) List

func (s *Store[Entity, DTO, ID]) List(ctx context.Context, params ...query.Param) ([]Entity, error)

List retrieves a list of entities matching the provided query parameters. Returns a slice of entities and an error if the operation fails.

func (*Store[Entity, DTO, ID]) PartialUpdate

func (s *Store[Entity, DTO, ID]) PartialUpdate(ctx context.Context, entity Entity, params ...query.Param) error

PartialUpdate updates specific fields of an existing entity in the store. Only non-zero fields of the entity are updated. Returns an error if the operation fails.

func (*Store[Entity, DTO, ID]) Update

func (s *Store[Entity, DTO, ID]) Update(ctx context.Context, entity Entity, params ...query.Param) error

Update modifies an existing entity in the store, including fields with zero values. Returns an error if the update operation fails.

func (*Store[Entity, DTO, ID]) Upsert

func (s *Store[Entity, DTO, ID]) Upsert(ctx context.Context, entity Entity, onConflict store.OnConflict) (ID, error)

Upsert either creates a new entity or updates an existing one based on the provided conflict resolution strategy. Returns the ID of the affected entity and an error if the operation fails.

Jump to

Keyboard shortcuts

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