db

package
v0.0.0-...-c859fe7 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2015 License: MIT Imports: 9 Imported by: 0

README

DB

This package implements a way to open DB connections. Basically, this package opens DB connections by assuming that a JSON config file exists somewhere. This JSON file has the following structure:

{
  "development": {
    "user": "mssola",
      "dbname": "project-dev",
      "password": "1234",
      "sslmode": "disable"
  },
  "production": {
    "user": "mssola",
    "dbname": "project",
    "password": "1234",
    "sslmode": "require"
  },
  "test": {
    "user": "mssola",
    "dbname": "project-test",
    "password": "1234",
    "sslmode": "disable"
  }
}

Moreover, I've also added the possibility to tell the Open function if we're using Heroku's PostgreSQL add-on. An example:

package main

import (
	"github.com/mssola/go-utils/db"
	"github.com/mssola/go-utils/path"
)

func main() {
	// The Options struct has a default value for each of its fields. Take a
	// look at the documentation of the Options struct for more information.
	// The Base field is mandatory (it panics if it's not set).

	// It also panics if the JSON file could not be found.

	d := db.Open(db.Options{
		Base:        path.FindRoot("project", "."),
		Relative:    "db/database.json",
		Environment: "production",
		DBMS:        "postgres",
	})

	// Doing something useful with the d connection...

	d.Close()

	// Heroku.
	d = db.Open(db.Options{
		Base:        path.FindRoot("project", "."),
		Relative:    "db/database.json",
		Environment: "production",
		Heroku:      true,
	})

	// Doing something useful with the d connection...

	d.Close()
}

Copyright © 2014-2015 Miquel Sabaté Solà, released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Open

func Open(options Options) *sql.DB

Types

type Options

type Options struct {
	// An absolute path containing the base directory of the project.
	// NOTE: it panics if it's not set.
	Base string

	// Path relative to the Base path pointing to the JSON file.
	// Default: "database.json"
	Relative string

	// The name of the current environment. Default: "development".
	Environment string

	// The name of the DBMS driver. If Heroku is set to true, this option will
	// be ignored since we're always picking "postgres" in Heroku. If Heroku
	// is set to false, then it defaults to "postgres".
	DBMS string

	// Set to true if the Open function has to deal with Heroku's config.
	Heroku bool
}

The options that can be passed to the Open function.

Jump to

Keyboard shortcuts

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