Skip to content

Commit

Permalink
add(orm): GORM connection initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Himitsuko committed Nov 11, 2022
1 parent 6665501 commit 9266a70
Show file tree
Hide file tree
Showing 3 changed files with 341 additions and 21 deletions.
58 changes: 58 additions & 0 deletions database/gorm/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package connection

import (
"fmt"
"github.com/caarlos0/env/v6"
"go.uber.org/zap"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"log"
"moul.io/zapgorm2"
)

type Config struct {
URL string `env:"DATABASE_URL"`
Host string `env:"DATABASE_HOST"`
Port int32 `env:"DATABASE_PORT"`
User string `env:"DATABASE_USER"`
Password string `env:"DATABASE_PASSWORD"`
Database string `env:"DATABASE_DB"`
Environment string `env:"GIN_MODE"`
AppName string `env:"APP_NAME"`
}

func NewConnection(log *zap.Logger, sch *string) *gorm.DB {
config := parseConfig()
connection := config.ToConnectionURI()
logger := zapgorm2.New(log)
logger.SetAsDefault()

public := "public"
if sch == nil {
sch = &public
}
db, err := gorm.Open(postgres.Open(connection), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: *sch + ".",
SingularTable: false,
},
Logger: logger,
})
if err != nil {
log.Error(err.Error())
}
return db
}

func (c Config) ToConnectionURI() string {
return fmt.Sprintf(`postgresql://%s:%s@%s:%v/%s?application_name=%s`, c.User, c.Password, c.Host, c.Port, c.Database, c.AppName)
}

func parseConfig() *Config {
c := Config{}
if err := env.Parse(&c); err != nil {
log.Fatal(err)
}
return &c
}
29 changes: 23 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,30 @@ module github.com/depocket/support

go 1.18

require github.com/samber/lo v1.21.0
require (
github.com/samber/lo v1.21.0
go.uber.org/zap v1.23.0
gorm.io/driver/postgres v1.4.5
gorm.io/gorm v1.24.1
moul.io/zapgorm2 v1.1.3
)

require (
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/go-ethereum v1.10.25 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
github.com/caarlos0/env/v6 v6.10.1 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/pgx/v4 v4.17.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/exp v0.0.0-20220426173459-3bcf042a4bf5 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
)
Loading

0 comments on commit 9266a70

Please sign in to comment.