postgres

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2021 License: MIT Imports: 15 Imported by: 2

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 CreateMigrationFiles

func CreateMigrationFiles(filename string) error

CreateMigrationFiles for postgres and put in migration folder

func IsDuplicate

func IsDuplicate(err error) bool

IsDuplicate check error from postgres if error is because duplicated record

func IsForeignNotFound

func IsForeignNotFound(err error) bool

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

func IsInvalidInput

func IsInvalidInput(err error) bool

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

func IsNotFound

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 TranslateQuery

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      int
	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, 1(Silent) - 2(Error) - 3(Warn) - 4(Info), default is Error
	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 github.com/jinzhu/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) Reset

func (p *Config) Reset()

Reset singleton postgres connection client

func (*Config) SetDB

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

SetDB with existing connection

type Filter

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

Filter sql query

func NewFilter

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

NewFilter creates a new property Filter

type Ordering

type Ordering struct {
	Field     string
	Direction string
}

Ordering set ordering result

func NewOrdering

func NewOrdering(field, direction string) *Ordering

NewOrdering create a new property Ordering

type Query

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

Query preparation sql

func NewQuery

func NewQuery(model string) *Query

NewQuery sql

func (*Query) Filter

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

Filter adds a filter to the query

func (*Query) Ordering

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

Ordering adds a sort order to the query

func (*Query) Slice

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