Skip to content

Commit

Permalink
WIP: Refactored constructor names for meaningfulness and migrated fro…
Browse files Browse the repository at this point in the history
…m snake case to small case import aliases in database
  • Loading branch information
shashank-priyadarshi committed Mar 20, 2024
1 parent 22b746f commit 19374e8
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 74 deletions.
6 changes: 3 additions & 3 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"github.com/shashank-priyadarshi/utilities/database/models"
"github.com/shashank-priyadarshi/utilities/database/ports"
"github.com/shashank-priyadarshi/utilities/logger"
loggerPort "github.com/shashank-priyadarshi/utilities/logger/ports"
loggerport "github.com/shashank-priyadarshi/utilities/logger/ports"
)

func NewDatabase(ctx context.Context, log loggerPort.Logger, config models.Config) (database ports.Database, err error) {
func New(ctx context.Context, log loggerport.Logger, config models.Config) (database ports.Database, err error) {

if !isSupported(config.Type) {
err = fmt.Errorf("unsupported database type: %s", config.Type)
Expand All @@ -33,7 +33,7 @@ func NewDatabase(ctx context.Context, log loggerPort.Logger, config models.Confi
}
}

return adapters.NewDatabaseAdapter(ctx, log, &config)
return adapters.New(ctx, log, &config)
}

func isSupported(db constants.Database) bool {
Expand Down
16 changes: 8 additions & 8 deletions database/internal/adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,38 @@ import (
"github.com/shashank-priyadarshi/utilities/database/internal/redis"
"github.com/shashank-priyadarshi/utilities/database/models"
"github.com/shashank-priyadarshi/utilities/database/ports"
ports2 "github.com/shashank-priyadarshi/utilities/logger/ports"
loggerport "github.com/shashank-priyadarshi/utilities/logger/ports"
)

func NewDatabaseAdapter(ctx context.Context, log ports2.Logger, config *models.Config) (handle ports.Database, err error) {
func New(ctx context.Context, log loggerport.Logger, config *models.Config) (handle ports.Database, err error) {

switch config.Type {
case constants.MONGODB:
client, err := connections.NewMongoDBClient(ctx, log, config)
client, err := connections.MongoDB(ctx, log, config)
if err != nil {
return nil, fmt.Errorf("error connecting to mongo db: %w", err)
}

handle = mongodb.NewMongoDBHandle(log, client)
handle = mongodb.Handle(log, client)

case constants.MYSQLDB:
client, err := connections.NewRDBMSClient(ctx, log, config)
client, err := connections.RDBMS(ctx, log, config)
if err != nil {
return nil, fmt.Errorf("error connecting to rdbms: %w", err)
}

handle, err = rdbms.NewRelationalDBHandle(log, config.Options.WithORM, config.Options.ORM, client)
handle, err = rdbms.Handle(log, config.Options.WithORM, config.Options.ORM, client)
if err != nil {
return nil, fmt.Errorf("error creating relational db handle: %w", err)
}

case constants.REDIS:
client, err := connections.NewRedisClient(ctx, log, config)
client, err := connections.Redis(ctx, log, config)
if err != nil {
return nil, fmt.Errorf("error connecting to redis: %w", err)
}

handle = redis.NewRedisHandle(log, client)
handle = redis.Handle(log, client)

default:
return nil, fmt.Errorf("database type %s is not supported", config.Type)
Expand Down
3 changes: 1 addition & 2 deletions database/internal/connections/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"github.com/shashank-priyadarshi/utilities/logger/ports"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
_ "go.mongodb.org/mongo-driver/mongo/options"
)

var opts = &options.ClientOptions{}

func NewMongoDBClient(ctx context.Context, log ports.Logger, config *models.Config) (client *mongo.Client, err error) {
func MongoDB(ctx context.Context, log ports.Logger, config *models.Config) (client *mongo.Client, err error) {

if len(config.Options.URI) == 0 {
err = fmt.Errorf("mongo db uri cannot be empty")
Expand Down
2 changes: 1 addition & 1 deletion database/internal/connections/rdbms.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"gorm.io/gorm"
)

func NewRDBMSClient(ctx context.Context, log ports.Logger, config *models.Config) (client interface{}, err error) {
func RDBMS(ctx context.Context, log ports.Logger, config *models.Config) (client interface{}, err error) {

if !isSupported(config.Options.Driver) {
err = fmt.Errorf("unsupported sql driver")
Expand Down
8 changes: 4 additions & 4 deletions database/internal/connections/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package connections
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
redisclient "github.com/redis/go-redis/v9"
"github.com/shashank-priyadarshi/utilities/database/models"
"github.com/shashank-priyadarshi/utilities/logger/ports"
)

func NewRedisClient(ctx context.Context, log ports.Logger, config *models.Config) (client *redis.Client, err error) {
func Redis(ctx context.Context, log ports.Logger, config *models.Config) (client *redisclient.Client, err error) {

if len(config.Options.URI) == 0 {
err = fmt.Errorf("redis uri cannot be empty")
Expand All @@ -17,13 +17,13 @@ func NewRedisClient(ctx context.Context, log ports.Logger, config *models.Config
}

// Other options pending: Using functional options
opts := &redis.Options{
opts := &redisclient.Options{
Addr: config.Options.URI,
Username: config.Options.Username,
Password: config.Options.Password,
}

client = redis.NewClient(opts)
client = redisclient.NewClient(opts)
if client.Ping(ctx); err != nil {
err = fmt.Errorf("error pinging redis: %v", err)
log.Error(err)
Expand Down
24 changes: 12 additions & 12 deletions database/internal/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

type Handle struct {
type Handler struct {
log ports.Logger
client *mongo.Client
}

func NewMongoDBHandle(log ports.Logger, client *mongo.Client) *Handle {
return &Handle{
func Handle(log ports.Logger, client *mongo.Client) *Handler {
return &Handler{
log: log,
client: client,
}
}

func (h *Handle) Create(ctx context.Context, params ...interface{}) (*models.Response, error) {
func (h *Handler) Create(ctx context.Context, params ...interface{}) (*models.Response, error) {

if len(params) < 3 {
return nil, utilities.InsufficientParameters
Expand Down Expand Up @@ -56,7 +56,7 @@ func (h *Handle) Create(ctx context.Context, params ...interface{}) (*models.Res
return nil, nil
}

func (h *Handle) Query(ctx context.Context, params ...interface{}) (*models.Response, error) {
func (h *Handler) Query(ctx context.Context, params ...interface{}) (*models.Response, error) {

if len(params) < 3 {
return nil, utilities.InsufficientParameters
Expand Down Expand Up @@ -102,7 +102,7 @@ func (h *Handle) Query(ctx context.Context, params ...interface{}) (*models.Resp
return response, nil
}

func (h *Handle) Update(ctx context.Context, params ...interface{}) (*models.Response, error) {
func (h *Handler) Update(ctx context.Context, params ...interface{}) (*models.Response, error) {

if len(params) < 4 {
return nil, utilities.InsufficientParameters
Expand Down Expand Up @@ -148,7 +148,7 @@ func (h *Handle) Update(ctx context.Context, params ...interface{}) (*models.Res
}}}, nil
}

func (h *Handle) Delete(ctx context.Context, params ...interface{}) (*models.Response, error) {
func (h *Handler) Delete(ctx context.Context, params ...interface{}) (*models.Response, error) {

if len(params) < 4 {
return nil, utilities.InsufficientParameters
Expand Down Expand Up @@ -182,22 +182,22 @@ func (h *Handle) Delete(ctx context.Context, params ...interface{}) (*models.Res
return &models.Response{Result: []interface{}{deleteResult.DeletedCount}}, nil
}

func (h *Handle) Begin(ctx context.Context, params ...interface{}) (*models.Response, error) {
func (h *Handler) Begin(ctx context.Context, params ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Execute(ctx context.Context, params ...interface{}) (*models.Response, error) {
func (h *Handler) Execute(ctx context.Context, params ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Rollback(ctx context.Context, params ...interface{}) (*models.Response, error) {
func (h *Handler) Rollback(ctx context.Context, params ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Configure(ctx context.Context, params ...interface{}) error {
func (h *Handler) Configure(ctx context.Context, params ...interface{}) error {
return nil
}

func (h *Handle) Close() error {
func (h *Handler) Close() error {
return nil
}
22 changes: 11 additions & 11 deletions database/internal/rdbms/orm/gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,50 @@ import (
"gorm.io/gorm"
)

type Handle struct {
type Handler struct {
log ports.Logger
client *gorm.DB
}

func NewGORMHandle(log ports.Logger, client *gorm.DB) (handle *Handle) {
func Handle(log ports.Logger, client *gorm.DB) (handle *Handler) {
handle.log = log
handle.client = client

return
}

func (h *Handle) Create(ctx context.Context, i ...interface{}) (*models.Response, error) {
func (h *Handler) Create(ctx context.Context, i ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Query(ctx context.Context, i ...interface{}) (*models.Response, error) {
func (h *Handler) Query(ctx context.Context, i ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Update(ctx context.Context, i ...interface{}) (*models.Response, error) {
func (h *Handler) Update(ctx context.Context, i ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Delete(ctx context.Context, i ...interface{}) (*models.Response, error) {
func (h *Handler) Delete(ctx context.Context, i ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Begin(ctx context.Context, i ...interface{}) (*models.Response, error) {
func (h *Handler) Begin(ctx context.Context, i ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Execute(ctx context.Context, i ...interface{}) (*models.Response, error) {
func (h *Handler) Execute(ctx context.Context, i ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Rollback(ctx context.Context, i ...interface{}) (*models.Response, error) {
func (h *Handler) Rollback(ctx context.Context, i ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Configure(ctx context.Context, i ...interface{}) error {
func (h *Handler) Configure(ctx context.Context, i ...interface{}) error {
return nil
}

func (h *Handle) Close() error {
func (h *Handler) Close() error {
return nil
}
8 changes: 4 additions & 4 deletions database/internal/rdbms/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
_ "entgo.io/ent/entc/gen"
_ "entgo.io/ent/schema/field"
"github.com/shashank-priyadarshi/utilities/database/constants"
gormHandler "github.com/shashank-priyadarshi/utilities/database/internal/rdbms/orm/gorm"
gormhandler "github.com/shashank-priyadarshi/utilities/database/internal/rdbms/orm/gorm"
"github.com/shashank-priyadarshi/utilities/database/ports"
ports2 "github.com/shashank-priyadarshi/utilities/logger/ports"
loggerport "github.com/shashank-priyadarshi/utilities/logger/ports"
"gorm.io/gorm"
)

func NewORMHandle(log ports2.Logger, orm constants.ORM, client interface{}) (handle ports.Database, err error) {
func Handle(log loggerport.Logger, orm constants.ORM, client interface{}) (handle ports.Database, err error) {

switch orm {
case constants.GORM:
Expand All @@ -25,7 +25,7 @@ func NewORMHandle(log ports2.Logger, orm constants.ORM, client interface{}) (han
return
}

handle = gormHandler.NewGORMHandle(log, conn)
handle = gormhandler.Handle(log, conn)
return
}

Expand Down
12 changes: 6 additions & 6 deletions database/internal/rdbms/rdbms.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"database/sql"
"fmt"
"github.com/shashank-priyadarshi/utilities/database/constants"
ormHandler "github.com/shashank-priyadarshi/utilities/database/internal/rdbms/orm"
sqlHandler "github.com/shashank-priyadarshi/utilities/database/internal/rdbms/sql"
ormhandler "github.com/shashank-priyadarshi/utilities/database/internal/rdbms/orm"
sqlhandler "github.com/shashank-priyadarshi/utilities/database/internal/rdbms/sql"
"github.com/shashank-priyadarshi/utilities/database/ports"
ports2 "github.com/shashank-priyadarshi/utilities/logger/ports"
loggerport "github.com/shashank-priyadarshi/utilities/logger/ports"
)

func NewRelationalDBHandle(log ports2.Logger, withORM bool, orm constants.ORM, client interface{}) (handle ports.Database, err error) {
func Handle(log loggerport.Logger, withORM bool, orm constants.ORM, client interface{}) (handle ports.Database, err error) {

switch withORM {
case true:
handle, err = ormHandler.NewORMHandle(log, orm, client)
handle, err = ormhandler.Handle(log, orm, client)
if err != nil {
err = fmt.Errorf("error creating orm handle: %w", err)
log.Error(err)
Expand All @@ -29,7 +29,7 @@ func NewRelationalDBHandle(log ports2.Logger, withORM bool, orm constants.ORM, c
return
}

handle = sqlHandler.NewMySQLHandle(log, conn)
handle = sqlhandler.Handle(log, conn)
}

return
Expand Down
22 changes: 11 additions & 11 deletions database/internal/rdbms/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,50 @@ import (
"github.com/shashank-priyadarshi/utilities/logger/ports"
)

type Handle struct {
type Handler struct {
log ports.Logger
client *sql.DB
}

func NewMySQLHandle(log ports.Logger, client *sql.DB) (handle *Handle) {
func Handle(log ports.Logger, client *sql.DB) (handle *Handler) {
handle.log = log
handle.client = client

return
}

func (h *Handle) Create(ctx context.Context, i ...interface{}) (*models.Response, error) {
func (h *Handler) Create(ctx context.Context, i ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Query(ctx context.Context, i ...interface{}) (*models.Response, error) {
func (h *Handler) Query(ctx context.Context, i ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Update(ctx context.Context, i ...interface{}) (*models.Response, error) {
func (h *Handler) Update(ctx context.Context, i ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Delete(ctx context.Context, i ...interface{}) (*models.Response, error) {
func (h *Handler) Delete(ctx context.Context, i ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Begin(ctx context.Context, i ...interface{}) (*models.Response, error) {
func (h *Handler) Begin(ctx context.Context, i ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Execute(ctx context.Context, i ...interface{}) (*models.Response, error) {
func (h *Handler) Execute(ctx context.Context, i ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Rollback(ctx context.Context, i ...interface{}) (*models.Response, error) {
func (h *Handler) Rollback(ctx context.Context, i ...interface{}) (*models.Response, error) {
return nil, nil
}

func (h *Handle) Configure(ctx context.Context, i ...interface{}) error {
func (h *Handler) Configure(ctx context.Context, i ...interface{}) error {
return nil
}

func (h *Handle) Close() error {
func (h *Handler) Close() error {
return nil
}
Loading

0 comments on commit 19374e8

Please sign in to comment.