Skip to content

Commit

Permalink
Added FindOneAndDeleteById, FindOneAndReplaceById, FindOneAndUpdateBy…
Browse files Browse the repository at this point in the history
…Id and global option
  • Loading branch information
Gabriel Cataldo committed Feb 18, 2024
1 parent 727e3e6 commit 420b420
Show file tree
Hide file tree
Showing 19 changed files with 845 additions and 388 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ MongoDB Template
<!--suppress ALL -->
<img align="right" src="gopher-mongo.png" alt="">

[![Project status](https://img.shields.io/badge/version-v1.2.0-vividgreen.svg)](https://github.com/GabrielHCataldo/go-mongo-template/releases/tag/v1.2.0)
[![Project status](https://img.shields.io/badge/version-v1.2.1-vividgreen.svg)](https://github.com/GabrielHCataldo/go-mongo-template/releases/tag/v1.2.1)
[![Go Report Card](https://goreportcard.com/badge/github.com/GabrielHCataldo/go-mongo-template)](https://goreportcard.com/report/github.com/GabrielHCataldo/go-mongo-template)
[![Coverage Status](https://coveralls.io/repos/GabrielHCataldo/go-mongo-template/badge.svg?branch=main&service=github)](https://coveralls.io/github/GabrielHCataldo/go-mongo?branch=main)
[![Open Source Helpers](https://www.codetriage.com/gabrielhcataldo/go-mongo-template/badges/users.svg)](https://www.codetriage.com/gabrielhcataldo/go-mongo)
Expand Down
2 changes: 1 addition & 1 deletion _example/insert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func insertOneManualCloseSession() {
CreatedAt: primitive.NewDateTimeFromTime(time.Now()),
}
//new document need a pointer
err = mongoTemplate.InsertOne(ctx, &testDocument, option.NewInsertOne().
err = mongoTemplate.InsertOne(ctx, &testDocument, option.InsertOne().
SetForceRecreateSession(false).SetDisableAutoCloseSession(true))
if helper.IsNotNil(err) {
logger.Error("error insert document:", err)
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ module github.com/GabrielHCataldo/go-mongo-template
go 1.21.3

require (
github.com/GabrielHCataldo/go-errors v1.1.2
github.com/GabrielHCataldo/go-helper v1.3.7
github.com/GabrielHCataldo/go-logger v1.2.4
go.mongodb.org/mongo-driver v1.13.1
github.com/GabrielHCataldo/go-helper v1.4.7
github.com/GabrielHCataldo/go-logger v1.2.7
go.mongodb.org/mongo-driver v1.14.0
)

require (
Expand Down
24 changes: 6 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
github.com/GabrielHCataldo/go-errors v1.1.2 h1:pzfegYVLH6+7pUzymcSU2HreUWiahGTJCOz+FMtgqyQ=
github.com/GabrielHCataldo/go-errors v1.1.2/go.mod h1:tJH0y1gLoR8uJS5SeuMpBzmcasZpI00j/PnH4HXTHpE=
github.com/GabrielHCataldo/go-helper v1.3.7 h1:aAUdFNJJyx6YCx9q4+1qaO/jmBkztDwwZiLtnip0kcI=
github.com/GabrielHCataldo/go-helper v1.3.7/go.mod h1:+whI36aUg1BfvLpZWJ8j81+d5iPuxl58ApOJqTIliCU=
github.com/GabrielHCataldo/go-logger v1.2.4 h1:a0IrcAdBa0dh4UUvQEy2kGbif3gGzpvjmPA4nIJOEVA=
github.com/GabrielHCataldo/go-logger v1.2.4/go.mod h1:1Vn/557fErplbuV+1jT/5U4Xi6/8+6cyZaj8niSv2uA=
github.com/GabrielHCataldo/go-helper v1.4.7 h1:E7F0KXQYia2efeFoIKd/qgVvfjbrCujRxsyCzlnm8dk=
github.com/GabrielHCataldo/go-helper v1.4.7/go.mod h1:+whI36aUg1BfvLpZWJ8j81+d5iPuxl58ApOJqTIliCU=
github.com/GabrielHCataldo/go-logger v1.2.7 h1:SNAjQxLzPIuk8W1I892hxOthoB7dxLMwtQRARaJd0Vg=
github.com/GabrielHCataldo/go-logger v1.2.7/go.mod h1:1Vn/557fErplbuV+1jT/5U4Xi6/8+6cyZaj8niSv2uA=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -19,17 +17,14 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.17.0 h1:SmVVlfAOtlZncTxRuinDPomC2DkXJ4E5T9gDA0AIH74=
github.com/go-playground/validator/v10 v10.17.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc=
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
github.com/klassmann/cpfcnpj v0.0.0-20200907140233-a595c5fd8de1 h1:nT1t/3YnkjBWdVl6zmvmim6S8gjAZOpZi19iEBq3/Ko=
github.com/klassmann/cpfcnpj v0.0.0-20200907140233-a595c5fd8de1/go.mod h1:2lGFirXS+qsYDFtk4OAzWXyILL3mrSAluEH26Ao65ZY=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/leekchan/accounting v1.0.0 h1:+Wd7dJ//dFPa28rc1hjyy+qzCbXPMR91Fb6F1VGTQHg=
Expand All @@ -38,7 +33,6 @@ github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/nyaruka/phonenumbers v1.3.0 h1:IFyyJfF2Elg8xGKFghWrRXzb6qAHk+Q3uPqmIgS20JQ=
Expand All @@ -65,23 +59,20 @@ github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk=
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk=
go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo=
go.mongodb.org/mongo-driver v1.14.0 h1:P98w8egYRjYe3XDjxhYJagTokP/H6HzlsnojRgZRd80=
go.mongodb.org/mongo-driver v1.14.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
Expand All @@ -92,7 +83,6 @@ golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -102,10 +92,8 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
93 changes: 13 additions & 80 deletions mongo/error.go
Original file line number Diff line number Diff line change
@@ -1,82 +1,15 @@
package mongo

import "github.com/GabrielHCataldo/go-errors/errors"

var MsgErrRefDocument = "mongo: ref document needs to be structure or slice of the struct"
var MsgErrDatabaseNotConfigured = "mongo: database not correct configured"
var MsgErrCollectionNotConfigured = "mongo: collection not correct configured"
var MsgErrDocumentIsNotPointer = "mongo: document param is not a pointer"
var MsgErrDocumentIsNotStruct = "mongo: document param is not a struct"
var MsgErrDocumentIsEmpty = "mongo: document param is empty"
var MsgErrDocumentsIsEmpty = "mongo: documents param is empty"
var MsgErrDestIsNotPointer = "mongo: dest param is not a pointer"
var MsgErrDestIsNotStruct = "mongo: dest param is not a struct"
var MsgErrNoDocuments = "mongo: no documents in result"
var MsgErrNoOpenSession = "mongo: no open session"

var ErrRefDocument = errors.New(MsgErrRefDocument)
var ErrDatabaseNotConfigured = errors.New(MsgErrDatabaseNotConfigured)
var ErrCollectionNotConfigured = errors.New(MsgErrCollectionNotConfigured)
var ErrDocumentIsNotPointer = errors.New(MsgErrDocumentIsNotPointer)
var ErrDocumentIsNotStruct = errors.New(MsgErrDocumentIsNotStruct)
var ErrDocumentIsEmpty = errors.New(MsgErrDocumentIsEmpty)
var ErrDocumentsIsEmpty = errors.New(MsgErrDocumentsIsEmpty)
var ErrDestIsNotPointer = errors.New(MsgErrDestIsNotPointer)
var ErrDestIsNotStruct = errors.New(MsgErrDestIsNotStruct)
var ErrNoDocuments = errors.New(MsgErrNoDocuments)
var ErrNoOpenSession = errors.New(MsgErrNoOpenSession)

func errRefDocument(skip int) error {
ErrRefDocument = errors.NewSkipCaller(skip+1, MsgErrRefDocument)
return ErrRefDocument
}

func errDatabaseNotConfigured(skip int) error {
ErrDatabaseNotConfigured = errors.NewSkipCaller(skip+1, MsgErrDatabaseNotConfigured)
return ErrDatabaseNotConfigured
}

func errCollectionNotConfigured(skip int) error {
ErrCollectionNotConfigured = errors.NewSkipCaller(skip+1, MsgErrCollectionNotConfigured)
return ErrCollectionNotConfigured
}

func errDocumentIsNotPointer(skip int) error {
ErrDocumentIsNotPointer = errors.NewSkipCaller(skip+1, MsgErrDocumentIsNotPointer)
return ErrDocumentIsNotPointer
}

func errDocumentIsNotStruct(skip int) error {
ErrDocumentIsNotStruct = errors.NewSkipCaller(skip+1, MsgErrDocumentIsNotStruct)
return ErrDocumentIsNotStruct
}

func errDocumentIsEmpty(skip int) error {
ErrDocumentIsEmpty = errors.NewSkipCaller(skip+1, MsgErrDocumentIsEmpty)
return ErrDocumentIsEmpty
}

func errDocumentsIsEmpty(skip int) error {
ErrDocumentsIsEmpty = errors.NewSkipCaller(skip+1, MsgErrDocumentsIsEmpty)
return ErrDocumentsIsEmpty
}

func errDestIsNotPointer(skip int) error {
ErrDestIsNotPointer = errors.NewSkipCaller(skip+1, MsgErrDestIsNotPointer)
return ErrDestIsNotPointer
}

func errDestIsNotStruct(skip int) error {
ErrDestIsNotStruct = errors.NewSkipCaller(skip+1, MsgErrDestIsNotStruct)
return ErrDestIsNotStruct
}

func errNoDocuments(skip int) error {
ErrNoDocuments = errors.NewSkipCaller(skip+1, MsgErrNoDocuments)
return ErrNoDocuments
}

func errNoOpenSession(skip int) error {
ErrNoOpenSession = errors.NewSkipCaller(skip+1, MsgErrNoOpenSession)
return ErrNoOpenSession
}
import "errors"

var ErrRefDocument = errors.New("mongo: ref document needs to be structure or slice of the struct")
var ErrDatabaseNotConfigured = errors.New("mongo: database not correct configured")
var ErrCollectionNotConfigured = errors.New("mongo: collection not correct configured")
var ErrDocumentIsNotPointer = errors.New("mongo: document param is not a pointer")
var ErrDocumentIsNotStruct = errors.New("mongo: document param is not a struct")
var ErrDocumentIsEmpty = errors.New("mongo: document param is empty")
var ErrDocumentsIsEmpty = errors.New("mongo: documents param is empty")
var ErrDestIsNotPointer = errors.New("mongo: dest param is not a pointer")
var ErrDestIsNotStruct = errors.New("mongo: dest param is not a struct")
var ErrNoDocuments = errors.New("mongo: no documents in result")
var ErrNoOpenSession = errors.New("mongo: no open session")
Loading

0 comments on commit 420b420

Please sign in to comment.