mirror of
https://github.com/dalbodeule/sshchat.git
synced 2025-12-08 07:05:44 +09:00
apply postgresql(with bun orm)
This commit is contained in:
35
db/main.go
Normal file
35
db/main.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"database/sql"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/dialect/pgdialect"
|
||||
"github.com/uptrace/bun/driver/pgdriver"
|
||||
)
|
||||
|
||||
func GetDB(pgDsn string) (*bun.DB, error) {
|
||||
if pgDsn == "" {
|
||||
return nil, fmt.Errorf("pg_dsn is required")
|
||||
}
|
||||
|
||||
sqlConnection := sql.OpenDB(pgdriver.NewConnector(
|
||||
pgdriver.WithDSN(pgDsn),
|
||||
))
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := sqlConnection.PingContext(ctx); err != nil {
|
||||
_ = sqlConnection.Close()
|
||||
return nil, fmt.Errorf("failed to connect to database: %w", err)
|
||||
}
|
||||
|
||||
db := bun.NewDB(sqlConnection, pgdialect.New())
|
||||
|
||||
return db, nil
|
||||
}
|
||||
Reference in New Issue
Block a user