db

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithTx

func ContextWithTx(ctx context.Context, tx TxInterface) context.Context

ContextWithTx returns a new context with the transaction attached.

func Offset

func Offset(pageParam int64, pageSize uint64) uint64

Offset calculates the offset for pagination based on the provided page parameter and page size.

func PageSize

func PageSize(sizeParam int64) uint64

PageSize calculates the page size for pagination based on the provided size parameter.

func StreamReplicaRoutingInterceptor added in v1.4.0

func StreamReplicaRoutingInterceptor() grpc.StreamServerInterceptor

StreamReplicaRoutingInterceptor routes read-only streaming RPCs to the read-only replica database pool, provided that no active transaction exists in the context.

func TransactionMiddleware

func TransactionMiddleware(db DBClientInterface, logger logging.LoggerInterface) func(http.Handler) http.Handler

TransactionMiddleware creates a middleware that wraps each request in a database transaction. The transaction is committed if the handler completes successfully (status < 400). The transaction is rolled back if the handler returns an error or status >= 400.

func UnaryReplicaRoutingInterceptor added in v1.4.0

func UnaryReplicaRoutingInterceptor() grpc.UnaryServerInterceptor

UnaryReplicaRoutingInterceptor routes read-only unary RPCs to the read-only replica database pool, provided that no active transaction exists in the context.

func WithReadOnly added in v1.4.0

func WithReadOnly(ctx context.Context) context.Context

Types

type Config

type Config struct {
	DSN             string
	MaxConns        int32
	MinConns        int32
	MaxConnLifetime time.Duration
	MaxConnIdleTime time.Duration
	TracingEnabled  bool

	ReplicaDSN                string
	ReplicaMaxConns           int32
	ReplicaMinConns           int32
	ReplicaMaxConnLifetime    time.Duration
	ReplicaMaxConnIdleTime    time.Duration
	MaxReplicaLagMs           int64
	ReplicaPoolSizeMultiplier float64
}

type DBClient

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

func NewDBClient

func NewDBClient(cfg Config, tracer tracing.TracingInterface, monitor monitoring.MonitorInterface, logger logging.LoggerInterface) (*DBClient, error)

NewDBClient creates a new DBClient instance with the provided DSN and configuration options.

func (*DBClient) BeginTx

func (d *DBClient) BeginTx(ctx context.Context) (context.Context, TxInterface, error)

BeginTx starts a new transaction and returns a context with the transaction attached.

func (*DBClient) Close

func (d *DBClient) Close()

func (*DBClient) Statement

func (d *DBClient) Statement(ctx context.Context) sq.StatementBuilderType

Statement provides a StatementBuilderType configured to use the DBClient's database connection. If a transaction exists in the context, it will be used (created lazily on first use).

func (*DBClient) TxStatement

TxStatement provides a StatementBuilderType configured to use a transaction.

func (*DBClient) WithTx

func (d *DBClient) WithTx(ctx context.Context, fn func(context.Context) error) error

WithTx executes a function within a transaction context. The transaction is created lazily on first database access. If the function returns an error, the transaction is rolled back. Otherwise, the transaction is committed. If no database operations occurred, no transaction is created or committed.

type DBClientInterface

type DBClientInterface interface {
	Statement(context.Context) sq.StatementBuilderType
	TxStatement(context.Context) (TxInterface, sq.StatementBuilderType, error)
	BeginTx(context.Context) (context.Context, TxInterface, error)
	WithTx(context.Context, func(context.Context) error) error
	Close()
}

type LazyTxContextKey

type LazyTxContextKey struct{}

type ReadOnlyContextKey added in v1.4.0

type ReadOnlyContextKey struct{}

type TxContextKey

type TxContextKey struct{}

type TxInterface

type TxInterface interface {
	Commit() error
	Rollback() error
	sq.BaseRunner
}

func TxFromContext

func TxFromContext(ctx context.Context) TxInterface

TxFromContext extracts a transaction from the context, returning nil if none exists.

Jump to

Keyboard shortcuts

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