Skip to content

dynastymasra/pgocon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PGoCon

Go Report Card GoDoc License: MIT

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

About

Go helper for Postgres database connection

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages