database

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package database provides database connection management.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect() (*gorm.DB, error)

Connect establishes a database connection using environment configuration.

func ConnectWithConfig

func ConnectWithConfig(cfg DBConfig) (*gorm.DB, error)

ConnectWithConfig establishes a database connection using the provided configuration.

func WithTransaction

func WithTransaction(db *gorm.DB, fn TxFunc) error

WithTransaction wraps fn in a database transaction. If fn returns nil the transaction commits. If fn returns an error or panics the transaction rolls back.

Types

type CursorResult

type CursorResult struct {
	NextCursor string `json:"next_cursor,omitempty"`
	PrevCursor string `json:"prev_cursor,omitempty"`
	PerPage    int    `json:"per_page"`
	HasMore    bool   `json:"has_more"`
}

CursorResult holds metadata for cursor-based pagination.

func CursorPaginate

func CursorPaginate(db *gorm.DB, cursor, orderCol string, perPage int, direction string, dest interface{}) (*CursorResult, error)

CursorPaginate performs cursor-based pagination.

Parameters:

  • db: a *gorm.DB query (pre-scoped with any WHERE clauses).
  • cursor: base64-encoded value of the last seen item (empty string = first page).
  • orderCol: column to order and paginate by (must be unique and indexed, e.g. "id").
  • perPage: items per page, clamped to 1–100 (default 15).
  • direction: "next" (default, items after cursor) or "prev" (items before cursor).
  • dest: pointer to a slice of model structs.

The function sets NextCursor/PrevCursor on the result based on the last/first item's orderCol value. The caller can pass these cursors back for the next page.

type DBConfig

type DBConfig struct {
	Driver          string
	Host            string
	Port            string
	Name            string
	User            string
	Password        string
	SSLMode         string
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
	ConnMaxIdleTime time.Duration
}

DBConfig holds database connection configuration.

func NewDBConfig

func NewDBConfig() DBConfig

NewDBConfig reads database configuration from environment variables.

func NewReadDBConfig

func NewReadDBConfig() DBConfig

NewReadDBConfig reads read-replica configuration from DB_READ_* environment variables. Each setting falls back to the corresponding DB_* value, then to the same defaults used by NewDBConfig.

func (DBConfig) DSN

func (cfg DBConfig) DSN() string

DSN returns the data source name for the configured driver.

type PageResult

type PageResult struct {
	Page       int   `json:"page"`
	PerPage    int   `json:"per_page"`
	Total      int64 `json:"total"`
	TotalPages int   `json:"total_pages"`
}

PageResult holds metadata for offset-based pagination.

func Paginate

func Paginate(db *gorm.DB, page, perPage int, dest interface{}) (*PageResult, error)

Paginate performs offset-based pagination on the given query. page is clamped to ≥ 1, perPage is clamped to 1–100 (default 15). dest must be a pointer to a slice.

type Resolver

type Resolver struct {
	// contains filtered or unexported fields
}

Resolver holds separate database connections for write and read operations.

func NewResolver

func NewResolver(writer, reader *gorm.DB) *Resolver

NewResolver creates a Resolver with the given writer and reader connections. If reads should go to the same database, pass the same *gorm.DB for both.

func (*Resolver) Reader

func (r *Resolver) Reader() *gorm.DB

Reader returns the read (replica) database connection.

func (*Resolver) Writer

func (r *Resolver) Writer() *gorm.DB

Writer returns the write (primary) database connection.

type TxFunc

type TxFunc func(tx *gorm.DB) error

TxFunc is the callback signature for transactional operations. Return nil to commit, return an error to rollback.

Directories

Path Synopsis
Package migrations provides a file-based migration engine for GORM.
Package migrations provides a file-based migration engine for GORM.
Package seeders provides a registry-based database seeding system.
Package seeders provides a registry-based database seeding system.

Jump to

Keyboard shortcuts

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