db

package
v0.0.0-...-8414c70 Latest Latest
Warning

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

Go to latest
Published: May 28, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

package db defines the interface to interact with the database, and its implementations

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUniqueViolation = pgconn.PgError{
		Code: pgerrcode.UniqueViolation,
	}
)

Functions

func GetPgError

func GetPgError(err error) *pgconn.PgError

func IsError

func IsError(err error) bool

func IsNoRowsError

func IsNoRowsError(err error) bool

func NewBool

func NewBool(b *bool) pgtype.Bool

NewBool converts a bool pointer to a pgtype.Bool

func NewError

func NewError(err error) error

func NewInt8

func NewInt8(i *int64) pgtype.Int8

NewInt8 converts an int64 pointer to a pgtype.Int8

func NewPgxPool

func NewPgxPool(ctx context.Context, config string) (*pgxpool.Pool, error)

func NewText

func NewText(s *string) pgtype.Text

Functions below are used to convert go types to a nullable sql field NewText converts a string pointer to a pgtype.Text

func NewTimestamp

func NewTimestamp(t *time.Time) pgtype.Timestamp

NewTimestamp converts a time.Time pointer to a pgtype.Timestamp

Types

type AfterCreateUserFunc

type AfterCreateUserFunc func(user User) (any, error)

type CreateUserTxParams

type CreateUserTxParams struct {
	CreateUserParams
	AfterCreate AfterCreateUserFunc
}

CreateUserTxParams is the input parameters for the create user function

type CreateUserTxResult

type CreateUserTxResult struct {
	User              User `json:"user"`
	AfterCreateResult any  `json:"result"`
}

CreateUserTxResult is the result of the create user function

type Error

type Error struct {
	Err    error
	Status int
}

func GetError

func GetError(err error) *Error

func (*Error) Error

func (de *Error) Error() string

type PostgresStore

type PostgresStore struct {
	*Queries
	// contains filtered or unexported fields
}

PostgresStore is the implementation of the Store interface that uses the postgres database

func NewPostgresStore

func NewPostgresStore(pool *pgxpool.Pool) *PostgresStore

func (*PostgresStore) Check

func (s *PostgresStore) Check(ctx context.Context) error

Check returns nil if it can successfully talk to the database. It returns a non-nil error otherwise.

func (*PostgresStore) Close

func (s *PostgresStore) Close()

Close closes all connections in the pool

func (*PostgresStore) CreateUserTx

func (s *PostgresStore) CreateUserTx(
	ctx context.Context,
	arg CreateUserTxParams,
) (CreateUserTxResult, error)

func (*PostgresStore) ExecuteInTransaction

func (s *PostgresStore) ExecuteInTransaction(ctx context.Context, fn QueryFunc) error

func (*PostgresStore) TransferTx

func (s *PostgresStore) TransferTx(
	ctx context.Context,
	arg TransferTxParams,
) (TransferTxResult, error)

TransferTx performs a money transfer from one account to the other It creates a transfer record, create an entry for both accounts, and update accounts' balance within a single database transaction

type QueryFunc

type QueryFunc = func(*db_generated.Queries) error

type Store

type Store interface {
	db_generated.Querier
	CreateUserTx(ctx context.Context, arg CreateUserTxParams) (CreateUserTxResult, error)
	TransferTx(ctx context.Context, arg TransferTxParams) (TransferTxResult, error)
	ExecuteInTransaction(ctx context.Context, fn QueryFunc) error
	Check(ctx context.Context) error
}

Store defines all functions to execute db queries and transactions

type TransferTxParams

type TransferTxParams struct {
	FromAccountId int64 `json:"from_account_id"`
	ToAccountId   int64 `json:"to_account_id"`
	Amount        int64 `json:"amount"`
}

TransferTxParams is the input parameters for the transfer transaction

type TransferTxResult

type TransferTxResult struct {
	Transfer    Transfer `json:"transfer"`
	FromAccount Account  `json:"from_account"`
	ToAccount   Account  `json:"to_account"`
	FromEntry   Entry    `json:"from_entry"`
	ToEntry     Entry    `json:"to_entry"`
}

TransferTxResult is the result of the transfer transaction

Jump to

Keyboard shortcuts

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