database

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection interface {
	Initialize() error
	Db() *gorm.DB
	DbRo() *gorm.DB
}

func NewPostgresqlConnection

func NewPostgresqlConnection(readWriteConfiguration *ConnectionConfiguration, readOnlyConfiguration *ConnectionConfiguration) (Connection, error)

type ConnectionConfiguration

type ConnectionConfiguration struct {
	Host                      string
	Port                      string
	User                      string
	Password                  string
	Database                  string
	MaxIdleConnections        int
	MaxOpenConnections        int
	MaxIdleConnectionLifetime time.Duration
	MaxConnectionLifetime     time.Duration
	*gorm.Config
}

type JSONB

type JSONB map[string]any

JSONB is a custom type for handling JSONB fields in PostgreSQL It implements sql.Scanner and driver.Valuer interfaces for automatic JSON marshaling/unmarshaling with GORM

func (JSONB) GormDataType

func (JSONB) GormDataType() string

GormDataType tells GORM what SQL data type to use for this field

func (JSONB) MarshalJSON

func (j JSONB) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for JSONB This ensures JSONB serializes as a nested JSON object rather than base64-encoded bytes

func (*JSONB) Scan

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

Scan implements the sql.Scanner interface for reading from the database

func (*JSONB) UnmarshalJSON

func (j *JSONB) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler for JSONB This allows JSONB to be deserialized from nested JSON objects

func (JSONB) Value

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

Value implements the driver.Valuer interface for writing to the database

type JSONBG

type JSONBG[T any] struct {
	V T
}

JSONBG is a generic JSONB wrapper that unmarshals into the concrete type parameter T. It implements sql.Scanner and driver.Valuer, and also json.Marshaler/json.Unmarshaler so it can be used seamlessly with GORM and HTTP JSON payloads.

Example:

type MyCfg struct { Enabled bool `json:"enabled"` }
type Model struct { Cfg JSONBG[MyCfg] `gorm:"type:jsonb"` }

In the database this is stored as jsonb, and when scanning it will populate MyCfg. When marshaling to JSON (e.g. API responses), it serializes the inner value of type T.

Note: The zero value contains the zero value of T. If T is a pointer type, the zero value will be nil and NULL may be written to DB. If you want an empty object `{}` instead, use a non-pointer struct type for T.

This type avoids map[string]any when a strongly-typed struct is desired.

func (*JSONBG[T]) GormDataType

func (*JSONBG[T]) GormDataType() string

GormDataType tells GORM what SQL data type to use for this field

func (*JSONBG[T]) MarshalJSON

func (j *JSONBG[T]) MarshalJSON() ([]byte, error)

MarshalJSON delegates to the inner value.

func (*JSONBG[T]) Scan

func (j *JSONBG[T]) Scan(value interface{}) error

Scan implements sql.Scanner: it unmarshals the DB value into the inner value.

func (*JSONBG[T]) UnmarshalJSON

func (j *JSONBG[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON delegates to the inner value.

func (*JSONBG[T]) Value

func (j *JSONBG[T]) Value() (driver.Value, error)

Value implements driver.Valuer: it marshals the inner value to JSON.

type PostgresqlConnection

type PostgresqlConnection struct {
	ReadWriteConfiguration *ConnectionConfiguration
	ReadOnlyConfiguration  *ConnectionConfiguration
	// contains filtered or unexported fields
}

func (*PostgresqlConnection) Db

func (p *PostgresqlConnection) Db() *gorm.DB

func (*PostgresqlConnection) DbRo

func (p *PostgresqlConnection) DbRo() *gorm.DB

func (*PostgresqlConnection) Initialize

func (p *PostgresqlConnection) Initialize() error

type Repository

type Repository interface {
	Initialize(connection Connection) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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