database

package
v0.0.0-...-c801cc5 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package database contains Database interface implementation for certain database.

Here is a PostgreSQL implementation.

To connect to the Postgres database call method `NewPostgresClient()` that will initiate database connection

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PostgresClient

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

PostgresClient is a database communication manager

func NewPostgresClient

func NewPostgresClient(ctx context.Context, options string, wait bool) (*PostgresClient, error)

NewPostgresClient create a new database communication manager

- ctx: context of the database client. Can be used to interrupt connection wait loop or long transactions; - options: database connection options. Please provide a string of options in format `host=localhost port=5432 ...`. For more information about possible options see [Database Connection Control Functions](https://www.postgresql.org/docs/current/libpq-connect.html); - wait: describes will the app wait until the database will up or fails after first unsuccessful ping. Useful for orchestration environments like K8s or Docker Compose or Swarm to wait when the database container of proxy will up.

func (*PostgresClient) CreateAccount

func (pg *PostgresClient) CreateAccount(a model.Account) (*model.Account, error)

CreateAccount creates a new account.

If the account already exists, the method will return `model.ErrRowExists` error

func (*PostgresClient) CreatePayment

func (pg *PostgresClient) CreatePayment(p model.Payment, lastChangedFrom, lastChangedTo *time.Time) (*model.Payment, error)

CreatePayment tries to create a financial transaction Concurrent data access is managed by means of MVCC (Multiversion Concurrency Control) In case of any inconsistency, race condition or any other concurrency problem it raises an error

func (*PostgresClient) GetAccount

func (pg *PostgresClient) GetAccount(accountID string) (*model.Account, error)

GetAccount returns an existing account.

The view v_accounts calculates a sum of account balance and following payments affecting this account.

To improve database performance you can periodically calculate a sum op payments related to each account and update its fields `balance` and `balance_date`. Thus, the payments older than balance_date will not be affected in aggregations anymore. All dates should be in UTC+0.

func (*PostgresClient) GetAllAccounts

func (pg *PostgresClient) GetAllAccounts() ([]model.Account, error)

GetAllAccounts returns a list of existing accounts.

The view v_accounts calculates a sum of account balance and following payments affecting this account.

To improve database performance you can periodically calculate a sum op payments related to each account and update its fields `balance` and `balance_date`. Thus, the payments older than balance_date will not be affected in aggregations anymore. All dates should be in UTC+0.

func (*PostgresClient) GetAllPayments

func (pg *PostgresClient) GetAllPayments() ([]model.Payment, error)

GetAllPayments returns a list of existing payments in historical order

Since the payment doesn't contain currency code, it will be received from the corresponding payer account

Jump to

Keyboard shortcuts

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