Documentation
¶
Overview ¶
Package database provides database connection management.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConnectWithConfig ¶
ConnectWithConfig establishes a database connection using the provided configuration.
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.
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.
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver holds separate database connections for write and read operations.
func NewResolver ¶
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.
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. |