Skip to content

Commit

Permalink
Added Simple Disconnect to use on defer call
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Cataldo committed Jan 15, 2024
1 parent b118ca2 commit 6da4e8e
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 45 deletions.
36 changes: 18 additions & 18 deletions 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.0.3-vividgreen.svg)](https://github.com/GabrielHCataldo/go-mongo-template/releases/tag/v1.0.3)
[![Project status](https://img.shields.io/badge/version-v1.0.4-vividgreen.svg)](https://github.com/GabrielHCataldo/go-mongo-template/releases/tag/v1.0.4)
[![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 Expand Up @@ -107,7 +107,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
testDocument := test{
Random: rand.Int(),
Name: "Foo Bar",
Expand Down Expand Up @@ -157,7 +157,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
testDocuments := []*test{
{
Random: rand.Int(),
Expand Down Expand Up @@ -218,7 +218,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
update := bson.M{"$set": bson.M{"name": "Foo Bar Updated"}}
updateResult, err := mongoTemplate.UpdateOne(ctx, filter, update, test{})
Expand Down Expand Up @@ -258,7 +258,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
objectId, _ := primitive.ObjectIDFromHex("6585f3a8bf2af8ad9bcab912")
update := bson.M{"$set": bson.M{"name": "Foo Bar Updated"}}
updateResult, err := mongoTemplate.UpdateOneById(ctx, objectId, update, test{})
Expand Down Expand Up @@ -299,7 +299,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
update := bson.M{"$set": bson.M{"name": "Foo Bar Updated"}}
updateResult, err := mongoTemplate.UpdateOne(ctx, filter, update, test{})
Expand Down Expand Up @@ -344,7 +344,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
replacement := test{
Random: rand.Int(),
Expand Down Expand Up @@ -395,7 +395,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
deleteResult, err := mongoTemplate.DeleteOne(ctx, filter, test{})
if err != nil {
Expand Down Expand Up @@ -434,7 +434,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
objectId, _ := primitive.ObjectIDFromHex("6585f3a8bf2af8ad9bcab912")
deleteResult, err := mongoTemplate.DeleteOneById(ctx, objectId, test{})
if err != nil {
Expand Down Expand Up @@ -473,7 +473,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
deleteResult, err := mongoTemplate.DeleteMany(ctx, filter, test{})
if err != nil {
Expand Down Expand Up @@ -514,7 +514,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
//dest need a pointer
var dest []test
err = mongoTemplate.FindAll(ctx, &dest)
Expand Down Expand Up @@ -554,7 +554,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
pageOutput, err := mongoTemplate.FindPageable(ctx, filter, mongo.PageInput{
Page: 0,
Expand Down Expand Up @@ -597,7 +597,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
objectId, _ := primitive.ObjectIDFromHex("6585db26633e225cbeadf553")
//dest need a pointer
var dest test
Expand Down Expand Up @@ -641,7 +641,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
objectId, _ := primitive.ObjectIDFromHex("6585f5b8fd8fa97d562419f7")
exists, err := mongoTemplate.ExistsById(ctx, objectId, test{})
if err != nil {
Expand Down Expand Up @@ -680,7 +680,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
exists, err := mongoTemplate.Exists(ctx, filter, test{})
if err != nil {
Expand Down Expand Up @@ -723,7 +723,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
countResult, err := mongoTemplate.EstimatedDocumentCount(ctx, test{})
if err != nil {
logger.Error("error estimated document count:", err)
Expand Down Expand Up @@ -761,7 +761,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
countResult, err := mongoTemplate.CountDocuments(ctx, filter, test{})
if err != nil {
Expand Down Expand Up @@ -805,7 +805,7 @@ func main() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
pipeline := mongo.Pipeline{bson.D{{"$match", bson.D{
{"operationType", bson.M{"$in": []string{"insert", "update", "delete", "replace"}}},
}}}}
Expand Down
4 changes: 2 additions & 2 deletions _example/count/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func estimatedDocumentCount() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
countResult, err := mongoTemplate.EstimatedDocumentCount(ctx, test{})
if err != nil {
logger.Error("error estimated document count:", err)
Expand All @@ -51,7 +51,7 @@ func countDocuments() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
countResult, err := mongoTemplate.CountDocuments(ctx, filter, test{})
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions _example/delete/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func deleteOne() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
deleteResult, err := mongoTemplate.DeleteOne(ctx, filter, test{})
if err != nil {
Expand All @@ -53,7 +53,7 @@ func deleteOneById() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
objectId, _ := primitive.ObjectIDFromHex("6585f3a8bf2af8ad9bcab912")
deleteResult, err := mongoTemplate.DeleteOneById(ctx, objectId, test{})
if err != nil {
Expand All @@ -71,7 +71,7 @@ func deleteMany() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
deleteResult, err := mongoTemplate.DeleteMany(ctx, filter, test{})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions _example/exists/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func exists() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
existsResult, err := mongoTemplate.Exists(ctx, filter, test{})
if err != nil {
Expand All @@ -52,7 +52,7 @@ func existsById() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
objectId, _ := primitive.ObjectIDFromHex("6585f5b8fd8fa97d562419f7")
existsResult, err := mongoTemplate.ExistsById(ctx, objectId, test{})
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions _example/find/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func findOne() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
var dest test
err = mongoTemplate.FindOne(ctx, filter, &dest)
Expand All @@ -56,7 +56,7 @@ func findOneById() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
objectId, _ := primitive.ObjectIDFromHex("6585db26633e225cbeadf553")
//dest need a pointer
var dest test
Expand All @@ -76,7 +76,7 @@ func find() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
//dest need a pointer
var dest []test
Expand All @@ -96,7 +96,7 @@ func findPageable() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
pageOutput, err := mongoTemplate.FindPageable(ctx, filter, mongo.PageInput{
Page: 0,
Expand All @@ -119,7 +119,7 @@ func findAll() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
var dest []test
err = mongoTemplate.FindAll(ctx, &dest)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions _example/insert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func insertOne() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
testDocument := test{
Random: rand.Int(),
Name: "Foo Bar",
Expand All @@ -62,7 +62,7 @@ func insertMany() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
testDocuments := []*test{
{
Random: rand.Int(),
Expand Down Expand Up @@ -98,7 +98,7 @@ func insertOneManualCloseSession() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
testDocument := test{
Random: rand.Int(),
Name: "Foo Bar",
Expand Down
4 changes: 2 additions & 2 deletions _example/replace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func replaceOne() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
replacement := test{
Random: rand.Int(),
Expand All @@ -61,7 +61,7 @@ func replaceOneById() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
objectId, _ := primitive.ObjectIDFromHex("6585db26633e225cbeadf553")
replacement := test{
Random: rand.Int(),
Expand Down
6 changes: 3 additions & 3 deletions _example/update/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func updateOne() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
update := bson.M{"$set": bson.M{"name": "Foo Bar Updated"}}
updateResult, err := mongoTemplate.UpdateOne(ctx, filter, update, test{})
Expand All @@ -54,7 +54,7 @@ func updateOneById() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
objectId, _ := primitive.ObjectIDFromHex("6585f3a8bf2af8ad9bcab911")
update := bson.M{"$set": bson.M{"name": "Foo Bar Updated"}}
updateResult, err := mongoTemplate.UpdateOneById(ctx, objectId, update, test{})
Expand All @@ -73,7 +73,7 @@ func updateMany() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
update := bson.M{"$set": bson.M{"name": "Foo Bar Updated"}}
updateResult, err := mongoTemplate.UpdateOne(ctx, filter, update, test{})
Expand Down
4 changes: 2 additions & 2 deletions _example/watch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func watch() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
pipeline := mongo.Pipeline{bson.D{{"$match", bson.D{
{"operationType", bson.M{"$in": []string{"insert", "update", "delete", "replace"}}},
}}}}
Expand Down Expand Up @@ -52,7 +52,7 @@ func watchHandler() {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.Disconnect(ctx)
defer mongoTemplate.SimpleDisconnect(ctx)
pipeline := mongo.Pipeline{bson.D{{"$match", bson.D{
{"operationType", bson.M{"$in": []string{"insert", "update", "delete", "replace"}}},
}}}}
Expand Down
1 change: 1 addition & 0 deletions mongo/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,7 @@ func initOptionInsertMany() option.InsertMany {

func initOptionDelete() option.Delete {
return option.NewDelete().
SetDisableAutoRollbackSession(true).
SetForceRecreateSession(true).
SetDisableAutoCloseSession(false).
SetComment("comment delete golang unit test").
Expand Down
Loading

0 comments on commit 6da4e8e

Please sign in to comment.