90 lines
3.9 KiB
Go
90 lines
3.9 KiB
Go
package mongomanager
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"gitlab.com/arkadooti.sarkar/go-boilerplate/core/appcontext"
|
|
"gitlab.com/arkadooti.sarkar/go-boilerplate/core/log"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"time"
|
|
)
|
|
|
|
type mongoService struct {
|
|
DB *mongo.Client
|
|
}
|
|
|
|
type MongoDB interface {
|
|
Disconnect(ctx appcontext.AppContext) error
|
|
CreateOne(ctx appcontext.AppContext, database, collectionName string, d interface{}) (*mongo.InsertOneResult, error)
|
|
CreateMany(ctx appcontext.AppContext, database, collectionName string, d []interface{}) (*mongo.InsertManyResult, error)
|
|
ReadOne(ctx appcontext.AppContext, database, collectionName string, filter, data interface{}) error
|
|
ReadAll(ctx appcontext.AppContext, database, collectionName string, filter, data interface{}, opts ...*options.FindOptions) error
|
|
Update(ctx appcontext.AppContext, database, collectionName string, filter, update interface{}, options ...*options.UpdateOptions) (*mongo.UpdateResult, error)
|
|
ReplaceOne(ctx appcontext.AppContext, database, collectionName string, filter, replacement interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)
|
|
Upsert(ctx appcontext.AppContext, database, collectionName string, filter, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
|
|
UpdateAndReturn(ctx appcontext.AppContext, database, collectionName string, filter, update, data interface{}) error
|
|
UpdateAll(ctx appcontext.AppContext, database, collectionName string, filter, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
|
|
Delete(ctx appcontext.AppContext, database, collectionName string, filter interface{}) (*mongo.DeleteResult, error)
|
|
DeleteAll(ctx appcontext.AppContext, database, collectionName string, filter interface{}) (*mongo.DeleteResult, error)
|
|
CountDocuments(ctx appcontext.AppContext, database, collectionName string, filter interface{}, opts ...*options.CountOptions) (int64, error)
|
|
Exist(ctx appcontext.AppContext, database, collectionName string, filter interface{}) (bool, error)
|
|
GetDistinct(ctx appcontext.AppContext, database, collectionName, fieldName string, filter interface{}) (interface{}, error)
|
|
AggregateAll(ctx appcontext.AppContext, database, collectionName string, query, data interface{}, options ...*options.AggregateOptions) error
|
|
FindOneAndUpdate(ctx appcontext.AppContext, database, collectionName string, filter, update, data interface{}, opts ...*options.FindOneAndUpdateOptions) error
|
|
BulkWrite(ctx appcontext.AppContext, database, collectionName string, operations []mongo.WriteModel, bulkOption *options.BulkWriteOptions) (*mongo.BulkWriteResult, error)
|
|
}
|
|
|
|
func NewMongoClient(url, appName string) (MongoDB, error) {
|
|
ctx := appcontext.NewAppContext()
|
|
client, err := connect(url, appName)
|
|
if err != nil {
|
|
log.GenericError(ctx, errors.New("can't connect to db: "+err.Error()), nil)
|
|
return &mongoService{DB: client}, err
|
|
}
|
|
return &mongoService{DB: client}, nil
|
|
}
|
|
|
|
func connect(host, appName string) (*mongo.Client, error) {
|
|
var client *mongo.Client
|
|
|
|
servSelecTimeout := time.Duration(15) * time.Second
|
|
connTimeout := time.Duration(10) * time.Second
|
|
idleTime := time.Duration(2) * time.Minute
|
|
socketTimeout := time.Duration(2) * time.Minute
|
|
maxPooling := uint64(100)
|
|
|
|
clientOptions := &options.ClientOptions{
|
|
AppName: &appName,
|
|
ServerSelectionTimeout: &servSelecTimeout,
|
|
ConnectTimeout: &connTimeout,
|
|
MaxConnIdleTime: &idleTime,
|
|
MaxPoolSize: &maxPooling,
|
|
SocketTimeout: &socketTimeout,
|
|
}
|
|
|
|
clientOptions = clientOptions.ApplyURI(host)
|
|
|
|
err := clientOptions.Validate()
|
|
if err != nil {
|
|
return client, err
|
|
}
|
|
|
|
client, err = mongo.NewClient(clientOptions)
|
|
if err != nil {
|
|
return client, err
|
|
}
|
|
|
|
err = client.Connect(context.TODO())
|
|
if err != nil {
|
|
return client, err
|
|
}
|
|
|
|
err = client.Ping(context.TODO(), nil)
|
|
if err != nil {
|
|
return client, err
|
|
}
|
|
|
|
return client, nil
|
|
}
|