Documentation
¶
Index ¶
- func AddColumn(db *gorm.DB, model interface{}, column string) error
- func AutoMigrate(db *gorm.DB, models ...interface{}) error
- func Close(ctx context.Context, db *gorm.DB, serviceName string) error
- func Count(ctx context.Context, db *gorm.DB, model interface{}, count *int64, ...) error
- func Create(ctx context.Context, db *gorm.DB, value interface{}) error
- func CreateInBatches(ctx context.Context, db *gorm.DB, value interface{}, batchSize int) error
- func CreateIndex(db *gorm.DB, model interface{}, indexName string) error
- func CreateTable(db *gorm.DB, model interface{}) error
- func Delete(ctx context.Context, db *gorm.DB, model interface{}, conds ...interface{}) error
- func Distinct(ctx context.Context, db *gorm.DB, model interface{}, dest interface{}, ...) error
- func DropColumn(db *gorm.DB, model interface{}, column string) error
- func DropIndex(db *gorm.DB, model interface{}, indexName string) error
- func DropTable(db *gorm.DB, model interface{}) error
- func Exec(ctx context.Context, db *gorm.DB, sql string, values ...interface{}) error
- func Exists(ctx context.Context, db *gorm.DB, model interface{}, conds ...interface{}) (bool, error)
- func Find(ctx context.Context, db *gorm.DB, dest interface{}, conds ...interface{}) error
- func FindInBatches(ctx context.Context, db *gorm.DB, dest interface{}, batchSize int, ...) error
- func FindOne(ctx context.Context, db *gorm.DB, dest interface{}, conds ...interface{}) error
- func FindWithPagination(ctx context.Context, db *gorm.DB, dest interface{}, page, pageSize int, ...) error
- func First(ctx context.Context, db *gorm.DB, dest interface{}, conds ...interface{}) error
- func FirstOrCreate(ctx context.Context, db *gorm.DB, dest interface{}, conds ...interface{}) error
- func FirstOrInit(ctx context.Context, db *gorm.DB, dest interface{}, conds ...interface{}) error
- func GetDB(gormDB *gorm.DB) (*sql.DB, error)
- func GetTracedClient(ctx context.Context, cfg Config) (*gorm.DB, error)
- func Group(ctx context.Context, db *gorm.DB, dest interface{}, groupBy string, ...) error
- func HasTable(db *gorm.DB, model interface{}) bool
- func Joins(ctx context.Context, db *gorm.DB, dest interface{}, joinQuery string, ...) error
- func Last(ctx context.Context, db *gorm.DB, dest interface{}, conds ...interface{}) error
- func NewPostgresClient(ctx context.Context, cfg Config) (*gorm.DB, error)
- func NewPostgresClientWithTracing(ctx context.Context, cfg Config, serviceName, serviceVersion string) (*gorm.DB, error)
- func Order(ctx context.Context, db *gorm.DB, dest interface{}, orderBy string, ...) error
- func Ping(ctx context.Context, db *gorm.DB, serviceName string) error
- func Pluck(ctx context.Context, db *gorm.DB, model interface{}, column string, ...) error
- func Preload(ctx context.Context, db *gorm.DB, dest interface{}, ...) error
- func Raw(ctx context.Context, db *gorm.DB, dest interface{}, sql string, ...) error
- func Save(ctx context.Context, db *gorm.DB, value interface{}) error
- func Select(ctx context.Context, db *gorm.DB, dest interface{}, fields []string, ...) error
- func SoftDelete(ctx context.Context, db *gorm.DB, model interface{}, conds ...interface{}) error
- func Stats(db *gorm.DB) (sql.DBStats, error)
- func Transaction(ctx context.Context, db *gorm.DB, fn func(*gorm.DB) error) error
- func Unscoped(db *gorm.DB) *gorm.DB
- func Update(ctx context.Context, db *gorm.DB, model interface{}, column string, ...) error
- func UpdateColumn(ctx context.Context, db *gorm.DB, model interface{}, column string, ...) error
- func UpdateColumns(ctx context.Context, db *gorm.DB, model interface{}, values interface{}, ...) error
- func Updates(ctx context.Context, db *gorm.DB, model interface{}, values interface{}, ...) error
- func Upsert(ctx context.Context, db *gorm.DB, value interface{}, conflictColumns []string, ...) error
- type Config
- type PaginationResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AutoMigrate ¶
AutoMigrate runs auto migration for given models
func Count ¶
func Count(ctx context.Context, db *gorm.DB, model interface{}, count *int64, conds ...interface{}) error
Count counts records matching given conditions
func CreateInBatches ¶
CreateInBatches creates multiple records in batches
func CreateIndex ¶
CreateIndex creates an index
func CreateTable ¶
CreateTable creates table for given model
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 ¶
DropColumn drops a column from table
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 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 FindWithPagination ¶
func FindWithPagination(ctx context.Context, db *gorm.DB, dest interface{}, page, pageSize int, conds ...interface{}) error
FindWithPagination finds records with pagination
func FirstOrCreate ¶
FirstOrCreate finds the first matched record or creates a new one
func FirstOrInit ¶
FirstOrInit finds the first matched record or initializes a new one
func GetTracedClient ¶
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 Joins ¶
func Joins(ctx context.Context, db *gorm.DB, dest interface{}, joinQuery string, conds ...interface{}) error
Joins performs a join query
func NewPostgresClient ¶
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 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 Select ¶
func Select(ctx context.Context, db *gorm.DB, dest interface{}, fields []string, conds ...interface{}) error
Select specifies fields to retrieve
func SoftDelete ¶
SoftDelete performs a soft delete (updates deleted_at timestamp) Note: Your model must have gorm.DeletedAt field
func Transaction ¶
Transaction executes a function within a database transaction
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)
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
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