pgocon

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2021 License: MIT Imports: 12 Imported by: 0

README

PGoCon

Go Report Card GoDoc

Go helper for Postgres database connection, base on GORM and migrate for database migration

How to Use

import the library go get github.com/dynastymasra/pgocon

Create a New Database Client
config := pgocon.Config{		
	Database:     "database",
	Host:         "localhost",
	Port:         5432,
	Username:     "postgres",
	Password:     "root",
	Params:       "sslmode=disable",
	MaxIdleConn:  10,
	MaxOpenConn:  10,
	LogMode:      "silent",
	DebugEnabled: true,
}
		
// Return gorm client
conn, err := config.Client()	
if err != nil {		
	// Handle error
}

var model Model
if err := conn.Model("tables").Find("id = ?", id).Error; err != nil {
// Handle error
}
Ping The Database Connection
if err := config.Ping(); err != nil {
	// Handle error 
}
Close the Database Connection
if err := config.Close(); err != nil {
	// Handle error
}
Set Database with Existing Connection
config.SetDB(gorm.DB)

How to Migrate

Import the library go get github.com/dynastymasra/pgocon, create a new folder in root and set the name to migration

Create a New Migration File
if err := pgocon.CreateMigrationFiles("filename_name"); err != nil {
    // Handler error
}
Set The Database Connection
migration, err := pgocon.Migration(gorm.DB)
if err != nil {
    // Handler error
}

if err := pgocon.RunMigration(migration); err != nil {
    // Handler error
}
Rollback the Database Migration
if err : pgocon.RollbackMigration(migration); err != nil { 
	// Handler error
}

Development and Contributing

Open to anyone who wants to contribute to this library, fork and create a Pull Request

Documentation

Index

Constants

View Source
const (
	Equal            = "Equal"
	LessThan         = "LessThan"
	GreaterThan      = "GreaterThan"
	GreaterThanEqual = "GreaterThanEqual"
	LessThanEqual    = "LessThanEqual"
	IN               = "IN"
	JSON             = "JSON"
	Like             = "Like"

	Descending = "Descending"
	Ascending  = "Ascending"
)

Variables

This section is empty.

Functions

func CreateFile

func CreateFile(filename string) error

CreateFile create a new migration file

func CreateMigrationFiles

func CreateMigrationFiles(filename string) error

CreateMigrationFiles for postgres and put in migration folder

func IsConnClosed added in v0.4.0

func IsConnClosed(err error) bool

IsConnClosed check error from sql if the connection was closed

func IsConnTerminated added in v0.4.0

func IsConnTerminated(err error) bool

IsConnTerminated check error from sql if the connection was terminated

func IsDuplicate added in v0.3.0

func IsDuplicate(err error) bool

IsDuplicate check error from postgres if error is because duplicated record

func IsForeignNotFound added in v0.3.0

func IsForeignNotFound(err error) bool

IsForeignNotFound check error from postgres if error is because foreign key not found

func IsInvalidInput added in v0.3.0

func IsInvalidInput(err error) bool

IsInvalidInput check error from postgres if error is because invalid input enumeration

func IsNotFound added in v0.3.0

func IsNotFound(err error) bool

IsNotFound check error from postgres if error is because record not found

func Migration

func Migration(data *gorm.DB) (*migrate.Migrate, error)

Migration preparation for postgres

func RollbackMigration

func RollbackMigration(migration *migrate.Migrate) error

RollbackMigration rollback the database migration

func RunMigration

func RunMigration(migration *migrate.Migrate) error

RunMigration run the database migration

func TranslateQuery added in v0.3.0

func TranslateQuery(db *gorm.DB, query *Query) *gorm.DB

TranslateQuery from struct to gorm.DB query

Types

type Config

type Config struct {
	Database     string
	Host         string
	Port         int
	Username     string
	Password     string
	Params       string
	MaxIdleConn  int
	MaxOpenConn  int
	LogMode      string
	DebugEnabled bool
}

Config struct to create new postgres connection client

{
	Database: the postgres database name
	Host: the postgres database host (localhost)
	Port: the postgres database port (5432)
	Username: the postgres database username
	Password: the postgres database password
	Params: the postgres database params, use space to separate value (sslmode=disable TimeZone=Asia/Jakarta)
	MaxIdleConn: sets the maximum number of connections in the idle connection pool.
	MaxOpenConn: sets the maximum number of open connections to the database.
	LogMode: sets log mode, (silent) - (error) - (warn) - (info), default is Silent
	DebugEnabled: sets true if enabled debug mode, will show query on console
}

func (Config) Client

func (p Config) Client() (*gorm.DB, error)

Client singleton of Postgres connection client, use Postgres struct to call this method library with gorm.io/gorm

func (Config) Close

func (p Config) Close() error

Close database connection

func (Config) Ping

func (p Config) Ping() error

Ping check database connection

func (Config) SetDB

func (p Config) SetDB(conn *gorm.DB)

SetDB with existing connection

type Filter added in v0.3.0

type Filter struct {
	Condition string
	Field     string
	Value     interface{}
}

Filter sql query

func NewFilter added in v0.3.0

func NewFilter(field, condition string, value interface{}) *Filter

NewFilter creates a new property Filter

type Ordering added in v0.3.0

type Ordering struct {
	Field     string
	Direction string
}

Ordering set ordering result

func NewOrdering added in v0.3.0

func NewOrdering(field, direction string) *Ordering

NewOrdering create a new property Ordering

type Query added in v0.3.0

type Query struct {
	Model     string
	Limit     int
	Offset    int
	Filters   []*Filter
	Orderings []*Ordering
}

Query preparation sql

func NewQuery added in v0.3.0

func NewQuery(model string) *Query

NewQuery sql

func (*Query) Filter added in v0.3.0

func (q *Query) Filter(property, condition string, value interface{}) *Query

Filter adds a filter to the query

func (*Query) Ordering added in v0.3.0

func (q *Query) Ordering(property, direction string) *Query

Ordering adds a sort order to the query

func (*Query) Slice added in v0.3.0

func (q *Query) Slice(offset, limit int) *Query

Slice result from database

Jump to

Keyboard shortcuts

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