database

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package database provide structs and methods to create database connection configurations.

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = new(DatabaseConfig)

DefaultConfig is the default database configuration, whose values come from environment variables with default values if the environment variables are not provided to the value used in a testing setup. - CONFIG_DBUSER: database username - CONFIG_DBPASS: database user password - CONFIG_DBHOST: database host - CONFIG_DBPORT: database port - CONFIG_DBNAME: database name - CONFIG_DBSSLMODE: ssl mode to use - CONFIG_DBAPPNAME: application name - CONFIG_DBTIMEOUT: connection timeout

View Source
var ErrNoConfig = errors.NewKind("database: can't get database with no configuration")

ErrNoConfig is returned when there is an attempt of getting a databsse connection with no configuration.

Functions

func Default

func Default(configurators ...ConfigFunc) (*sql.DB, error)

Default returns a database connection established using the default configuration.

func Get

func Get(config *DatabaseConfig, configurators ...ConfigFunc) (*sql.DB, error)

Get returns a database connection with the configuration resultant of applying the given configfuncs to the config. Passing a nil configuration will result in an error.

func Must

func Must(db *sql.DB, err error) *sql.DB

Must will panic if the given error is not nil and, otherwise, will return the database connection.

Types

type ConfigFunc

type ConfigFunc func(*DatabaseConfig) *DatabaseConfig

ConfigFunc is a function that will receive a database configuration and return a new one with some parameters changed.

func WithName

func WithName(name string) ConfigFunc

WithName returns a ConfigFunc that will change the database name of the received database config to the one given.

type DatabaseConfig

type DatabaseConfig struct {
	configurable.BasicConfiguration

	// Username of the database user.
	Username string `envconfig:"dbuser" default:"testing"`
	// Password of the database user.
	Password string `envconfig:"dbpass" default:"testing"`
	// Port used to establish connection with the database.
	Port int `envconfig:"dbport" default:"5432"`
	// Host to establish connection with the database.
	Host string `envconfig:"dbhost" default:"0.0.0.0"`
	// Name of the database to connect to.
	Name string `envconfig:"dbname" default:"testing"`
	// SSLMode used to specify the way of using SSL in the connection.
	SSLMode SSLMode `envconfig:"dbsslmode" default:"disable"`
	// AppName is the name of the app using the connection.
	AppName string `envconfig:"dbappname"`
	// Timeout is the number time to consider a connection timed out.
	Timeout time.Duration `envconfig:"dbtimeout" default:"30s"`
}

DatabaseConfig describes the configuration of the database parameters used to establish a connection with it.

func (*DatabaseConfig) DataSourceName

func (c *DatabaseConfig) DataSourceName() (string, error)

DataSourceName returns the DSN string to connect to the database.

type SSLMode

type SSLMode string

SSLMode provides different levels of protection against attacks. Check https://www.postgresql.org/docs/9.1/static/libpq-ssl.html

const (
	// Disable disables the SSL.
	Disable SSLMode = "disable"
	// Allow enables the SSL only if the server insists on it.
	Allow SSLMode = "allow"
	// Prefer enables the SSL but only if the server supports it.
	Prefer SSLMode = "prefer"
	// Require ensures SSL is used but does not check the network is secure.
	Require SSLMode = "require"
	// VerifyCA ensures SSL is used and the connection is established with a
	// trusted server.
	VerifyCA SSLMode = "verify-ca"
	// VerifyFull ensures SSL is used and the connection is established with
	// the specified server.
	VerifyFull SSLMode = "verify-full"
)

Jump to

Keyboard shortcuts

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