Documentation ¶
Overview ¶
Package database provides a layer for interacting with read/write databases
Index ¶
Constants ¶
const ( MySQLDriver = "mysql" PostgreSQLDriver = "postgresql" )
Database constants
Variables ¶
This section is empty.
Functions ¶
func CloseAllConnections ¶
func CloseAllConnections()
CloseAllConnections closes the current database connections
func OpenConnection ¶
func OpenConnection() (err error)
OpenConnection opens the database connection (read / write)
func SetConfiguration ¶
func SetConfiguration(conf Configuration)
SetConfiguration sets the configuration
Types ¶
type APIDatabase ¶ added in v0.1.7
type APIDatabase struct { *sql.DB // calls are passed through by default to me // contains filtered or unexported fields }
APIDatabase Extends sql.DB
var ( ReadDatabase *APIDatabase WriteDatabase *APIDatabase )
Global database instances
func NewAPIDatabase ¶ added in v0.1.7
func NewAPIDatabase(read, write *sql.DB) *APIDatabase
NewAPIDatabase creates a new database connection
func (*APIDatabase) Close ¶ added in v0.1.7
func (d *APIDatabase) Close()
Close both or any connections
func (*APIDatabase) Enque ¶ added in v0.1.7
func (d *APIDatabase) Enque(handle func())
Enque adds a worker
func (*APIDatabase) GetReadDatabase ¶ added in v0.1.7
func (d *APIDatabase) GetReadDatabase() *sql.DB
GetReadDatabase gets the read database connection these are needed for testing because for some reason it can't determine that DeliveryDudesDB extends sql.DB
func (*APIDatabase) GetWriteDatabase ¶ added in v0.1.7
func (d *APIDatabase) GetWriteDatabase() *sql.DB
GetWriteDatabase gets the "write database" connection
func (*APIDatabase) StopWorker ¶ added in v0.1.7
func (d *APIDatabase) StopWorker()
StopWorker will wait for the queue to empty and then shutdown the worker
type Configuration ¶
type Configuration struct { DatabaseRead ConnectionConfig `json:"database_read" mapstructure:"database_read"` // Read database connection DatabaseWrite ConnectionConfig `json:"database_write" mapstructure:"database_write"` // Write database connection }
Configuration is the database configuration
type ConnectionConfig ¶
type ConnectionConfig struct { Driver string `json:"driver" mapstructure:"driver"` // mysql or postgresql Host string `json:"host" mapstructure:"host"` // localhost MaxConnectionTime int `json:"max_connection_time" mapstructure:"max_connection_time"` // 60 MaxIdleConnections int `json:"max_idle_connections" mapstructure:"max_idle_connections"` // 5 MaxOpenConnections int `json:"max_open_connections" mapstructure:"max_open_connections"` // 5 Name string `json:"name" mapstructure:"name"` // database-name Password string `json:"password" mapstructure:"password"` // user-password Port string `json:"port" mapstructure:"port"` // 3306 User string `json:"user" mapstructure:"user"` // username }
ConnectionConfig is a configuration for a SQL connection