updown/repository/connector.go

42 lines
815 B
Go
Raw Normal View History

2019-02-27 05:44:47 +00:00
package repository
import (
"database/sql"
"errors"
2019-02-27 05:44:47 +00:00
_ "github.com/lib/pq"
)
const driver = "postgres"
// ErrNotConnected is returned when the database connection has never been attempted
var ErrNotConnected = errors.New("repository: Database connection was never established")
var connector *DatabaseConnector
2019-02-27 05:44:47 +00:00
// DatabaseConnector holds a connection to the database
type DatabaseConnector struct {
*sql.DB
}
// GetConnector gets the stored database connection
func GetConnector() *DatabaseConnector {
if connector == nil {
panic(ErrNotConnected)
}
return connector
}
// Connect connects to the database
func Connect() (*DatabaseConnector, error) {
connectorCandidate, err := initDatabaseConnector()
2019-02-27 05:44:47 +00:00
if err != nil {
return nil, err
}
connector = connectorCandidate
return connector, nil
2019-02-27 05:44:47 +00:00
}