Go helper for Postgres database connection, base on GORM and migrate for database migration
import the library go get github.com/dynastymasra/pgocon
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
}
if err := config.Ping(); err != nil {
// Handle error
}
if err := config.Close(); err != nil {
// Handle error
}
config.SetDB(gorm.DB)
Import the library go get github.com/dynastymasra/pgocon
, create a new folder in root and set the name to migration
if err := pgocon.CreateMigrationFiles("filename_name"); err != nil {
// Handler error
}
migration, err := pgocon.Migration(gorm.DB)
if err != nil {
// Handler error
}
if err := pgocon.RunMigration(migration); err != nil {
// Handler error
}
if err : pgocon.RollbackMigration(migration); err != nil {
// Handler error
}
Open to anyone who wants to contribute to this library, fork
and create a Pull Request