Documentation
¶
Overview ¶
Package repository provides a generic repository pattern interface for database operations. It offers a type-safe wrapper around database operations with support for transactions, scoped queries, pagination, and common CRUD operations.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidType is returned when the generic type T is not a struct type. ErrInvalidType = errors.New("generic type must be a struct type") // ErrNotFound is returned when a requested record is not found. ErrNotFound = errors.New("not found") // ErrTxRequired is returned when a transaction is required but not provided. ErrTxRequired = errors.New("tx is required") // ErrNilSchema is returned when database schema parsing results in a nil schema. ErrNilSchema = errors.New("nil schema") // ErrDangerous is returned when attempting potentially dangerous operations // like deleting without conditions. ErrDangerous = errors.New("dangerous operation is prohibited") )
Common errors returned by repository operations.
Functions ¶
This section is empty.
Types ¶
type PageResult ¶
type PageResult[T any] struct { Items []T `json:"items"` // The items in the current page Total int64 `json:"total"` // Total number of items across all pages Page int `json:"page"` // Current page number (1-based) PageSize int `json:"page_size"` // Number of items per page HasNext bool `json:"has_next"` // Whether there are more pages available }
PageResult represents the result of a paginated query.
type Repository ¶
type Repository[T any] interface { // Create inserts a new entity into the database. // If tx is provided, the operation is performed within that transaction. Create(ctx context.Context, tx Transaction, entity *T) error // Update saves the entity to the database, updating all fields. // If tx is provided, the operation is performed within that transaction. Update(ctx context.Context, tx Transaction, entity *T) error // Delete removes records from the database based on the provided conditions. // At least one scope must be provided to prevent accidental deletion of all records. // If tx is provided, the operation is performed within that transaction. Delete(ctx context.Context, tx Transaction, scopes ...Scope) error // First retrieves the first record that matches the provided scopes. // Returns ErrNotFound if no record is found. First(ctx context.Context, scopes ...Scope) (T, error) // List retrieves all records that match the provided scopes. // Consider using Limit and Order scopes to control the result set size and ordering. List(ctx context.Context, scopes ...Scope) ([]T, error) // Count returns the number of records that match the provided scopes. Count(ctx context.Context, scopes ...Scope) (int64, error) // Exists checks whether any record matching the provided scopes exists. // Returns true if at least one record exists, false otherwise. Exists(ctx context.Context, scopes ...Scope) (bool, error) // Page retrieves a paginated result set based on the provided scopes. // Page numbers are 1-based. If page <= 0, defaults to 1. // If pageSize <= 0, defaults to 20. Maximum pageSize is capped at 1000. Page(ctx context.Context, page, pageSize int, scopes ...Scope) (PageResult[T], error) // BatchInsert performs a batch insert operation for multiple entities. // If tx is provided, the operation is performed within that transaction. // The optional batchSize parameter controls how many records are inserted in each batch. // If not specified or zero, defaults to 1000 records per batch. BatchInsert(ctx context.Context, tx Transaction, entities []*T, batchSize ...int) error // Transact executes the provided function within a database transaction. // If the function returns an error, the transaction is rolled back. // Otherwise, the transaction is committed. Transact(ctx context.Context, fn func(ctx context.Context, tx Transaction) error) error // FirstForUpdate retrieves the first record that matches the provided scopes // with a SELECT FOR UPDATE lock. This method requires a transaction to be provided. // Returns ErrNotFound if no record is found, ErrTxRequired if no transaction is provided. FirstForUpdate(ctx context.Context, tx Transaction, scopes ...Scope) (T, error) // FindForUpdate retrieves all records that match the provided scopes // with a SELECT FOR UPDATE lock. This method requires a transaction to be provided. // Returns ErrTxRequired if no transaction is provided. FindForUpdate(ctx context.Context, tx Transaction, scopes ...Scope) ([]T, error) }
Repository defines the generic repository interface for database operations on entities of type T. It provides type-safe methods for CRUD operations, querying, and transaction handling.
type Scope ¶
Scope represents a function that can modify a database query. Scopes can be chained together to build complex queries in a composable way.
type Transaction ¶
type Transaction interface{}
Transaction represents a database transaction. The specific implementation depends on the underlying database driver.