From 34ccc98a942098faefb5f4211b215ff9ccc7ad0e Mon Sep 17 00:00:00 2001 From: Alexander Neonxp Kiryukhin Date: Mon, 9 Dec 2024 01:07:15 +0300 Subject: Начальный MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/db/config.go | 6 ++++++ pkg/db/db.go | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 pkg/db/config.go create mode 100644 pkg/db/db.go (limited to 'pkg/db') diff --git a/pkg/db/config.go b/pkg/db/config.go new file mode 100644 index 0000000..0acab0c --- /dev/null +++ b/pkg/db/config.go @@ -0,0 +1,6 @@ +package db + +type Config struct { + Database string `env:"DATABASE"` + Debug bool `env:"DB_DEBUG"` +} diff --git a/pkg/db/db.go b/pkg/db/db.go new file mode 100644 index 0000000..7c361f6 --- /dev/null +++ b/pkg/db/db.go @@ -0,0 +1,24 @@ +package db + +import ( + "database/sql" + + "github.com/uptrace/bun" + "github.com/uptrace/bun/dialect/pgdialect" + "github.com/uptrace/bun/driver/pgdriver" + "github.com/uptrace/bun/extra/bundebug" +) + +// dsn := "postgres://postgres:@localhost:5432/test?sslmode=disable" +// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432" + +func New(config *Config) *bun.DB { + sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(config.Database))) + + db := bun.NewDB(sqldb, pgdialect.New()) + if config.Debug { + db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true))) + } + + return db +} -- cgit v1.2.3