Skip to content

Commit

Permalink
chore: manager separate
Browse files Browse the repository at this point in the history
  • Loading branch information
23doors committed Jun 12, 2020
1 parent f4f573f commit 23d6fbc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 48 deletions.
13 changes: 13 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,16 @@ func (d *DB) Shutdown() error {

return nil
}

const ContextSchemaKey = "schema"

// GetDB returns base db for context.
func GetDB(db Databaser, c DBContext) *pg.DB {
return db.DB()
}

// GetTenantDB returns base tenant db for context.
func GetTenantDB(db Databaser, c DBContext) *pg.DB {
schema := c.Get(ContextSchemaKey).(string)
return db.TenantDB(schema)
}
10 changes: 10 additions & 0 deletions database/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package database

import (
"net/http"

"github.com/go-pg/pg/v9"
)

//go:generate go run github.com/vektra/mockery/cmd/mockery -name DBContext
Expand All @@ -15,3 +17,11 @@ type DBContext interface {
// Request returns `*http.Request`.
Request() *http.Request
}

//go:generate go run github.com/vektra/mockery/cmd/mockery -name Databaser
type Databaser interface {
DB() *pg.DB
TenantDB(schema string) *pg.DB
}

var _ Databaser = (*DB)(nil)
20 changes: 0 additions & 20 deletions database/manager/db.go

This file was deleted.

15 changes: 0 additions & 15 deletions database/manager/interface.go

This file was deleted.

8 changes: 4 additions & 4 deletions database/manager/live_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ type LiveManager struct {
}

// NewLiveManager creates and returns new live manager.
func (q *Factory) NewLiveManager(c database.DBContext) *LiveManager {
return &LiveManager{Manager: q.NewManager(c)}
func NewLiveManager(db *database.DB, c database.DBContext) *LiveManager {
return &LiveManager{Manager: NewManager(db, c)}
}

// NewLiveTenantManager creates and returns new live tenant manager.
func (q *Factory) NewLiveTenantManager(c database.DBContext) *LiveManager {
return &LiveManager{Manager: q.NewTenantManager(c)}
func NewLiveTenantManager(db *database.DB, c database.DBContext) *LiveManager {
return &LiveManager{Manager: NewTenantManager(db, c)}
}

// Query returns only alive objects.
Expand Down
18 changes: 9 additions & 9 deletions database/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ import (

// Manager defines object manager.
type Manager struct {
*Factory
db *database.DB
dbCtx database.DBContext
curDB orm.DB
dbGet func(database.DBContext) orm.DB
}

// NewManager creates and returns new manager.
func (q *Factory) NewManager(c database.DBContext) *Manager {
func NewManager(db *database.DB, c database.DBContext) *Manager {
return &Manager{
Factory: q,
dbCtx: c,
db: db,
dbCtx: c,
dbGet: func(c database.DBContext) orm.DB {
return DB(q.db, c)
return database.GetDB(db, c)
},
}
}

// NewTenantManager creates and returns new tenant manager.
func (q *Factory) NewTenantManager(c database.DBContext) *Manager {
func NewTenantManager(db *database.DB, c database.DBContext) *Manager {
return &Manager{
Factory: q,
dbCtx: c,
db: db,
dbCtx: c,
dbGet: func(c database.DBContext) orm.DB {
return TenantDB(q.db, c)
return database.GetTenantDB(db, c)
},
}
}
Expand Down

0 comments on commit 23d6fbc

Please sign in to comment.