Documentation
¶
Overview ¶
Package database provides database connection and session management using GORM.
Index ¶
- Variables
- func ApplyConditions(db *gorm.DB, options ...repository.Option) *gorm.DB
- func ApplyOptions(db *gorm.DB, options ...repository.Option) *gorm.DB
- func ApplySearchFilters(db *gorm.DB, filters search.Filters) *gorm.DB
- func WithTransaction(ctx context.Context, db Database, fn func(tx *gorm.DB) error) error
- func WithTransactionResult[T any](ctx context.Context, db Database, fn func(tx *gorm.DB) (T, error)) (T, error)
- type Database
- type EntityMapper
- type Filter
- type FilterOperator
- type OrderBy
- type PgVector
- type Query
- func (q Query) Apply(db *gorm.DB) *gorm.DB
- func (q Query) Equal(field string, value any) Query
- func (q Query) Filters() []Filter
- func (q Query) GreaterThan(field string, value any) Query
- func (q Query) GreaterThanOrEqual(field string, value any) Query
- func (q Query) ILike(field string, pattern string) Query
- func (q Query) In(field string, values any) Query
- func (q Query) IsNotNull(field string) Query
- func (q Query) IsNull(field string) Query
- func (q Query) LessThan(field string, value any) Query
- func (q Query) LessThanOrEqual(field string, value any) Query
- func (q Query) Like(field string, pattern string) Query
- func (q Query) Limit(limit int) Query
- func (q Query) LimitValue() int
- func (q Query) NotEqual(field string, value any) Query
- func (q Query) NotIn(field string, values any) Query
- func (q Query) Offset(offset int) Query
- func (q Query) OffsetValue() int
- func (q Query) Order(field string, direction SortDirection) Query
- func (q Query) OrderAsc(field string) Query
- func (q Query) OrderDesc(field string) Query
- func (q Query) Orders() []OrderBy
- func (q Query) Paginate(page, pageSize int) Query
- func (q Query) Where(field string, operator FilterOperator, value any) Query
- func (q Query) WhereBetween(field string, low, high any) Query
- type Repository
- func (r Repository[D, E]) Count(ctx context.Context, options ...repository.Option) (int64, error)
- func (r Repository[D, E]) DB(ctx context.Context) *gorm.DB
- func (r Repository[D, E]) DeleteBy(ctx context.Context, options ...repository.Option) error
- func (r Repository[D, E]) Exists(ctx context.Context, options ...repository.Option) (bool, error)
- func (r Repository[D, E]) Find(ctx context.Context, options ...repository.Option) ([]D, error)
- func (r Repository[D, E]) FindOne(ctx context.Context, options ...repository.Option) (D, error)
- func (r Repository[D, E]) Mapper() EntityMapper[D, E]
- func (r Repository[D, E]) Table() string
- type SortDirection
- type Transaction
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("entity not found")
ErrNotFound indicates the requested entity was not found.
var ErrUnsupportedDriver = errors.New("unsupported database driver")
ErrUnsupportedDriver indicates the database URL uses an unsupported driver.
Functions ¶
func ApplyConditions ¶
ApplyConditions applies only WHERE conditions (no limit/offset/order) for COUNT queries.
func ApplyOptions ¶
ApplyOptions builds a store.Query from the given options and applies it to a GORM session.
func ApplySearchFilters ¶
ApplySearchFilters adds JOINs and WHERE clauses to a GORM query based on search filters. The calling table must have a snippet_id column that stores enrichment IDs as strings; the JOINs cast snippet_id to the appropriate integer type for the dialect.
func WithTransaction ¶
WithTransaction executes fn within a transaction, committing on success or rolling back on error.
Types ¶
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
Database wraps a GORM connection with lifecycle management.
func NewDatabase ¶
NewDatabase creates a new Database from a connection URL. Supported URL formats: - sqlite:///path/to/file.db - postgresql://user:pass@host:port/dbname - postgres://user:pass@host:port/dbname
func NewDatabaseWithConfig ¶
NewDatabaseWithConfig creates a Database with custom GORM configuration.
func (Database) ConfigurePool ¶
ConfigurePool sets connection pool parameters.
func (Database) GORM ¶
GORM returns the underlying GORM database instance. This is useful for repositories that need direct access to the *gorm.DB.
func (Database) IsPostgres ¶
IsPostgres returns true if the underlying database is PostgreSQL.
type EntityMapper ¶
EntityMapper defines the interface for mapping between domain and database model types.
type Filter ¶
type Filter struct {
// contains filtered or unexported fields
}
Filter represents a single query filter condition.
func NewBetweenFilter ¶
NewBetweenFilter creates a new BETWEEN Filter.
func NewFilter ¶
func NewFilter(field string, operator FilterOperator, value any) Filter
NewFilter creates a new Filter.
func (Filter) Operator ¶
func (f Filter) Operator() FilterOperator
Operator returns the filter operator.
type FilterOperator ¶
type FilterOperator int
FilterOperator represents SQL comparison operators.
const ( OpEqual FilterOperator = iota OpNotEqual OpGreaterThan OpGreaterThanOrEqual OpLessThan OpLessThanOrEqual OpLike OpILike OpIn OpNotIn OpIsNull OpIsNotNull OpBetween )
FilterOperator values.
func (FilterOperator) String ¶
func (o FilterOperator) String() string
String returns the SQL representation of the operator.
type OrderBy ¶
type OrderBy struct {
// contains filtered or unexported fields
}
OrderBy represents a sort specification.
func NewOrderBy ¶
func NewOrderBy(field string, direction SortDirection) OrderBy
NewOrderBy creates a new OrderBy.
func (OrderBy) Direction ¶
func (o OrderBy) Direction() SortDirection
Direction returns the sort direction.
type PgVector ¶
type PgVector struct {
// contains filtered or unexported fields
}
PgVector wraps a float64 slice for use as a PostgreSQL VECTOR column value. It implements sql.Scanner and driver.Valuer to convert between Go and the PostgreSQL text format "[1.0,2.0,3.0]".
func NewPgVector ¶
NewPgVector creates a PgVector from a float64 slice. The input is defensively copied so later mutations of the source slice have no effect.
func (PgVector) Floats ¶
Floats returns a defensive copy of the underlying float64 slice. Returns nil if the vector was never initialized (e.g. scanned from nil).
func (*PgVector) Scan ¶
Scan implements sql.Scanner. It parses the PostgreSQL vector text format "[1.0,2.0,3.0]" from either a string or []byte value.
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query represents a database query with filters, ordering, and pagination.
func (Query) GreaterThan ¶
GreaterThan adds a greater-than filter.
func (Query) GreaterThanOrEqual ¶
GreaterThanOrEqual adds a greater-than-or-equal filter.
func (Query) LessThanOrEqual ¶
LessThanOrEqual adds a less-than-or-equal filter.
func (Query) LimitValue ¶
LimitValue returns the limit value (0 means no limit).
func (Query) Order ¶
func (q Query) Order(field string, direction SortDirection) Query
Order adds an ordering specification.
type Repository ¶
Repository provides generic persistence operations for database entities using repository.Option-based queries.
func NewRepository ¶
func NewRepository[D any, E any](db Database, mapper EntityMapper[D, E], label string) Repository[D, E]
NewRepository creates a new Repository.
func NewRepositoryForTable ¶
func NewRepositoryForTable[D any, E any](db Database, mapper EntityMapper[D, E], label string, tableName string) Repository[D, E]
NewRepositoryForTable creates a Repository that targets a specific table name. GORM caches schemas by type, so dynamic TableName() methods on entities do not work when the same struct maps to multiple tables. This constructor sets a tableName that is applied via .Table() after .Model() in every operation, which GORM respects because TableExpr prevents Parse() from overriding Statement.Table.
func (Repository[D, E]) Count ¶
func (r Repository[D, E]) Count(ctx context.Context, options ...repository.Option) (int64, error)
Count returns the number of entities matching the given options.
func (Repository[D, E]) DB ¶
func (r Repository[D, E]) DB(ctx context.Context) *gorm.DB
DB returns a GORM session scoped to the optional dynamic table.
func (Repository[D, E]) DeleteBy ¶
func (r Repository[D, E]) DeleteBy(ctx context.Context, options ...repository.Option) error
DeleteBy removes entities matching the given options.
func (Repository[D, E]) Exists ¶
func (r Repository[D, E]) Exists(ctx context.Context, options ...repository.Option) (bool, error)
Exists checks if any entity matches the given options.
func (Repository[D, E]) Find ¶
func (r Repository[D, E]) Find(ctx context.Context, options ...repository.Option) ([]D, error)
Find retrieves entities matching the given options. When no ordering is specified, results are sorted by primary key ASC to ensure deterministic output across database engines (Postgres does not guarantee row order without an explicit ORDER BY).
func (Repository[D, E]) FindOne ¶
func (r Repository[D, E]) FindOne(ctx context.Context, options ...repository.Option) (D, error)
FindOne retrieves a single entity matching the given options.
func (Repository[D, E]) Mapper ¶
func (r Repository[D, E]) Mapper() EntityMapper[D, E]
Mapper returns the entity mapper for external use.
func (Repository[D, E]) Table ¶
func (r Repository[D, E]) Table() string
Table returns the dynamic table name, or empty if not set.
type SortDirection ¶
type SortDirection int
SortDirection represents sort direction.
const ( SortAsc SortDirection = iota SortDesc )
SortDirection values.
func (SortDirection) String ¶
func (s SortDirection) String() string
String returns the SQL representation.
type Transaction ¶
type Transaction struct {
// contains filtered or unexported fields
}
Transaction wraps a GORM transaction with commit/rollback semantics.
func NewTransaction ¶
func NewTransaction(ctx context.Context, db Database) (Transaction, error)
NewTransaction starts a new database transaction.
func (*Transaction) Rollback ¶
func (t *Transaction) Rollback() error
Rollback rolls back the transaction if not already finished.
func (Transaction) Session ¶
func (t Transaction) Session() *gorm.DB
Session returns the transaction session for executing queries.