apply postgresql(with bun orm)

This commit is contained in:
dalbodeule
2025-10-14 20:16:42 +09:00
parent 44e4446bf0
commit e261ec59a1
7 changed files with 125 additions and 12 deletions

35
db/main.go Normal file
View 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
}