Skip to content

Commit

Permalink
- Added and refactored conditions to use go-helper.
Browse files Browse the repository at this point in the history
- Added to FullDocument when watching, the ParseToStruct function to make the document easier to read.
- Updated the go-logger lib.
- Refactored and added go-errors lib for better understanding when returning an error with file, line and message information.
  • Loading branch information
Gabriel Cataldo committed Jan 25, 2024
1 parent 503c265 commit 11dddbf
Show file tree
Hide file tree
Showing 30 changed files with 1,067 additions and 916 deletions.
87 changes: 52 additions & 35 deletions README.md

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions _example/count/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"github.com/GabrielHCataldo/go-helper/helper"
"github.com/GabrielHCataldo/go-logger/logger"
"github.com/GabrielHCataldo/go-mongo-template/mongo"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -30,13 +31,13 @@ func estimatedDocumentCount() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.SimpleDisconnect(ctx)
countResult, err := mongoTemplate.EstimatedDocumentCount(ctx, test{})
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error estimated document count:", err)
} else {
logger.Info("estimated document count successfully:", countResult)
Expand All @@ -47,14 +48,14 @@ func countDocuments() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
countResult, err := mongoTemplate.CountDocuments(ctx, filter, test{})
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error count documents:", err)
} else {
logger.Info("count documents successfully:", countResult)
Expand Down
13 changes: 7 additions & 6 deletions _example/delete/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"github.com/GabrielHCataldo/go-helper/helper"
"github.com/GabrielHCataldo/go-logger/logger"
"github.com/GabrielHCataldo/go-mongo-template/mongo"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -31,14 +32,14 @@ func deleteOne() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
deleteResult, err := mongoTemplate.DeleteOne(ctx, filter, test{})
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error delete document:", err)
} else {
logger.Info("document deleted successfully:", deleteResult)
Expand All @@ -49,14 +50,14 @@ func deleteOneById() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.SimpleDisconnect(ctx)
objectId, _ := primitive.ObjectIDFromHex("6585f3a8bf2af8ad9bcab912")
deleteResult, err := mongoTemplate.DeleteOneById(ctx, objectId, test{})
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error delete document:", err)
} else {
logger.Info("document deleted successfully:", deleteResult)
Expand All @@ -67,14 +68,14 @@ func deleteMany() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
deleteResult, err := mongoTemplate.DeleteMany(ctx, filter, test{})
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error delete documents:", err)
} else {
logger.Info("documents deleted successfully:", deleteResult)
Expand Down
9 changes: 5 additions & 4 deletions _example/exists/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"github.com/GabrielHCataldo/go-helper/helper"
"github.com/GabrielHCataldo/go-logger/logger"
"github.com/GabrielHCataldo/go-mongo-template/mongo"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -30,14 +31,14 @@ func exists() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
existsResult, err := mongoTemplate.Exists(ctx, filter, test{})
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error check exists document:", err)
} else {
logger.Info("document exists:", existsResult)
Expand All @@ -48,14 +49,14 @@ func existsById() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.SimpleDisconnect(ctx)
objectId, _ := primitive.ObjectIDFromHex("6585f5b8fd8fa97d562419f7")
existsResult, err := mongoTemplate.ExistsById(ctx, objectId, test{})
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error check exists document:", err)
} else {
logger.Info("document exists:", existsResult)
Expand Down
21 changes: 11 additions & 10 deletions _example/find/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"github.com/GabrielHCataldo/go-helper/helper"
"github.com/GabrielHCataldo/go-logger/logger"
"github.com/GabrielHCataldo/go-mongo-template/mongo"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -33,15 +34,15 @@ func findOne() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.SimpleDisconnect(ctx)
filter := bson.M{"_id": bson.M{"$exists": true}}
var dest test
err = mongoTemplate.FindOne(ctx, filter, &dest)
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error find document:", err)
} else {
logger.Info("find document successfully:", dest)
Expand All @@ -52,7 +53,7 @@ func findOneById() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
Expand All @@ -61,7 +62,7 @@ func findOneById() {
//dest need a pointer
var dest test
err = mongoTemplate.FindOneById(ctx, objectId, &dest)
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error find all documents:", err)
} else {
logger.Info("find by id document successfully:", dest)
Expand All @@ -72,7 +73,7 @@ func find() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
Expand All @@ -81,7 +82,7 @@ func find() {
//dest need a pointer
var dest []test
err = mongoTemplate.Find(ctx, filter, &dest)
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error find documents:", err)
} else {
logger.Info("find documents successfully:", dest)
Expand All @@ -92,7 +93,7 @@ func findPageable() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
Expand All @@ -104,7 +105,7 @@ func findPageable() {
Ref: []test{}, //need a slice of the structure
Sort: bson.M{"createdAt": mongo.SortDesc},
})
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error find pageable documents:", err)
} else {
logger.Info("find pageable documents successfully:", pageOutput)
Expand All @@ -115,14 +116,14 @@ func findAll() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
defer mongoTemplate.SimpleDisconnect(ctx)
var dest []test
err = mongoTemplate.FindAll(ctx, &dest)
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error find all documents:", err)
} else {
logger.Info("find all documents successfully:", dest)
Expand Down
17 changes: 9 additions & 8 deletions _example/insert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"github.com/GabrielHCataldo/go-helper/helper"
"github.com/GabrielHCataldo/go-logger/logger"
"github.com/GabrielHCataldo/go-mongo-template/mongo"
"github.com/GabrielHCataldo/go-mongo-template/mongo/option"
Expand Down Expand Up @@ -32,7 +33,7 @@ func insertOne() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
Expand All @@ -47,7 +48,7 @@ func insertOne() {
}
//new document need a pointer
err = mongoTemplate.InsertOne(ctx, &testDocument)
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error insert document:", err)
} else {
logger.Info("document inserted successfully:", testDocument)
Expand All @@ -58,7 +59,7 @@ func insertMany() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
Expand All @@ -83,7 +84,7 @@ func insertMany() {
}
//new document need a slice of the pointer
err = mongoTemplate.InsertMany(ctx, testDocuments)
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error insert document:", err)
} else {
logger.Info("document inserted successfully:", testDocuments)
Expand All @@ -94,7 +95,7 @@ func insertOneManualCloseSession() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
Expand All @@ -110,15 +111,15 @@ func insertOneManualCloseSession() {
//new document need a pointer
err = mongoTemplate.InsertOne(ctx, &testDocument, option.NewInsertOne().
SetForceRecreateSession(false).SetDisableAutoCloseSession(true))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error insert document:", err)
} else {
logger.Info("document inserted successfully:", testDocument)
}
//mongoTemplate.AbortTransaction(ctx) or mongoTemplate.CommitTransaction(ctc)
abort := err != nil
abort := helper.IsNotNil(err)
err = mongoTemplate.CloseSession(ctx, abort)
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error close session:", err)
}
}
9 changes: 5 additions & 4 deletions _example/replace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"github.com/GabrielHCataldo/go-helper/helper"
"github.com/GabrielHCataldo/go-logger/logger"
"github.com/GabrielHCataldo/go-mongo-template/mongo"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -31,7 +32,7 @@ func replaceOne() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
Expand All @@ -46,7 +47,7 @@ func replaceOne() {
CreatedAt: primitive.NewDateTimeFromTime(time.Now()),
}
updateResult, err := mongoTemplate.ReplaceOne(ctx, filter, replacement, test{})
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error replace document:", err)
} else {
logger.Info("document replaced successfully:", updateResult)
Expand All @@ -57,7 +58,7 @@ func replaceOneById() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
mongoTemplate, err := mongo.NewTemplate(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URL")))
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error to init mongo template:", err)
return
}
Expand All @@ -72,7 +73,7 @@ func replaceOneById() {
CreatedAt: primitive.NewDateTimeFromTime(time.Now()),
}
updateResult, err := mongoTemplate.ReplaceOneById(ctx, objectId, replacement, test{})
if err != nil {
if helper.IsNotNil(err) {
logger.Error("error replace document:", err)
} else {
logger.Info("document replaced successfully:", updateResult)
Expand Down
Loading

0 comments on commit 11dddbf

Please sign in to comment.