repository

package
v6.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2022 License: Apache-2.0 Imports: 6 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 ModelSQL

type ModelSQL struct {
	// ID contains the primary key identifier.
	ID uint `gorm:"primary_key"`
	// CreatedAt contains the date and time at which this model has been persisted.
	CreatedAt time.Time `json:"created_at"`
	// UpdatedAt contains the last date and time when this model has been updated.
	UpdatedAt time.Time `json:"updated_at"`
	// DeletedAt is used to implement soft record deletion. If set, the record will be considered
	// as deleted.
	DeletedAt *time.Time `json:"deleted_at" sql:"index"`
}

ModelSQL implements Model for SQL backends. It provides a set of common generic fields and operations that partially implement the repository.Model interface. To use it, embed it in your application-specific repository.Model implementation.

func (ModelSQL) GetID

func (m ModelSQL) GetID() uint

GetID returns the unique identifier for this Model.

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.

func Fields

func Fields(fields ...string) Option

Fields defines fields to return. Passing this Option to a Repository operation overwrites any previous GroupBy options passed.

func GroupBy

func GroupBy(fields ...string) Option

GroupBy groups results based field values. Passing this Option to a Repository operation overwrites any previous GroupBy options passed.

func MaxResults

func MaxResults(n int) Option

MaxResults defines the maximum number of results for an operation that can return multiple results. Passing this Option to a Repository operation overwrites any previous MaxResults options passed.

func Offset

func Offset(offset int) Option

Offset defines a number of results to skip before starting to capture values to return. This Option will be ignored if the MaxResults Option is not present. Passing this Option to a Repository operation overwrites any previous Offset options passed.

func OrderBy

func OrderBy(orders ...OrderByField) Option

OrderBy sorts results based on fields. Use the Ascending and Descending functions to pass orders to this Option. In situations with multiple orders, they are applied in sequence. Multiple OrderBy options can be passed to a single Repository operation. They are appended to any previous orders.

func Preload

func Preload(field string, filter ...interface{}) Option

Preload allows loading a single related model to have it be included in the returned value. The field parameter must match the model field name exactly (case-sensitive). An optional filter composed of a template and any number of values can be passed to filter preloaded results. Multiple Preload options can be passed to a single Repository operation. They are appended to any previous preloads.

func Where

func Where(template string, values ...interface{}) Option

Where filters results based on passed conditions. Multiple Filter options can be passed to a single Repository operation. They are logically ANDed together.

type OrderByField

type OrderByField string

OrderByField contains order by information for a field

func Ascending

func Ascending(field string) OrderByField

Ascending sorts the passed field in ascending order.

func Descending

func Descending(field string) OrderByField

Descending sorts the passed field in descending order.

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(entity Model, filters ...Filter) error
	// Create inserts a single entry.
	// entity: The entry to insert.
	Create(entity Model) (Model, error)
	// CreateBulk is a bulk operation to create multiple entries with a single operation.
	// entities: should be a slice of a Model implementation.
	CreateBulk(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(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(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(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(data interface{}, filters ...Filter) error
	// Delete removes all the model entries that match filters.
	// filters: selection criteria for entries that should be deleted.
	Delete(filters ...Filter) error
	// Count counts all the model entries that match filters.
	// filters: selection criteria for entries that should be considered when counting entries.
	Count(filters ...Filter) (uint64, error)
	// Model returns this repository's model.
	Model() Model
}

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

func NewRepository

func NewRepository(db *gorm.DB, entity Model) Repository

NewRepository initializes a new Repository implementation for SQL databases.

type SQLOption

type SQLOption func(r *gorm.DB)

SQLOption is a SQL-specific Repository Option implementation. It is used to configure SQL repository operations. TODO This should be extracted into its own package to avoid colliding with other implementations

func (SQLOption) IsOption

func (l SQLOption) IsOption()

Jump to

Keyboard shortcuts

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