Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pgsqlx support #17

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions example/testapp/go.mod
Original file line number Diff line number Diff line change
@@ -1,46 +1,37 @@
module testapp

go 1.18
go 1.21.1

require (
github.com/go-sql-driver/mysql v1.7.1
github.com/google/uuid v1.3.0
github.com/google/uuid v1.3.1
github.com/gorilla/mux v1.8.0
github.com/hanagantig/gracy v0.0.0-20220629074052-41c110a9e5e2
github.com/jackc/pgx/v4 v4.18.1
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.9
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.2
go.uber.org/zap v1.24.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.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.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
200 changes: 36 additions & 164 deletions example/testapp/go.sum

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions example/testapp/goro.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ app:
storages:
- mysql
- mysqlx
- postgres
- pgsqlx

handlers:
- http
Expand Down Expand Up @@ -44,10 +44,14 @@ adapters:
- GetOne
- GetAll
- Save
- Update

- name: ClientRepo
storage: mysqlx
methods:
- GitByDate
- GetByID

- name: UserRepo
storage: pgsqlx
methods:
- GetByID

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions example/testapp/internal/adapter/pgsqlxrepo/transactor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package pgsqlxrepo

import (
"context"

"errors"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)

type contextKey string

const txKey contextKey = "sql_tx"
const txIDKey contextKey = "tx_id"

type Transactor struct {
conn *sqlx.DB
wraps map[context.Context][]func(ctx context.Context) error
}

func NewTransactor(c *sqlx.DB) Transactor {
return Transactor{conn: c}
}

func (t *Transactor) NewTxContext(ctx context.Context) context.Context {
return context.WithValue(ctx, txIDKey, uuid.NewString())
}

func (t *Transactor) hasTxID(ctx context.Context) bool {
txID := ctx.Value(txIDKey)
return txID != nil && txID != ""
}

func (t *Transactor) InTransaction(ctx context.Context, txFunc func(ctx context.Context) error) error {
if !t.hasTxID(ctx) {
return errors.New("not transaction context. Please create it with NewTxContext")
}

if _, ok := t.wraps[ctx]; !ok {
t.wraps[ctx] = make([]func(ctx context.Context) error, 0, 0)
}

t.wraps[ctx] = append(t.wraps[ctx], txFunc)

return nil
}

func (t *Transactor) GetConn(ctx context.Context) *sqlx.DB {
conn, ok := ctx.Value(txKey).(*sqlx.DB)
if !ok {
return t.conn
}

return conn
}

func (t *Transactor) RunTransaction(ctx context.Context) error {
defer t.reset(ctx)

tx, err := t.conn.BeginTx(ctx, nil)
if err != nil {
return err
}

txCtx := context.WithValue(ctx, txKey, tx)
for _, wrap := range t.wraps[ctx] {
err = wrap(txCtx)
if err != nil {
err = tx.Rollback()
if err != nil {
return err
}
}
}

err = tx.Commit()
if err != nil {
return err
}

return nil
}

func (t *Transactor) reset(ctx context.Context) {
delete(t.wraps, ctx)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package myrepo
package userrepo

import (
"context"
)

func (r *Repository) Update(ctx context.Context) {
func (r *Repository) GetByID(ctx context.Context) {
// TODO: put your repository logic here
panic("implement me")
}
20 changes: 20 additions & 0 deletions example/testapp/internal/adapter/pgsqlxrepo/userrepo/repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions example/testapp/internal/app/app.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions example/testapp/internal/app/container.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 11 additions & 40 deletions example/testapp/internal/app/database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/testapp/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewConfig(filePath string) (Config, error) {
return conf, err
}

viper.SetDefault("app.name", "dobby")
viper.SetDefault("app.name", "testapp")
viper.SetDefault("app.env", "dev")
viper.SetDefault("app.version", "v1")
viper.SetDefault("http.read_timeout", "1s")
Expand Down
Loading