database

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeRow

func DecodeRow[T any](row map[string]any) (T, error)

DecodeRow decodes a single map[string]any row (from DaprDB.Query) into a struct T by round-tripping through JSON. T's fields must carry `json` tags that match the SQL column names returned by the query.

func DecodeRows

func DecodeRows[T any](rows []map[string]any) ([]T, error)

DecodeRows decodes a slice of map[string]any rows into []T. Returns an empty (non-nil) slice when rows is empty.

Types

type DB

type DB interface {
	// Query executes a SQL statement and returns the result rows as a slice of maps.
	Query(ctx context.Context, sql string, params ...any) ([]map[string]any, error)

	// Exec executes a SQL statement that produces no result rows.
	Exec(ctx context.Context, sql string, params ...any) error

	// Ping verifies the underlying connection is reachable.
	Ping(ctx context.Context) error
}

DB abstracts data access so that the application can swap between Dapr binding (production) and a direct pgx pool (tests) transparently.

type DaprDB

type DaprDB struct {
	// contains filtered or unexported fields
}

DaprDB executes SQL via a Dapr PostgreSQL output binding. The binding reads its connection string from a K8s Secret managed by ESO, so credential rotation is transparent to the application.

func NewDaprDB

func NewDaprDB(client dapr.Client, bindingName string) *DaprDB

NewDaprDB creates a DaprDB backed by the named Dapr binding component.

func (*DaprDB) Exec

func (d *DaprDB) Exec(ctx context.Context, sql string, params ...any) error

Exec executes a SQL statement that produces no result rows. Use this for INSERT/UPDATE/DELETE without RETURNING.

func (*DaprDB) Ping

func (d *DaprDB) Ping(ctx context.Context) error

Ping verifies the binding is reachable by executing a trivial statement. Uses exec (not query) to avoid JSON parsing overhead on a connectivity check.

func (*DaprDB) Query

func (d *DaprDB) Query(ctx context.Context, sql string, params ...any) ([]map[string]any, error)

Query executes a SQL statement and returns the result rows as a slice of maps. Use this for SELECT statements and for INSERT/UPDATE … RETURNING.

The Dapr PostgreSQL binding v1 returns positional arrays [[val1, val2], ...]. To recover column names we wrap every query with json_agg(row_to_json(t))::text, which makes PostgreSQL serialise the result set as a single JSON text value. The binding then returns [[jsonText]] and we decode that inner JSON into []map[string]any — the same shape the rest of the codebase expects.

Jump to

Keyboard shortcuts

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