Skip to content

Commit

Permalink
refactor mapper; update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
alexferl committed Jan 10, 2024
1 parent 57cad5f commit b3de3ed
Show file tree
Hide file tree
Showing 36 changed files with 1,359 additions and 1,374 deletions.
18 changes: 18 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
quiet: False
with-expecter: True
mockname: "{{.InterfaceName}}"
filename: "{{.MockName}}.go"
outpkg: mocks
dir: mocks/
packages:
github.com/alexferl/echo-boilerplate/data:
interfaces:
IMapper:


#with-expecter: True
#inpackage: True
#dir: mocks/{{ replaceAll .InterfaceDirRelative "internal" "internal_" }}
#mockname: "{{.InterfaceName}}"
#outpkg: "{{.PackageName}}"
#filename: "{{.InterfaceName}}.go"
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: dev run test cover fmt generate openapi-lint pre-commit docker-build docker-run
.PHONY: dev run test cover fmt mocks openapi-lint pre-commit docker-build docker-run

.DEFAULT: help
help:
Expand All @@ -16,8 +16,8 @@ help:
@echo " run go mod tidy"
@echo "make fmt"
@echo " run gofumpt"
@echo "make generate"
@echo " run go generate"
@echo "make mocks"
@echo " run mockery"
@echo "make openapi-lint"
@echo " lint openapi spec"
@echo "make pre-commit"
Expand Down Expand Up @@ -71,8 +71,8 @@ tidy:
fmt: check-gofumpt
gofumpt -l -w .

generate:
go generate ./...
mocks:
mockery

openapi-lint: check-redocly
redocly lint openapi/openapi.yaml
Expand Down
184 changes: 174 additions & 10 deletions data/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,184 @@ package data

import (
"context"
"errors"

"github.com/rs/zerolog/log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
)

//go:generate mockery --output=../mocks --name Mapper
type Mapper interface {
Collection(name string) Mapper
Insert(ctx context.Context, document any, result any, opts ...*options.InsertOneOptions) (any, error)
var ErrNoDocuments = errors.New("no documents in result")

type IMapper interface {
Aggregate(ctx context.Context, pipeline mongo.Pipeline, results any, opts ...*options.AggregateOptions) (any, error)
Count(ctx context.Context, filter any, opts ...*options.CountOptions) (int64, error)
Find(ctx context.Context, filter any, results any, opts ...*options.FindOptions) (any, error)
FindOne(ctx context.Context, filter any, result any, opts ...*options.FindOneOptions) (any, error)
FindOneAndUpdate(ctx context.Context, filter any, update any, result any, opts ...*options.FindOneAndUpdateOptions) (any, error)
FindOneByIdAndUpdate(ctx context.Context, id string, update any, result any, opts ...*options.FindOneAndUpdateOptions) (any, error)
FindOneById(ctx context.Context, id string, result any, opts ...*options.FindOneOptions) (any, error)
Find(ctx context.Context, filter any, result any, opts ...*options.FindOptions) (any, error)
Aggregate(ctx context.Context, filter any, limit int, skip int, result any, opts ...*options.AggregateOptions) (any, error)
Count(ctx context.Context, filter any, opts ...*options.CountOptions) (int64, error)
Update(ctx context.Context, filter any, update any, result any, opts ...*options.UpdateOptions) (any, error)
UpdateById(ctx context.Context, id string, document any, result any, opts ...*options.UpdateOptions) (any, error)
Upsert(ctx context.Context, filter any, update any, result any, opts ...*options.FindOneAndUpdateOptions) (any, error)
InsertOne(ctx context.Context, document any, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
UpdateOne(ctx context.Context, filter any, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateOneById(ctx context.Context, id string, document any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
WithCollection(name string) IMapper
}

type Mapper struct {
client *mongo.Client
db *mongo.Database
dbName string
collection *mongo.Collection
}

func NewMapper(client *mongo.Client, databaseName string, collectionName string) IMapper {
db := client.Database(databaseName)
collection := db.Collection(collectionName)
return &Mapper{
client,
db,
databaseName,
collection,
}
}

func (m *Mapper) Aggregate(ctx context.Context, pipeline mongo.Pipeline, results any, opts ...*options.AggregateOptions) (any, error) {
cur, err := m.collection.Aggregate(ctx, pipeline, opts...)
if err != nil {
return nil, err
}

defer func(cur *mongo.Cursor, ctx context.Context) {
err := cur.Close(ctx)
if err != nil {
log.Error().Err(err).Msg("cursor error")
}
}(cur, ctx)

err = cur.All(ctx, &results)
if err != nil {
return nil, err
}

if err = cur.Err(); err != nil {
return nil, err
}

return results, nil
}

func (m *Mapper) Count(ctx context.Context, filter any, opts ...*options.CountOptions) (int64, error) {
if filter == nil {
filter = bson.D{}
}

count, err := m.collection.CountDocuments(ctx, filter, opts...)
if err != nil {
return 0, err
}

return count, nil
}

func (m *Mapper) Find(ctx context.Context, filter any, results any, opts ...*options.FindOptions) (any, error) {
if filter == nil {
filter = bson.D{}
}

cur, err := m.collection.Find(ctx, filter, opts...)
if err != nil {
return nil, err
}

defer func(cur *mongo.Cursor, ctx context.Context) {
err := cur.Close(ctx)
if err != nil {
log.Error().Err(err).Msg("cursor error")
}
}(cur, ctx)

err = cur.All(ctx, &results)
if err != nil {
return nil, err
}

if err = cur.Err(); err != nil {
return nil, err
}

return results, nil
}

func (m *Mapper) FindOne(ctx context.Context, filter any, result any, opts ...*options.FindOneOptions) (any, error) {
err := m.collection.FindOne(ctx, filter, opts...).Decode(result)
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, ErrNoDocuments
} else if err != nil {
return nil, err
}

return result, nil
}

func (m *Mapper) FindOneAndUpdate(ctx context.Context, filter any, update any, result any, opts ...*options.FindOneAndUpdateOptions) (any, error) {
opts = append(opts, options.FindOneAndUpdate().SetReturnDocument(options.After))
res := m.collection.FindOneAndUpdate(ctx, filter, bson.D{{"$set", update}}, opts...)
if res.Err() != nil {
return nil, res.Err()
}

if result != nil {
err := res.Decode(result)
if err != nil {
return nil, err
}
}

return result, nil
}

func (m *Mapper) FindOneByIdAndUpdate(ctx context.Context, id string, update any, result any, opts ...*options.FindOneAndUpdateOptions) (any, error) {
return m.FindOneAndUpdate(ctx, bson.D{{"id", id}}, update, result, opts...)
}

func (m *Mapper) FindOneById(ctx context.Context, id string, result any, opts ...*options.FindOneOptions) (any, error) {
filter := bson.D{{"id", id}}
return m.FindOne(ctx, filter, result, opts...)
}

func (m *Mapper) InsertOne(ctx context.Context, document any, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) {
res, err := m.collection.InsertOne(ctx, document, opts...)
return res, err
}

func (m *Mapper) UpdateOne(ctx context.Context, filter any, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
res, err := m.collection.UpdateOne(ctx, filter, update, opts...)
if err != nil {
return nil, err
}
return res, nil
}

func (m *Mapper) UpdateOneById(ctx context.Context, id string, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
return m.UpdateOne(ctx, bson.D{{"id", id}}, bson.D{{"$set", update}}, opts...)
}

func (m *Mapper) WithCollection(name string) IMapper {
return NewMapper(m.client, m.dbName, name)
}

func (m *Mapper) getSession() (mongo.Session, *options.TransactionOptions, error) {
wc := writeconcern.Majority()
rc := readconcern.Snapshot()
txnOpts := options.Transaction().SetWriteConcern(wc).SetReadConcern(rc)

session, err := m.client.StartSession()
if err != nil {
return nil, nil, err
}

return session, txnOpts, nil
}
73 changes: 35 additions & 38 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,82 +11,79 @@ require (
github.com/alexferl/golib/http/api v0.0.0-20231008015603-26982fa3ee12
github.com/alexferl/golib/log v0.0.0-20231008015603-26982fa3ee12
github.com/alexferl/httplink v0.1.0
github.com/casbin/casbin/v2 v2.77.2
github.com/labstack/echo/v4 v4.11.1
github.com/lestrrat-go/jwx/v2 v2.0.13
github.com/matthewhartstonge/argon2 v0.3.4
github.com/casbin/casbin/v2 v2.81.0
github.com/labstack/echo/v4 v4.11.4
github.com/lestrrat-go/jwx/v2 v2.0.19
github.com/matthewhartstonge/argon2 v1.0.0
github.com/rs/xid v1.5.0
github.com/rs/zerolog v1.31.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.17.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
go.mongodb.org/mongo-driver v1.12.1
go.mongodb.org/mongo-driver v1.13.1
go.uber.org/automaxprocs v1.5.3
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/oauth2 v0.13.0
golang.org/x/exp v0.0.0-20240110193028-0dcbfd608b1e
golang.org/x/oauth2 v0.16.0
)

require (
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
github.com/casbin/govaluate v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/getkin/kin-openapi v0.120.0 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/getkin/kin-openapi v0.107.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/swag v0.19.5 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/invopop/yaml v0.1.0 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
github.com/lestrrat-go/httprc v1.0.4 // indirect
github.com/lestrrat-go/iter v1.0.2 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tidwall/gjson v1.17.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit b3de3ed

Please sign in to comment.