superkit/bootstrap/app/db.go

35 lines
689 B
Go
Raw Normal View History

2024-06-12 18:12:27 +02:00
package app
2024-06-06 10:04:51 +02:00
import (
"log"
"os"
2024-06-06 16:53:16 +02:00
"github.com/anthdm/gothkit/db"
"github.com/anthdm/gothkit/kit"
2024-06-06 10:04:51 +02:00
_ "github.com/mattn/go-sqlite3"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/sqlitedialect"
"github.com/uptrace/bun/extra/bundebug"
)
2024-06-12 18:12:27 +02:00
var DB *bun.DB
2024-06-06 10:04:51 +02:00
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)
}
2024-06-12 18:12:27 +02:00
DB = bun.NewDB(db, sqlitedialect.New())
2024-06-06 10:04:51 +02:00
if kit.IsDevelopment() {
2024-06-12 18:12:27 +02:00
DB.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
2024-06-06 10:04:51 +02:00
}
}