Documentation
¶
Index ¶
- func ContextWithTx(ctx context.Context, tx TxInterface) context.Context
- func Offset(pageParam int64, pageSize uint64) uint64
- func PageSize(sizeParam int64) uint64
- func TransactionMiddleware(db DBClientInterface, logger logging.LoggerInterface) func(http.Handler) http.Handler
- type Config
- type DBClient
- func (d *DBClient) BeginTx(ctx context.Context) (context.Context, TxInterface, error)
- func (d *DBClient) Close()
- func (d *DBClient) Statement(ctx context.Context) sq.StatementBuilderType
- func (d *DBClient) TxStatement(ctx context.Context) (TxInterface, sq.StatementBuilderType, error)
- func (d *DBClient) WithTx(ctx context.Context, fn func(context.Context) error) error
- type DBClientInterface
- type LazyTxContextKey
- type TxContextKey
- type TxInterface
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 ¶
Offset calculates the offset for pagination based on the provided page parameter and page size.
func PageSize ¶
PageSize calculates the page size for pagination based on the provided size parameter.
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.
Types ¶
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 ¶
BeginTx starts a new transaction and returns a context with the transaction attached.
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 ¶
func (d *DBClient) TxStatement(ctx context.Context) (TxInterface, sq.StatementBuilderType, error)
TxStatement provides a StatementBuilderType configured to use a transaction.
func (*DBClient) WithTx ¶
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 LazyTxContextKey ¶
type LazyTxContextKey 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.