repository

package
v8.12.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoFilter represents an error when no filter are provided.
	ErrNoFilter = errors.New("no filters provided")
	// ErrNoEntriesUpdated represent an error when no entries were updated in the database
	// after an Update operation.
	ErrNoEntriesUpdated = errors.New("no entries were updated")
	// ErrNoEntriesDeleted represent an error when no entries were deleted in the database
	// after a Delete operation.
	ErrNoEntriesDeleted = errors.New("no entries were deleted")
)

Functions

This section is empty.

Types

type Filter

type Filter struct {
	// Template contains a filter template.
	// SQL:
	//   Contains a template string with placeholders (?).
	//   All values in a filter that are filled in using user-defined values should use a placeholder (?) to prevent
	//   SQL injection.
	// Example: `name = ? AND age = ?`
	Template string
	// Values contains a sequence of values for the placeholders defined in the template.
	// The values are replaced in the order they are defined in the template.
	// Example: `["Test", 33]`
	Values []interface{}
}

Filter is used by a Repository to filter data.

type Model

type Model interface {
	// TableName returns the table/collection name for a certain model.
	TableName() string
	// GetID returns the unique identifier for this Model.
	// It returns the ID of the model that has been persisted. It returns 0 if no value has been defined.
	// For SQL environments, this would be the primary key.
	GetID() uint
}

Model represents a generic entity. A Model is part of the domain layer and is persisted by a certain Repository.

type Option

type Option interface {
	// IsOption is a dummy method used to identify repository options.
	IsOption()
}

Option is used to define repository operation options for the generic Repository interface. It is expected that each Repository implementation will have its own set of concrete options available, and those options are not expected to be compatible with other implementations.

type Repository

type Repository interface {
	// FirstOrCreate inserts a new entry if the given filters don't find any existing record.
	// entity: must be a pointer to a Model implementation. Results will be saved in this argument if the record exists.
	FirstOrCreate(ctx context.Context, entity Model, filters ...Filter) error
	// Create inserts a single entry.
	// entity: The entry to insert.
	Create(ctx context.Context, entity Model) (Model, error)
	// CreateBulk creates multiple entries with a single operation.
	// entities: should be a slice of a Model implementation.
	CreateBulk(ctx context.Context, entities []Model) ([]Model, error)
	// Find filters entries and stores filtered entries in output.
	// output: will contain the result of the query. It must be a pointer to a slice.
	// options: configuration options for the search. Refer to the implementation's set of options to get a lit of options.
	Find(ctx context.Context, output interface{}, options ...Option) error
	// FindOne filters entries and stores the first filtered entry in output.
	// output: must be a pointer to a Model implementation.
	FindOne(ctx context.Context, output Model, filters ...Filter) error
	// Last gets the last record ordered by primary key desc.
	// output: must be a pointer to a Model implementation.
	Last(ctx context.Context, output Model, filters ...Filter) error
	// Update updates all model entries that match the provided filters with the given data.
	// data: must be a map[string]interface{}
	// filters: selection criteria for entries that should be updated.
	Update(ctx context.Context, data interface{}, filters ...Filter) error
	// Delete removes all the model entries that match filters.
	// filters: selection criteria for entries that should be deleted.
	Delete(ctx context.Context, opts ...Option) error
	// Count counts all the model entries that match filters.
	// filters: selection criteria for entries that should be considered when counting entries.
	Count(ctx context.Context, filters ...Filter) (uint64, error)
	// Model returns this repository's model.
	Model() Model
}

Repository holds methods to CRUD an entity on a certain persistence layer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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