Documentation ¶
Overview ¶
package db defines the interface to interact with the database, and its implementations
Index ¶
- Variables
- func GetPgError(err error) *pgconn.PgError
- func IsError(err error) bool
- func IsNoRowsError(err error) bool
- func NewBool(b *bool) pgtype.Bool
- func NewError(err error) error
- func NewInt8(i *int64) pgtype.Int8
- func NewPgxPool(ctx context.Context, config string) (*pgxpool.Pool, error)
- func NewText(s *string) pgtype.Text
- func NewTimestamp(t *time.Time) pgtype.Timestamp
- type AfterCreateUserFunc
- type CreateUserTxParams
- type CreateUserTxResult
- type Error
- type PostgresStore
- func (s *PostgresStore) Check(ctx context.Context) error
- func (s *PostgresStore) Close()
- func (s *PostgresStore) CreateUserTx(ctx context.Context, arg CreateUserTxParams) (CreateUserTxResult, error)
- func (s *PostgresStore) ExecuteInTransaction(ctx context.Context, fn QueryFunc) error
- func (s *PostgresStore) TransferTx(ctx context.Context, arg TransferTxParams) (TransferTxResult, error)
- type QueryFunc
- type Store
- type TransferTxParams
- type TransferTxResult
Constants ¶
This section is empty.
Variables ¶
var ( ErrUniqueViolation = pgconn.PgError{ Code: pgerrcode.UniqueViolation, } )
Functions ¶
func GetPgError ¶
func IsNoRowsError ¶
Types ¶
type AfterCreateUserFunc ¶
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 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