calendrier/services/connect.go

45 lines
907 B
Go
Raw Normal View History

2024-02-17 13:25:03 +01:00
package services
import (
"database/sql"
"os"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
)
func get() *bun.DB {
connector := pgdriver.NewConnector(
pgdriver.WithInsecure(true),
pgdriver.WithAddr(os.Getenv("DB_ADDRESS")),
pgdriver.WithUser(os.Getenv("DB_USER")),
pgdriver.WithPassword(os.Getenv("DB_PASSWD")),
pgdriver.WithDatabase(os.Getenv("DB_DATABASE")),
)
ret := bun.NewDB(sql.OpenDB(connector), pgdialect.New())
return ret
}
/*
func get() (*pgx.Conn, error) {
uri := "postgres://" + os.Getenv("DB_USER") + ":" + os.Getenv("DB_PASSWD") +
"@" + os.Getenv("DB_ADDRESS") + "/" + os.Getenv("DB_DATABASE")
conn, err := pgx.Connect(context.Background(), uri)
if err != nil {
return nil, err
}
return conn, nil
}
*/
/*
func close(conn *pgx.Conn) {
if conn != nil {
conn.Close(context.Background())
}
}
*/