Documentation
¶
Overview ¶
Package postgres contains the domain concept definitions needed to support SuperMQ PostgreSQL database functionality.
It provides the abstraction of the PostgreSQL database service, which is used to configure, setup and connect to the PostgreSQL database.
Index ¶
- func Connect(cfg Config) (*sqlx.DB, error)
- func CreateMetadataQuery(entity string, um map[string]any) (string, []byte, error)
- func HandleError(wrapper, err error) error
- func NewErrorHandler(opts ...errors.HandlerOption) errors.Handler
- func Setup(cfg Config, migrations migrate.MemoryMigrationSource) (*sqlx.DB, error)
- func Total(ctx context.Context, db Database, query string, params any) (uint64, error)
- func WithDuplicateErrors(mapper errors.Mapper) errors.HandlerOption
- type Config
- type Database
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Connect ¶
Connect creates a connection to the PostgreSQL instance.
For example:
db, err := postgres.Connect(postgres.Config{})
func CreateMetadataQuery ¶
CreateMetadataQuery creates a query to filter by metadata.
For example:
query, param, err := CreateMetadataQuery("", map[string]any{
"key": "value",
})
func HandleError ¶
HandleError handles the error and returns a wrapped error. It checks the error code and returns a specific error.
func NewErrorHandler ¶ added in v0.18.4
func NewErrorHandler(opts ...errors.HandlerOption) errors.Handler
func Setup ¶
Setup creates a connection to the PostgreSQL instance and applies any unapplied database migrations. A non-nil error is returned to indicate failure.
For example:
db, err := postgres.Setup(postgres.Config{}, migrate.MemoryMigrationSource{})
func Total ¶
Total returns the total number of rows.
For example:
total, err := Total(ctx, db, "SELECT COUNT(*) FROM table", nil)
func WithDuplicateErrors ¶ added in v0.18.4
func WithDuplicateErrors(mapper errors.Mapper) errors.HandlerOption
Types ¶
type Config ¶
type Config struct {
Host string `env:"HOST" envDefault:"localhost"`
Port string `env:"PORT" envDefault:"5432"`
User string `env:"USER" envDefault:"supermq"`
Pass string `env:"PASS" envDefault:"supermq"`
Name string `env:"NAME" envDefault:""`
SSLMode string `env:"SSL_MODE" envDefault:"disable"`
SSLCert string `env:"SSL_CERT" envDefault:""`
SSLKey string `env:"SSL_KEY" envDefault:""`
SSLRootCert string `env:"SSL_ROOT_CERT" envDefault:""`
}
type Database ¶
type Database interface {
// NamedQueryContext executes a named query against the database and returns
NamedQueryContext(context.Context, string, any) (*sqlx.Rows, error)
// NamedExecContext executes a named query against the database and returns
NamedExecContext(context.Context, string, any) (sql.Result, error)
// QueryRowxContext queries the database and returns an *sqlx.Row.
QueryRowxContext(context.Context, string, ...any) *sqlx.Row
// QueryxContext queries the database and returns an *sqlx.Rows and an error.
QueryxContext(context.Context, string, ...any) (*sqlx.Rows, error)
// QueryContext queries the database and returns an *sql.Rows and an error.
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
// ExecContext executes a query without returning any rows.
ExecContext(context.Context, string, ...any) (sql.Result, error)
// BeginTxx begins a transaction and returns an *sqlx.Tx.
BeginTxx(ctx context.Context, opts *sql.TxOptions) (*sqlx.Tx, error)
}
Database provides a database interface.