2017-05-03 07:16:45 +02:00
|
|
|
// Package that provides the functionality to open, close and use a database
|
2017-03-30 16:09:44 +02:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
2017-03-30 17:02:20 +02:00
|
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
2017-04-04 09:49:46 +02:00
|
|
|
|
|
|
|
"github.com/genofire/hs_master-kss-monolith/lib/log"
|
2017-03-30 16:09:44 +02:00
|
|
|
)
|
|
|
|
|
2017-05-03 07:16:45 +02:00
|
|
|
// Database connection for writing purposes
|
2017-04-28 10:27:36 +02:00
|
|
|
var Write *gorm.DB
|
|
|
|
|
2017-05-03 07:16:45 +02:00
|
|
|
// Database connection for reading purposes
|
2017-04-28 10:27:36 +02:00
|
|
|
var Read *gorm.DB
|
|
|
|
|
2017-05-03 07:16:45 +02:00
|
|
|
// Configuration files
|
2017-03-30 16:09:44 +02:00
|
|
|
var (
|
2017-04-28 10:27:36 +02:00
|
|
|
config *Config
|
2017-04-07 11:56:28 +02:00
|
|
|
runtime []interface{}
|
2017-03-30 16:09:44 +02:00
|
|
|
)
|
|
|
|
|
2017-05-03 07:16:45 +02:00
|
|
|
// Configuration of the database connection
|
2017-03-30 16:09:44 +02:00
|
|
|
type Config struct {
|
2017-05-03 07:16:45 +02:00
|
|
|
// type of the database, currently supports sqlite and postgres
|
2017-04-28 10:27:36 +02:00
|
|
|
Type string
|
|
|
|
// connection configuration
|
|
|
|
Connection string
|
2017-05-03 07:16:45 +02:00
|
|
|
// create another connection for reading only
|
2017-03-30 16:09:44 +02:00
|
|
|
ReadConnection string
|
2017-05-03 07:16:45 +02:00
|
|
|
// enable logging of the generated sql string
|
2017-04-28 10:27:36 +02:00
|
|
|
Logging bool
|
2017-03-30 16:09:44 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 10:02:42 +02:00
|
|
|
// Function to open a database and set the given configuration
|
2017-03-30 16:16:19 +02:00
|
|
|
func Open(c Config) (err error) {
|
2017-04-04 09:49:46 +02:00
|
|
|
writeLog := log.Log.WithField("db", "write")
|
2017-03-30 16:09:44 +02:00
|
|
|
config = &c
|
|
|
|
Write, err = gorm.Open(config.Type, config.Connection)
|
2017-04-04 19:28:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-03-30 18:44:09 +02:00
|
|
|
Write.SingularTable(true)
|
2017-03-31 10:57:01 +02:00
|
|
|
Write.LogMode(c.Logging)
|
2017-04-04 09:49:46 +02:00
|
|
|
Write.SetLogger(writeLog)
|
2017-03-30 18:44:09 +02:00
|
|
|
Write.Callback().Create().Remove("gorm:update_time_stamp")
|
|
|
|
Write.Callback().Update().Remove("gorm:update_time_stamp")
|
2017-03-30 16:09:44 +02:00
|
|
|
if len(config.ReadConnection) > 0 {
|
2017-04-04 09:49:46 +02:00
|
|
|
readLog := log.Log.WithField("db", "read")
|
2017-03-30 16:09:44 +02:00
|
|
|
Read, err = gorm.Open(config.Type, config.ReadConnection)
|
2017-04-04 19:28:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-03-30 18:44:09 +02:00
|
|
|
Read.SingularTable(true)
|
2017-03-31 10:57:01 +02:00
|
|
|
Read.LogMode(c.Logging)
|
2017-04-04 09:49:46 +02:00
|
|
|
Read.SetLogger(readLog)
|
2017-03-30 18:44:09 +02:00
|
|
|
Read.Callback().Create().Remove("gorm:update_time_stamp")
|
|
|
|
Read.Callback().Update().Remove("gorm:update_time_stamp")
|
2017-03-30 16:09:44 +02:00
|
|
|
} else {
|
|
|
|
Read = Write
|
|
|
|
}
|
2017-04-07 11:56:28 +02:00
|
|
|
Write.AutoMigrate(runtime...)
|
2017-03-30 16:09:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-28 10:27:36 +02:00
|
|
|
// Function to safely close the database
|
2017-03-30 16:09:44 +02:00
|
|
|
func Close() {
|
|
|
|
Write.Close()
|
|
|
|
if len(config.ReadConnection) > 0 {
|
|
|
|
Read.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 10:02:42 +02:00
|
|
|
// Function to add a model to the runtime
|
2017-03-30 16:09:44 +02:00
|
|
|
func AddModel(m interface{}) {
|
2017-04-07 11:56:28 +02:00
|
|
|
runtime = append(runtime, m)
|
2017-03-30 16:09:44 +02:00
|
|
|
}
|