database

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package database provides database agnostic helpers.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrResourceNotFound is an error with value "resource not found".
	ErrResourceNotFound = errors.New("resource not found")
	// ErrResourceConflict is an error with value "resource unique value already used".
	ErrResourceConflict = errors.New("resource conflicts with existing resource")
)

Functions

func WrapInSyncTx

func WrapInSyncTx[TTx MinimalDBTx, TResult any](
	ctx context.Context,
	tx *SyncTx[TTx],
	queryFunc func(ctx context.Context) (TResult, error),
) (TResult, error)

WrapInSyncTx is used to make sure a transactional database action can run concurrently.

func WrapInSyncTxNoError

func WrapInSyncTxNoError[TTx MinimalDBTx, TResult any](
	ctx context.Context,
	tx *SyncTx[TTx],
	queryFunc func(ctx context.Context) TResult,
) TResult

WrapInSyncTxNoError is used to make sure a transactional database action can run concurrently. The executed database action shouldn't return an error.

func WrapInSyncTxNoReturn

func WrapInSyncTxNoReturn[TTx MinimalDBTx](
	ctx context.Context,
	tx *SyncTx[TTx],
	queryFunc func(ctx context.Context),
)

WrapInSyncTxNoReturn is used to make sure a transactional database action can run concurrently. The executed database action shouldn't return anything.

func WrapWithSpan

func WrapWithSpan[T any](
	ctx context.Context,
	dbName string,
	queryFunc func(ctx context.Context, sql string, args ...any) (T, error),
	sql string, args ...any) (T, error)

WrapWithSpan is used to wrap a database action in a sentry.Span.

func WrapWithSpanNoError

func WrapWithSpanNoError[T any](
	ctx context.Context,
	dbName string,
	queryFunc func(ctx context.Context, sql string, args ...any) T,
	sql string, args ...any) T

WrapWithSpanNoError is used to wrap a database action in a sentry.Span. The executed database action shouldn't return an error.

Types

type MinimalDBTx

type MinimalDBTx interface {
	Rollback(ctx context.Context) error
}

MinimalDBTx is the minimal interface a DB transaction should implement.

type SyncTx

type SyncTx[TTx MinimalDBTx] struct {
	sync.Mutex
	Tx TTx
}

SyncTx wraps a database transaction to make sure it can be used concurrently. This is achieved by locking the transaction when in use.

func CreateSyncTx

func CreateSyncTx[TTx MinimalDBTx](
	ctx context.Context,
	beginTxFunc func(ctx context.Context) (TTx, error),
) *SyncTx[TTx]

CreateSyncTx creates a SyncTx which makes sure a database transaction can be used concurrently.

func (*SyncTx[TTx]) Rollback

func (tx *SyncTx[TTx]) Rollback(ctx context.Context) error

Rollback is used to rollback the wrapped transaction.

Directories

Path Synopsis
Package postgres provides several github.com/jackc/pgx related helpers for interacting with postgres.
Package postgres provides several github.com/jackc/pgx related helpers for interacting with postgres.

Jump to

Keyboard shortcuts

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