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 ¶
- Constants
- Variables
- func FromJsonb[T any](j *JSONB) (*T, error)
- func Merge() func(*gorm.DB) *gorm.DB
- func NullToString(s *string) string
- func OrderByCreatedAt(desc bool) func(*gorm.DB) *gorm.DB
- func OrderByUpdatedAt(desc bool) func(*gorm.DB) *gorm.DB
- func Paging(rq jet.PagingRequest) func(*gorm.DB) *gorm.DB
- func PagingLimit(rqLimit int) int
- func Single() func(*gorm.DB) *gorm.DB
- func StringToNull(s string) *string
- func Update() func(*gorm.DB) *gorm.DB
- func WhereStrings(field string, values []string) func(*gorm.DB) *gorm.DB
- type DbClusterConfig
- type DbConfig
- type GormDto
- type JSONB
- type Storage
- type TotalCount
Examples ¶
Constants ¶
const ( ErrCodePostgresOpen = "PG-001" ErrCodePgSetJsonb = "PG-003" ErrCodePgGetJsonb = "PG-004" )
const ( PageSizeMaxLimit = 100 PageSizeDefault = 20 )
Variables ¶
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 NullToString ¶
NullToString transforms NULL to empty string
func PagingLimit ¶
func StringToNull ¶
StringToNull transforms empty string to nil string, so that gorm stores it as NULL
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 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 ¶
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 (JSONB) GormDataType ¶
GormDataType tells GORM to use the jsonb column type.
type Storage ¶
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
}
Output:
type TotalCount ¶
type TotalCount struct {
TotalCount int `gorm:"column:total"`
}