Skip to content

Commit

Permalink
refactor: add multi-database support
Browse files Browse the repository at this point in the history
  • Loading branch information
OldSmokeGun committed Jul 2, 2024
1 parent dcfbb9d commit 74e994a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 2 additions & 8 deletions internal/pkg/ent/ent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@ package ent
//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --feature sql/execquery --feature sql/modifier --feature intercept --target ./ent ../../app/repository/schema

import (
"context"
"database/sql"
"log/slog"

entsql "entgo.io/ent/dialect/sql"

"go-scaffold/internal/config"
"go-scaffold/internal/pkg/db"
"go-scaffold/internal/pkg/ent/ent"
_ "go-scaffold/internal/pkg/ent/ent/runtime"
elog "go-scaffold/pkg/log/ent"
)

// New build db client
func New(ctx context.Context, env config.Env, dbConf config.DatabaseConn, logger *slog.Logger) (*ent.Client, error) {
sdb, err := db.New(ctx, dbConf)
if err != nil {
return nil, err
}

func New(env config.Env, dbConf config.DatabaseConn, logger *slog.Logger, sdb *sql.DB) (*ent.Client, error) {
driver := entsql.OpenDB(dbConf.Driver.String(), sdb)

options := []ent.Option{
Expand Down
8 changes: 7 additions & 1 deletion internal/pkg/ent/provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import (
"log/slog"

"go-scaffold/internal/config"
"go-scaffold/internal/pkg/db"
"go-scaffold/internal/pkg/ent/ent"
)

type DefaultClient = ent.Client

// ProvideDefault db client
func ProvideDefault(ctx context.Context, env config.Env, conf config.DefaultDatabase, logger *slog.Logger) (*DefaultClient, func(), error) {
client, err := New(ctx, env, conf.DatabaseConn, logger)
sdb, err := db.New(ctx, conf.DatabaseConn)
if err != nil {
return nil, nil, err
}

client, err := New(env, conf.DatabaseConn, logger, sdb)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 74e994a

Please sign in to comment.