superkit/bootstrap/app/db.go
2024-06-12 18:12:27 +02:00

34 lines
689 B
Go

package app
import (
"log"
"os"
"github.com/anthdm/gothkit/db"
"github.com/anthdm/gothkit/kit"
_ "github.com/mattn/go-sqlite3"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/sqlitedialect"
"github.com/uptrace/bun/extra/bundebug"
)
var DB *bun.DB
func init() {
config := db.Config{
Driver: os.Getenv("DB_DRIVER"),
Name: os.Getenv("DB_NAME"),
Password: os.Getenv("DB_PASSWORD"),
User: os.Getenv("DB_USER"),
Host: os.Getenv("DB_HOST"),
}
db, err := db.New(config)
if err != nil {
log.Fatal(err)
}
DB = bun.NewDB(db, sqlitedialect.New())
if kit.IsDevelopment() {
DB.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
}
}