pg

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package pg provides a PostgreSQL connection via GORM.

Open returns a *Storage wrapping a configured *gorm.DB. The package also offers helpers for JSONB conversion (ToJsonb/FromJsonb/MapToJsonb), null string handling and reusable query scopes (Paging, OrderBy*, WhereStrings, Merge, …).

Index

Examples

Constants

View Source
const (
	ErrCodePostgresOpen = "PG-001"
	ErrCodePgSetJsonb   = "PG-003"
	ErrCodePgGetJsonb   = "PG-004"
)
View Source
const (
	PageSizeMaxLimit = 100
	PageSizeDefault  = 20
)

Variables

View Source
var (
	ErrPostgresOpen = func(cause error) error {
		return jet.NewAppErrBuilder(ErrCodePostgresOpen, "").Wrap(cause).Err()
	}
	ErrPgSetJsonb = func(cause error) error {
		return jet.NewAppErrBuilder(ErrCodePgSetJsonb, "set JSONB").Wrap(cause).Err()
	}
	ErrPgGetJsonb = func(cause error) error {
		return jet.NewAppErrBuilder(ErrCodePgGetJsonb, "get JSONB").Wrap(cause).Err()
	}
)

Functions

func FromJsonb

func FromJsonb[T any](j *JSONB) (*T, error)

FromJsonb decodes a jsonb value into T.

func Merge

func Merge() func(*gorm.DB) *gorm.DB

func NullToString

func NullToString(s *string) string

NullToString transforms NULL to empty string

func OrderByCreatedAt

func OrderByCreatedAt(desc bool) func(*gorm.DB) *gorm.DB

func OrderByUpdatedAt

func OrderByUpdatedAt(desc bool) func(*gorm.DB) *gorm.DB

func Paging

func Paging(rq jet.PagingRequest) func(*gorm.DB) *gorm.DB

func PagingLimit

func PagingLimit(rqLimit int) int

func Single

func Single() func(*gorm.DB) *gorm.DB

func StringToNull

func StringToNull(s string) *string

StringToNull transforms empty string to nil string, so that gorm stores it as NULL

func Update

func Update() func(*gorm.DB) *gorm.DB

func WhereStrings

func WhereStrings(field string, values []string) func(*gorm.DB) *gorm.DB

Types

type DbClusterConfig

type DbClusterConfig struct {
	Master *DbConfig // Master database
	Slave  *DbConfig // Slave database
}

DbClusterConfig configuration of database cluster

type DbConfig

type DbConfig struct {
	ConnectionString string `mapstructure:"connection_string"` // ConnectionString if specified overrides all other connection params
	User             string
	Password         string
	DBName           string
	Port             string
	Host             string
}

DbConfig database configuration

type GormDto

type GormDto struct {
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *gorm.DeletedAt
}

GormDto specifies base attrs for GORM dto

type JSONB

type JSONB []byte

JSONB is a PostgreSQL jsonb value backed by raw JSON bytes. It implements driver.Valuer and sql.Scanner so GORM stores and loads it as a jsonb column.

func GetEmptyJson

func GetEmptyJson() (*JSONB, error)

GetEmptyJson returns an empty jsonb value ("{}").

func MapToJsonb

func MapToJsonb[T comparable, K any](payload map[T]K) (*JSONB, error)

MapToJsonb converts a map to jsonb.

func ToJsonb

func ToJsonb[T any](payload *T) (*JSONB, error)

ToJsonb converts an arbitrary object to jsonb.

func (JSONB) GormDataType

func (JSONB) GormDataType() string

GormDataType tells GORM to use the jsonb column type.

func (*JSONB) Scan

func (j *JSONB) Scan(src interface{}) error

Scan implements sql.Scanner.

func (JSONB) Value

func (j JSONB) Value() (driver.Value, error)

Value implements driver.Valuer.

type Storage

type Storage struct {
	Instance *gorm.DB
	DBName   string
	// contains filtered or unexported fields
}

func Open

func Open(config *DbConfig, logger jet.CLoggerFunc) (*Storage, error)
Example
package main

import (
	"github.com/zloevil/jet"
	"github.com/zloevil/jet/storages/pg"
)

func main() {
	logFn := func() jet.CLogger { return jet.L(jet.InitLogger(&jet.LogConfig{Level: jet.InfoLevel})) }

	db, err := pg.Open(&pg.DbConfig{
		Host:     "localhost",
		Port:     "5432",
		User:     "app",
		Password: "secret",
		DBName:   "app",
	}, logFn)
	if err != nil {
		return
	}
	defer db.Close()

	// db.Instance is the configured *gorm.DB
	_ = db.Instance
}

func (*Storage) Close

func (s *Storage) Close()

type TotalCount

type TotalCount struct {
	TotalCount int `gorm:"column:total"`
}

Jump to

Keyboard shortcuts

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