Skip to content

Commit

Permalink
feat(orm): add postgres connection handler for gorm
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Oct 2, 2020
1 parent f8f53a8 commit aca8fc1
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions orm/postgres.go
@@ -0,0 +1,48 @@
package orm

import (
"log"
"os"
"sync"

"github.com/adhocore/urlsh/model"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)

var conn *gorm.DB

func pgConnect() *gorm.DB {
dsn := os.Getenv("APP_DB_DSN")
if dsn == "" {
log.Fatal("Database configuration DSN missing, Pass in APP_DB_DSN env")
}

logLevel := logger.Warn
if os.Getenv("APP_ENV") == "prod" {
logLevel = logger.Silent
}

db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logLevel),
})

if err != nil {
log.Fatal("Cannot connect to database with given DSN")
}

_ = db.AutoMigrate(&model.Keyword{}, &model.Url{})

return db
}

var once sync.Once

func Connection() *gorm.DB {
once.Do(func() {
conn = pgConnect()
})

return conn
}

0 comments on commit aca8fc1

Please sign in to comment.