Skip to content

Commit

Permalink
Merge branch 'feature/mv_db' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
migueldv90 committed Nov 24, 2024
2 parents e3f7581 + 856082b commit 4e67c8f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion api-v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
panic("Cannot load environment")
}

db.SetDB(cfg.Database)
db.SetEntDB(cfg.Database)
db.SetBunDB(cfg.Database)
auth.SetSecretJWT(cfg.Secret)

Expand Down
17 changes: 17 additions & 0 deletions api-v1/utils/db/bun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package db

import (
"database/sql"

_ "github.com/jackc/pgx/v5/stdlib"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
)

var BunDB *bun.DB

func SetBunDB(urlDB string) {
sqlDB := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(urlDB)))
BunDB = bun.NewDB(sqlDB, pgdialect.New())
}
11 changes: 1 addition & 10 deletions api-v1/utils/db/db.go → api-v1/utils/db/ent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ import (
entSQL "entgo.io/ent/dialect/sql"

_ "github.com/jackc/pgx/v5/stdlib"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"

"api-go/ent"
)

var EntDB *ent.Client
var BunDB *bun.DB

func SetDB(urlDB string) {
func SetEntDB(urlDB string) {
connDB, err := sql.Open("pgx", urlDB)
if err != nil {
panic("Failed to open database connection")
Expand All @@ -31,8 +27,3 @@ func SetDB(urlDB string) {
sqlDB := entSQL.OpenDB(dialect.Postgres, connDB)
EntDB = ent.NewClient(ent.Driver(sqlDB))
}

func SetBunDB(urlDB string) {
sqlDB := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(urlDB)))
BunDB = bun.NewDB(sqlDB, pgdialect.New())
}

0 comments on commit 4e67c8f

Please sign in to comment.