postgres

package
v0.0.78 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddColumn

func AddColumn(db *gorm.DB, model interface{}, column string) error

AddColumn adds a column to table

func AutoMigrate

func AutoMigrate(db *gorm.DB, models ...interface{}) error

AutoMigrate runs auto migration for given models

func Close

func Close(ctx context.Context, db *gorm.DB, serviceName string) error

Close gracefully closes the database connection

func Count

func Count(ctx context.Context, db *gorm.DB, model interface{}, count *int64, conds ...interface{}) error

Count counts records matching given conditions

func Create

func Create(ctx context.Context, db *gorm.DB, value interface{}) error

Create creates a new record

func CreateInBatches

func CreateInBatches(ctx context.Context, db *gorm.DB, value interface{}, batchSize int) error

CreateInBatches creates multiple records in batches

func CreateIndex

func CreateIndex(db *gorm.DB, model interface{}, indexName string) error

CreateIndex creates an index

func CreateTable

func CreateTable(db *gorm.DB, model interface{}) error

CreateTable creates table for given model

func Delete

func Delete(ctx context.Context, db *gorm.DB, model interface{}, conds ...interface{}) error

Delete deletes record(s) matching given conditions

func Distinct

func Distinct(ctx context.Context, db *gorm.DB, model interface{}, dest interface{}, column string, conds ...interface{}) error

Distinct finds distinct values

func DropColumn

func DropColumn(db *gorm.DB, model interface{}, column string) error

DropColumn drops a column from table

func DropIndex

func DropIndex(db *gorm.DB, model interface{}, indexName string) error

DropIndex drops an index

func DropTable

func DropTable(db *gorm.DB, model interface{}) error

DropTable drops table for given model

func Exec

func Exec(ctx context.Context, db *gorm.DB, sql string, values ...interface{}) error

Exec executes raw SQL command (INSERT, UPDATE, DELETE)

func Exists

func Exists(ctx context.Context, db *gorm.DB, model interface{}, conds ...interface{}) (bool, error)

Exists checks if any record matches the given conditions

func Find

func Find(ctx context.Context, db *gorm.DB, dest interface{}, conds ...interface{}) error

Find finds all records matching given conditions

func FindInBatches

func FindInBatches(ctx context.Context, db *gorm.DB, dest interface{}, batchSize int, fn func(*gorm.DB, int) error) error

FindInBatches finds records in batches

func FindOne

func FindOne(ctx context.Context, db *gorm.DB, dest interface{}, conds ...interface{}) error

FindOne finds one record that matches given conditions

func FindWithPagination

func FindWithPagination(ctx context.Context, db *gorm.DB, dest interface{}, page, pageSize int, conds ...interface{}) error

FindWithPagination finds records with pagination

func First

func First(ctx context.Context, db *gorm.DB, dest interface{}, conds ...interface{}) error

First finds the first record ordered by primary key

func FirstOrCreate

func FirstOrCreate(ctx context.Context, db *gorm.DB, dest interface{}, conds ...interface{}) error

FirstOrCreate finds the first matched record or creates a new one

func FirstOrInit

func FirstOrInit(ctx context.Context, db *gorm.DB, dest interface{}, conds ...interface{}) error

FirstOrInit finds the first matched record or initializes a new one

func GetDB

func GetDB(gormDB *gorm.DB) (*sql.DB, error)

GetDB returns the underlying sql.DB from GORM

func GetTracedClient

func GetTracedClient(ctx context.Context, cfg Config) (*gorm.DB, error)

GetTracedClient returns a PostgreSQL client with custom tracing configuration

func Group

func Group(ctx context.Context, db *gorm.DB, dest interface{}, groupBy string, conds ...interface{}) error

Group performs a GROUP BY query

func HasTable

func HasTable(db *gorm.DB, model interface{}) bool

HasTable checks if table exists

func Joins

func Joins(ctx context.Context, db *gorm.DB, dest interface{}, joinQuery string, conds ...interface{}) error

Joins performs a join query

func Last

func Last(ctx context.Context, db *gorm.DB, dest interface{}, conds ...interface{}) error

Last finds the last record ordered by primary key

func NewPostgresClient

func NewPostgresClient(ctx context.Context, cfg Config) (*gorm.DB, error)

NewPostgresClient creates a new GORM PostgreSQL client with optional OpenTelemetry instrumentation

func NewPostgresClientWithTracing

func NewPostgresClientWithTracing(ctx context.Context, cfg Config, serviceName, serviceVersion string) (*gorm.DB, error)

NewPostgresClientWithTracing creates a new PostgreSQL client with tracing enabled

func Order

func Order(ctx context.Context, db *gorm.DB, dest interface{}, orderBy string, conds ...interface{}) error

Order specifies order when retrieving records

func Ping

func Ping(ctx context.Context, db *gorm.DB, serviceName string) error

Ping tests the PostgreSQL connection with optional tracing

func Pluck

func Pluck(ctx context.Context, db *gorm.DB, model interface{}, column string, dest interface{}, conds ...interface{}) error

Pluck retrieves single column as slice

func Preload

func Preload(ctx context.Context, db *gorm.DB, dest interface{}, preloadAssociations []string, conds ...interface{}) error

Preload preloads associations

func Raw

func Raw(ctx context.Context, db *gorm.DB, dest interface{}, sql string, values ...interface{}) error

Raw executes raw SQL query

func Save

func Save(ctx context.Context, db *gorm.DB, value interface{}) error

Save saves all fields (insert or update)

func Select

func Select(ctx context.Context, db *gorm.DB, dest interface{}, fields []string, conds ...interface{}) error

Select specifies fields to retrieve

func SoftDelete

func SoftDelete(ctx context.Context, db *gorm.DB, model interface{}, conds ...interface{}) error

SoftDelete performs a soft delete (updates deleted_at timestamp) Note: Your model must have gorm.DeletedAt field

func Stats

func Stats(db *gorm.DB) (sql.DBStats, error)

Stats returns connection pool statistics

func Transaction

func Transaction(ctx context.Context, db *gorm.DB, fn func(*gorm.DB) error) error

Transaction executes a function within a database transaction

func Unscoped

func Unscoped(db *gorm.DB) *gorm.DB

Unscoped returns a query that includes soft-deleted records

func Update

func Update(ctx context.Context, db *gorm.DB, model interface{}, column string, value interface{}, conds ...interface{}) error

Update updates record(s) with given conditions

func UpdateColumn

func UpdateColumn(ctx context.Context, db *gorm.DB, model interface{}, column string, value interface{}, conds ...interface{}) error

UpdateColumn updates a single column (skips hooks and timestamp updates)

func UpdateColumns

func UpdateColumns(ctx context.Context, db *gorm.DB, model interface{}, values interface{}, conds ...interface{}) error

UpdateColumns updates multiple columns (skips hooks and timestamp updates)

func Updates

func Updates(ctx context.Context, db *gorm.DB, model interface{}, values interface{}, conds ...interface{}) error

Updates updates multiple columns

func Upsert

func Upsert(ctx context.Context, db *gorm.DB, value interface{}, conflictColumns []string, updateColumns []string) error

Upsert performs an insert or update (using ON CONFLICT)

Types

type Config

type Config struct {
	ConnectionString  string            // PostgreSQL connection string (DSN) (required)
	MaxOpenConns      int               // Maximum number of open connections (default: 100)
	MaxIdleConns      int               // Maximum number of idle connections (default: 10)
	ConnMaxLifetime   time.Duration     // Maximum lifetime of a connection (default: 1 hour)
	ConnMaxIdleTime   time.Duration     // Maximum idle time of a connection (default: 10 minutes)
	PrepareStmt       bool              // Enable prepared statement cache (default: true)
	EnableTracing     bool              // Enable OpenTelemetry tracing
	EnableMetrics     bool              // Enable OpenTelemetry metrics
	ServiceName       string            // Service name for tracing
	ServiceVersion    string            // Service version for tracing
	TracingAttributes map[string]string // Additional tracing attributes
	Logger            logger.Interface  // Custom GORM logger (optional)
}

Config holds the configuration for the PostgreSQL/GORM client

func (Config) Validate

func (cfg Config) Validate() error

Validate validates the PostgreSQL configuration

type PaginationResult

type PaginationResult struct {
	Data       interface{} `json:"data"`
	Total      int64       `json:"total"`
	Page       int         `json:"page"`
	PageSize   int         `json:"page_size"`
	TotalPages int         `json:"total_pages"`
}

PaginationResult holds pagination information along with results

func FindWithPaginationInfo

func FindWithPaginationInfo(ctx context.Context, db *gorm.DB, model interface{}, dest interface{}, page, pageSize int, conds ...interface{}) (*PaginationResult, error)

FindWithPaginationInfo finds records with complete pagination information

Jump to

Keyboard shortcuts

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