Skip to content

Commit

Permalink
Merge pull request #219 from Revolyssup/postgres
Browse files Browse the repository at this point in the history
Add logic for postgres
  • Loading branch information
Revolyssup authored Oct 4, 2022
2 parents f240c46 + 8c309cb commit 5aa4cb7
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 17 deletions.
24 changes: 19 additions & 5 deletions database/database.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package database

import (
"fmt"
"sync"

"github.com/layer5io/meshkit/logger"
"gorm.io/driver/postgres"
sqlite "gorm.io/driver/sqlite"
gormpkg "gorm.io/gorm"
"gorm.io/gorm"
)

const (
Expand All @@ -14,6 +16,10 @@ const (
)

type Options struct {
Username string `json:"username,omitempty"`
Host string `json:"host,omitempty"`
Port string `json:"port,omitempty"`
Password string `json:"password,omitempty"`
Filename string `json:"filename,omitempty"`
Engine string `json:"engine,omitempty"`
Logger logger.Handler
Expand All @@ -27,7 +33,7 @@ type Model struct {
}

type Handler struct {
*gormpkg.DB
*gorm.DB
*sync.Mutex
// Implement methods if necessary
}
Expand All @@ -46,14 +52,22 @@ func (h *Handler) DBClose() error {
func New(opts Options) (Handler, error) {
switch opts.Engine {
case POSTGRES:
return Handler{}, ErrNoneDatabase
dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s", opts.Host, opts.Username, opts.Password, opts.Port)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return Handler{}, ErrDatabaseOpen(err)
}
return Handler{
db,
&sync.Mutex{},
}, nil
case SQLITE:
config := &gormpkg.Config{}
config := &gorm.Config{}
if opts.Logger != nil {
config.Logger = opts.Logger.DatabaseLogger()
}

db, err := gormpkg.Open(sqlite.Open(opts.Filename), config)
db, err := gorm.Open(sqlite.Open(opts.Filename), config)
if err != nil {
return Handler{}, ErrDatabaseOpen(err)
}
Expand Down
17 changes: 13 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ require (
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.11.0
gopkg.in/yaml.v2 v2.4.0
gorm.io/driver/postgres v1.3.10
gorm.io/driver/sqlite v1.3.1
gorm.io/gorm v1.23.4
gorm.io/gorm v1.23.7
helm.sh/helm/v3 v3.8.2
k8s.io/api v0.23.5
k8s.io/apimachinery v0.23.5
Expand Down Expand Up @@ -100,6 +101,14 @@ require (
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // 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/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.4 // indirect
Expand Down Expand Up @@ -159,7 +168,7 @@ require (
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.7.1 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
Expand All @@ -168,7 +177,7 @@ require (
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
go.opencensus.io v0.23.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
Expand All @@ -185,7 +194,7 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.23.5 // indirect
k8s.io/apiserver v0.23.5 // indirect
k8s.io/component-base v0.23.5 // indirect
Expand Down
Loading

0 comments on commit 5aa4cb7

Please sign in to comment.