using go modules now
This commit is contained in:
61
models/db.go
Normal file
61
models/db.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package models
|
||||
|
||||
import "os"
|
||||
import "time"
|
||||
import "github.com/astaxie/beego/orm"
|
||||
import "github.com/rs/zerolog/log"
|
||||
import _ "github.com/lib/pq"
|
||||
|
||||
var ormObject orm.Ormer
|
||||
|
||||
func Initialize() {
|
||||
if os.Getenv("DEBUG") != "" {
|
||||
orm.Debug = true
|
||||
}
|
||||
|
||||
ConnectToDb()
|
||||
SyncDB()
|
||||
}
|
||||
|
||||
// ConnectToDb - Initializes the ORM and Connection to the postgres DB
|
||||
func ConnectToDb() {
|
||||
|
||||
orm.DefaultTimeLoc = time.UTC
|
||||
|
||||
POSTGRES_DB_URL := os.Getenv("POSTGRES_DB_URL")
|
||||
|
||||
orm.RegisterDriver("postgres", orm.DRPostgres)
|
||||
orm.RegisterDataBase("default", "postgres", POSTGRES_DB_URL)
|
||||
orm.SetMaxIdleConns("default", 1)
|
||||
orm.SetMaxOpenConns("default", 5)
|
||||
|
||||
orm.RegisterModel(new(Merp))
|
||||
ormObject = orm.NewOrm()
|
||||
ormObject.Using("default")
|
||||
|
||||
}
|
||||
|
||||
func SyncDB() {
|
||||
// Database alias.
|
||||
name := "default"
|
||||
|
||||
// Drop table and re-create.
|
||||
force := false
|
||||
|
||||
// Print log.
|
||||
verbose := true
|
||||
|
||||
// Error.
|
||||
err := orm.RunSyncdb(name, force, verbose)
|
||||
if err != nil {
|
||||
log.Fatal().Msg(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// GetOrmObject - Getter function for the ORM object with which we can query the database
|
||||
func GetOrmObject() orm.Ormer {
|
||||
if ormObject == nil {
|
||||
Initialize()
|
||||
}
|
||||
return ormObject
|
||||
}
|
||||
12
models/merp.go
Normal file
12
models/merp.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type Merp struct {
|
||||
ID int `orm:"auto;column(id)"`
|
||||
Content string `json:"content" orm:"type(jsonb);column(content)"`
|
||||
Created time.Time `orm:"auto_now_add;type(datetime);column(created)"`
|
||||
RemoteIP string `orm:"size(128);column(remoteip)"`
|
||||
Thing string `json:"thing" orm:"index;size(256)"`
|
||||
UUID string `orm:"size(36);column(uuid)"`
|
||||
}
|
||||
Reference in New Issue
Block a user