diff options
author | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-12-09 01:07:15 +0300 |
---|---|---|
committer | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-12-09 01:07:15 +0300 |
commit | 34ccc98a942098faefb5f4211b215ff9ccc7ad0e (patch) | |
tree | 7696ab4d7c8d9fb09c7e2575d482517f68824ae3 /pkg/db/db.go |
Начальный
Diffstat (limited to 'pkg/db/db.go')
-rw-r--r-- | pkg/db/db.go | 24 |
1 files changed, 24 insertions, 0 deletions
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 +} |