diff --git a/README.md b/README.md index 29d6a9c..f6a07a2 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ MongoDB Template -[![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) @@ -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", @@ -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(), @@ -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{}) @@ -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{}) @@ -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{}) @@ -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(), @@ -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 { @@ -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 { @@ -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 { @@ -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) @@ -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, @@ -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 @@ -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 { @@ -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 { @@ -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) @@ -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 { @@ -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"}}}, }}}} diff --git a/_example/count/main.go b/_example/count/main.go index c38fef2..ad5f775 100644 --- a/_example/count/main.go +++ b/_example/count/main.go @@ -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) @@ -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 { diff --git a/_example/delete/main.go b/_example/delete/main.go index 5a1a97f..baca7f8 100644 --- a/_example/delete/main.go +++ b/_example/delete/main.go @@ -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 { @@ -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 { @@ -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 { diff --git a/_example/exists/main.go b/_example/exists/main.go index 7fef780..ae52154 100644 --- a/_example/exists/main.go +++ b/_example/exists/main.go @@ -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 { @@ -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 { diff --git a/_example/find/main.go b/_example/find/main.go index 4a0f808..be169b5 100644 --- a/_example/find/main.go +++ b/_example/find/main.go @@ -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) @@ -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 @@ -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 @@ -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, @@ -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 { diff --git a/_example/insert/main.go b/_example/insert/main.go index 6abd547..a8b5c1a 100644 --- a/_example/insert/main.go +++ b/_example/insert/main.go @@ -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", @@ -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(), @@ -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", diff --git a/_example/replace/main.go b/_example/replace/main.go index d3f1842..2aa9dee 100644 --- a/_example/replace/main.go +++ b/_example/replace/main.go @@ -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(), @@ -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(), diff --git a/_example/update/main.go b/_example/update/main.go index 6f11ed5..9c4b670 100644 --- a/_example/update/main.go +++ b/_example/update/main.go @@ -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{}) @@ -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{}) @@ -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{}) diff --git a/_example/watch/main.go b/_example/watch/main.go index 3f2dcf5..ef92782 100644 --- a/_example/watch/main.go +++ b/_example/watch/main.go @@ -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"}}}, }}}} @@ -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"}}}, }}}} diff --git a/mongo/main_test.go b/mongo/main_test.go index f2a6e71..6d43596 100644 --- a/mongo/main_test.go +++ b/mongo/main_test.go @@ -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"). diff --git a/mongo/template.go b/mongo/template.go index 105b63f..b2d7a37 100644 --- a/mongo/template.go +++ b/mongo/template.go @@ -3,6 +3,7 @@ package mongo import ( "context" "errors" + "github.com/GabrielHCataldo/go-logger/logger" "github.com/GabrielHCataldo/go-mongo-template/internal/util" "github.com/GabrielHCataldo/go-mongo-template/mongo/option" "go.mongodb.org/mongo-driver/bson" @@ -1026,6 +1027,16 @@ func (t *Template) Disconnect(ctx context.Context) error { return t.client.Disconnect(ctx) } +// SimpleDisconnect closes the mongodb connection client without return error +func (t *Template) SimpleDisconnect(ctx context.Context) { + err := t.Disconnect(ctx) + if err != nil { + logger.Error("error close connection to mongoDB:", err) + return + } + logger.Info("connection to mongoDB closed.") +} + func (t *Template) insertOne(sc mongo.SessionContext, document any, opt option.InsertOne) error { if util.IsNotPointer(document) { return ErrDocumentIsNotPointer @@ -1442,11 +1453,10 @@ func (t *Template) closeSession( } func (t *Template) endSession(ctx context.Context) { - if t.session == nil { - return + if t.session != nil { + t.session.EndSession(ctx) + t.session = nil } - t.session.EndSession(ctx) - t.session = nil } func getMongoInfosByAny(a any) (databaseName string, collectionName string, err error) { diff --git a/mongo/template_test.go b/mongo/template_test.go index d9df5ed..1b24dd5 100644 --- a/mongo/template_test.go +++ b/mongo/template_test.go @@ -40,9 +40,11 @@ func TestTemplateInsertOne(t *testing.T) { t.Log("err expected:", err) } if tt.forceErrCloseMongoClient { - _ = mongoTemplate.Disconnect(ctx) + mongoTemplate.SimpleDisconnect(ctx) + mongoTemplate.SimpleDisconnect(ctx) } _ = mongoTemplate.CloseSession(ctx, err != nil) + _ = mongoTemplate.CloseSession(ctx, err != nil) disconnectMongoTemplate() }) }