libpostgres

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 7, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

README

Overview

libpostgres is a collection of postgresql connections from various sources such as making connections with amp, elastic, etc.

How to use

Connect to database

  dbOptions := libpostgres.Options{
		Host:     "db_host",
		Port:     "db_port",
		Username: "db_username",
		Password: "db_password",
		DBName:   "db_name",
		SSLMode:  "db_sslmode",
	}
	db, err := libpostgres.Connect(dbOptions)
	if err != nil {
		fmt.Println("Error conn to DB", err)
		panic(err)
	}

Migrate database

  • Create directory db/migrations on yout project.
  • Create your file sql with format name
  • 000001_create_users_table.down.sql
  • 000001_create_users_table.up.sql In the .up.sql file let's create the table:
CREATE TABLE IF NOT EXISTS users(
  user_id serial PRIMARY KEY,
  username VARCHAR (50) UNIQUE NOT NULL,
  password VARCHAR (50) NOT NULL,
  email VARCHAR (300) UNIQUE NOT NULL
);

And in the .down.sql let's delete it:

DROP TABLE IF EXISTS users;

Read more here

   err := Migrate(db, "file://./migrations")
   if err != nil {
   	fmt.Println("Error migrate DB", err)
   	panic(err)
   }

Constribute

Please welcome .

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(options Options) (*sql.DB, error)

Connect is ..

func ConnectWithAMP

func ConnectWithAMP(options Options) (*sql.DB, error)

ConnectWithAMP is ...

func DownOneStep added in v1.0.1

func DownOneStep(db *sql.DB, path string) error

func Migrate added in v1.0.1

func Migrate(db *sql.DB, path string) error

Migrate is used to migrate table Ex: Migrate(opt, "file://db/migrations")

Types

type Options

type Options struct {
	Host           string
	Port           int
	Username       string
	Password       string
	DBName         string
	ConnectTimeout int
	MaxOpenConn    int
	MaxIdleConn    int
	SSLCert        string
	SSLKey         string
	SSLRootCert    string
	SSLMode        string
}

Options is a struct

Jump to

Keyboard shortcuts

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