Skip to content

Commit

Permalink
feat(bun): add bun based datastore
Browse files Browse the repository at this point in the history
  • Loading branch information
rafael-piovesan committed Dec 22, 2021
1 parent d750014 commit 1cb5c2d
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 174 deletions.
16 changes: 16 additions & 0 deletions adapters/datastore/audit_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package datastore

import (
"context"

"github.com/rafael-piovesan/go-rocket-ride/entity"
)

func (s *sqlStore) CreateAuditRecord(ctx context.Context, ar *entity.AuditRecord) (*entity.AuditRecord, error) {
_, err := s.db.NewInsert().
Model(ar).
Returning("*").
Exec(ctx)

return ar, err
}
43 changes: 43 additions & 0 deletions adapters/datastore/idempotency_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package datastore

import (
"context"

"github.com/rafael-piovesan/go-rocket-ride/entity"
)

func (s *sqlStore) CreateIdempotencyKey(
ctx context.Context,
ik *entity.IdempotencyKey,
) (*entity.IdempotencyKey, error) {
_, err := s.db.NewInsert().
Model(ik).
Returning("*").
Exec(ctx)

return ik, err
}

func (s *sqlStore) GetIdempotencyKey(ctx context.Context, key string, userID int64) (*entity.IdempotencyKey, error) {
ik := entity.IdempotencyKey{}
err := s.db.NewSelect().
Model(&ik).
Where("idempotency_key = ? AND user_id = ?", key, userID).
Limit(1).
Scan(ctx)

return &ik, err
}

func (s *sqlStore) UpdateIdempotencyKey(
ctx context.Context,
ik *entity.IdempotencyKey,
) (*entity.IdempotencyKey, error) {
_, err := s.db.NewUpdate().
Model(ik).
WherePK().
Returning("*").
Exec(ctx)

return ik, err
}
37 changes: 37 additions & 0 deletions adapters/datastore/ride.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package datastore

import (
"context"

"github.com/rafael-piovesan/go-rocket-ride/entity"
)

func (s *sqlStore) CreateRide(ctx context.Context, rd *entity.Ride) (*entity.Ride, error) {
_, err := s.db.NewInsert().
Model(rd).
Returning("*").
Exec(ctx)

return rd, err
}

func (s *sqlStore) GetRideByIdempotencyKeyID(ctx context.Context, keyID int64) (*entity.Ride, error) {
r := entity.Ride{}
err := s.db.NewSelect().
Model(&r).
Where("idempotency_key_id = ?", keyID).
Limit(1).
Scan(ctx)

return &r, err
}

func (s *sqlStore) UpdateRide(ctx context.Context, rd *entity.Ride) (*entity.Ride, error) {
_, err := s.db.NewUpdate().
Model(rd).
WherePK().
Returning("*").
Exec(ctx)

return rd, err
}
53 changes: 53 additions & 0 deletions adapters/datastore/sqlstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package datastore

import (
"context"
"database/sql"
"fmt"

"github.com/uptrace/bun"

rocketride "github.com/rafael-piovesan/go-rocket-ride"
)

type sqlStore struct {
conn *bun.DB
db bun.IDB
}

func NewStore(db *bun.DB) rocketride.Datastore {
return &sqlStore{
conn: db,
db: db,
}
}

func (s *sqlStore) Atomic(ctx context.Context, fn func(store rocketride.Datastore) error) (err error) {
tx, err := s.conn.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
return err
}

defer func() {
if p := recover(); p != nil {
err = fmt.Errorf("panic err: %v", p)
}
if err != nil {
if rbErr := tx.Rollback(); rbErr != nil {
err = fmt.Errorf("tx err: %v, rb err: %v", err, rbErr)
}
} else {
err = tx.Commit()
}
}()

// TODO: check if it works for nested transactions as well
newStore := &sqlStore{
conn: s.conn,
db: tx,
}
err = fn(newStore)
return err
}

var _ rocketride.Datastore = (*sqlStore)(nil)
16 changes: 16 additions & 0 deletions adapters/datastore/staged_job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package datastore

import (
"context"

"github.com/rafael-piovesan/go-rocket-ride/entity"
)

func (s *sqlStore) CreateStagedJob(ctx context.Context, sj *entity.StagedJob) (*entity.StagedJob, error) {
_, err := s.db.NewInsert().
Model(sj).
Returning("*").
Exec(ctx)

return sj, err
}
55 changes: 20 additions & 35 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,65 @@ module github.com/rafael-piovesan/go-rocket-ride
go 1.17

require (
github.com/brianvoe/gofakeit/v6 v6.10.0
github.com/docker/go-connections v0.4.0
github.com/go-testfixtures/testfixtures/v3 v3.6.1
github.com/golang-migrate/migrate/v4 v4.15.1
github.com/kyleconroy/sqlc v1.11.0
github.com/lib/pq v1.10.4
github.com/segmentio/golines v0.6.0
github.com/stretchr/testify v1.7.0
github.com/tabbed/pqtype v0.1.1
github.com/testcontainers/testcontainers-go v0.12.0
github.com/uptrace/bun/driver/pgdriver v1.0.20
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jackc/pgx/v4 v4.14.0 // indirect
github.com/lib/pq v1.10.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
mellium.im/sasl v0.2.1 // indirect
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/Microsoft/hcsshim v0.8.21 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/antlr/antlr4 v0.0.0-20200209180723-1177c0b58d07 // indirect
github.com/brianvoe/gofakeit/v6 v6.10.0
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/containerd/cgroups v1.0.1 // indirect
github.com/containerd/containerd v1.5.7 // indirect
github.com/dave/dst v0.26.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v20.10.11+incompatible // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/moby/sys/mount v0.2.0 // indirect
github.com/moby/sys/mountinfo v0.5.0 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v1.0.2 // indirect
github.com/pganalyze/pg_query_go/v2 v2.1.0 // indirect
github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63 // indirect
github.com/pingcap/log v0.0.0-20210906054005-afc726e70354 // indirect
github.com/pingcap/parser v0.0.0-20210914110036-002913dd28ec // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/cobra v1.2.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/x-cray/logrus-prefixed-formatter v0.5.2 // indirect
github.com/tabbed/pqtype v0.1.1
github.com/uptrace/bun v1.0.20
github.com/uptrace/bun/dialect/pgdialect v1.0.20
go.opencensus.io v0.23.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.19.1 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20211108170745-6635138e15ea // indirect
golang.org/x/sys v0.0.0-20211109184856-51b60fd695b3 // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
google.golang.org/genproto v0.0.0-20211013025323-ce878158c4d4 // indirect
google.golang.org/grpc v1.41.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit 1cb5c2d

Please sign in to comment.