2024-07-17 18:02:30 +02:00
|
|
|
package mongo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2024-07-29 15:07:59 +02:00
|
|
|
"fmt"
|
2024-07-26 13:45:10 +02:00
|
|
|
"strings"
|
2024-07-17 18:02:30 +02:00
|
|
|
"time"
|
|
|
|
|
2024-07-18 13:35:14 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
|
|
"cloud.o-forge.io/core/oc-lib/logs"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
2024-07-17 18:02:30 +02:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-07-18 11:51:12 +02:00
|
|
|
mngoClient *mongo.Client
|
|
|
|
mngoDB *mongo.Database
|
|
|
|
MngoCtx context.Context
|
|
|
|
cancel context.CancelFunc
|
2024-07-17 18:02:30 +02:00
|
|
|
|
2024-07-18 11:51:12 +02:00
|
|
|
existingCollections []string
|
2024-07-17 18:02:30 +02:00
|
|
|
|
2024-07-18 11:51:12 +02:00
|
|
|
ResourceMap map[string]interface{}
|
2024-07-17 18:02:30 +02:00
|
|
|
)
|
|
|
|
|
2024-07-18 13:35:14 +02:00
|
|
|
var MONGOService = MongoDB{}
|
|
|
|
|
2024-07-18 14:11:13 +02:00
|
|
|
type MongoConf interface {
|
|
|
|
GetUrl() string
|
|
|
|
GetDatabase() string
|
|
|
|
}
|
|
|
|
|
2024-07-18 13:35:14 +02:00
|
|
|
type MongoDB struct {
|
|
|
|
Logger zerolog.Logger
|
|
|
|
}
|
|
|
|
|
2024-07-18 14:11:13 +02:00
|
|
|
func (m *MongoDB) Init(collections []string, config MongoConf) {
|
2024-07-17 18:02:30 +02:00
|
|
|
// var baseConfig string
|
2024-07-18 17:55:27 +02:00
|
|
|
m.Logger = logs.GetLogger()
|
2024-07-17 18:02:30 +02:00
|
|
|
ResourceMap = make(map[string]interface{})
|
2024-07-18 11:51:12 +02:00
|
|
|
|
2024-07-18 17:55:27 +02:00
|
|
|
m.Logger.Info().Msg("Connecting to" + config.GetUrl())
|
2024-07-17 18:02:30 +02:00
|
|
|
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-07-18 14:11:13 +02:00
|
|
|
m.createClient(config.GetUrl())
|
2024-07-17 18:02:30 +02:00
|
|
|
|
2024-07-18 17:55:27 +02:00
|
|
|
m.Logger.Info().Msg("Connecting mongo client to db " + config.GetDatabase())
|
2024-07-18 14:11:13 +02:00
|
|
|
m.prepareDB(collections, config)
|
2024-07-17 18:02:30 +02:00
|
|
|
|
2024-07-18 17:55:27 +02:00
|
|
|
m.Logger.Info().Msg("Database is READY")
|
2024-07-17 18:02:30 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-07-18 13:35:14 +02:00
|
|
|
func (m *MongoDB) createClient(MongoURL string) {
|
2024-07-17 18:02:30 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Allows us to use marshal and unmarshall with results of FindOne() and others
|
2024-07-18 11:51:12 +02:00
|
|
|
bsonOpts := &options.BSONOptions{
|
2024-07-17 18:02:30 +02:00
|
|
|
UseJSONStructTags: true,
|
2024-07-18 11:51:12 +02:00
|
|
|
NilSliceAsEmpty: true,
|
2024-07-17 18:02:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
clientOptions := options.Client().ApplyURI(MongoURL).SetBSONOptions(bsonOpts)
|
2024-07-18 11:51:12 +02:00
|
|
|
mngoClient, err = mongo.Connect(MngoCtx, clientOptions)
|
2024-07-17 18:02:30 +02:00
|
|
|
if err != nil {
|
2024-07-18 13:35:14 +02:00
|
|
|
m.Logger.Fatal().Msg("Mongodb NewClient " + MongoURL + ":" + "err")
|
2024-07-17 18:02:30 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping the primary
|
|
|
|
if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil {
|
2024-07-18 13:35:14 +02:00
|
|
|
m.Logger.Fatal().Msg("Mongodb connect " + MongoURL + ":" + "err")
|
2024-07-17 18:02:30 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = mngoClient.Ping(MngoCtx, nil); err != nil {
|
2024-07-18 13:35:14 +02:00
|
|
|
m.Logger.Fatal().Msg("Mongodb ping " + MongoURL + ":" + "err")
|
2024-07-17 18:02:30 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-07-18 14:11:13 +02:00
|
|
|
func (m *MongoDB) prepareDB(list_collection []string, config MongoConf) {
|
2024-07-17 18:02:30 +02:00
|
|
|
var err error
|
2024-07-18 14:11:13 +02:00
|
|
|
mngoDB = mngoClient.Database(config.GetDatabase())
|
2024-07-18 11:51:12 +02:00
|
|
|
existingCollections, err = mngoDB.ListCollectionNames(MngoCtx, bson.D{})
|
2024-07-17 18:02:30 +02:00
|
|
|
if err != nil {
|
2024-07-18 13:35:14 +02:00
|
|
|
m.Logger.Fatal().Msg("Error contacting MongoDB\n" + err.Error())
|
2024-07-17 18:02:30 +02:00
|
|
|
}
|
|
|
|
collectionMap := make(map[string]bool)
|
2024-07-18 11:51:12 +02:00
|
|
|
for _, name := range existingCollections {
|
|
|
|
collectionMap[name] = true
|
|
|
|
}
|
2024-07-17 18:02:30 +02:00
|
|
|
// Only do the collection definition process if it doesn't already exists
|
|
|
|
// we add the collection to the collection map from mongo/mongo_utils to provide faster access to the collection
|
2024-07-18 11:51:12 +02:00
|
|
|
for _, collection_name := range list_collection {
|
2024-07-17 18:02:30 +02:00
|
|
|
new_collection := mngoDB.Collection(collection_name)
|
|
|
|
if _, exists := collectionMap[collection_name]; !exists {
|
2024-07-18 13:35:14 +02:00
|
|
|
m.createCollection(collection_name, new_collection)
|
2024-07-18 11:51:12 +02:00
|
|
|
} else {
|
2024-07-17 18:02:30 +02:00
|
|
|
CollectionMap[collection_name] = new_collection
|
|
|
|
}
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
2024-07-17 18:02:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates the collection with index specified in mongo/mongo_collections
|
|
|
|
// or use the basic collection creation function
|
2024-07-18 13:35:14 +02:00
|
|
|
func (m *MongoDB) createCollection(collection_name string, new_collection *mongo.Collection) {
|
2024-07-18 11:51:12 +02:00
|
|
|
var err error
|
2024-07-17 18:02:30 +02:00
|
|
|
CollectionMap[collection_name] = new_collection
|
2024-07-18 11:51:12 +02:00
|
|
|
_, exists := IndexesMap[collection_name]
|
|
|
|
if exists {
|
2024-07-17 18:02:30 +02:00
|
|
|
if _, err = new_collection.Indexes().CreateMany(MngoCtx, IndexesMap[collection_name]); err != nil {
|
|
|
|
var cmdErr mongo.CommandError
|
|
|
|
if errors.As(err, &cmdErr) && cmdErr.Code != 85 {
|
2024-07-18 13:35:14 +02:00
|
|
|
m.Logger.Fatal().Msg("Error creating indexes for " + collection_name + " collection : \n" + err.Error())
|
2024-07-17 18:02:30 +02:00
|
|
|
panic(err)
|
|
|
|
} else if !errors.As(err, &cmdErr) {
|
2024-07-18 13:35:14 +02:00
|
|
|
m.Logger.Fatal().Msg("Unexpected error: " + err.Error())
|
2024-07-17 18:02:30 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mngoDB.CreateCollection(MngoCtx, collection_name)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-07-19 11:27:58 +02:00
|
|
|
func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, int, error) {
|
2024-07-22 14:46:49 +02:00
|
|
|
filter := bson.M{"_id": id}
|
2024-07-18 11:51:12 +02:00
|
|
|
targetDBCollection := CollectionMap[collection_name]
|
|
|
|
opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}})
|
|
|
|
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
result, err := targetDBCollection.DeleteOne(MngoCtx, filter, opts)
|
|
|
|
if err != nil {
|
2024-07-18 13:35:14 +02:00
|
|
|
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
|
2024-07-19 11:27:58 +02:00
|
|
|
return 0, 404, err
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
2024-07-19 11:27:58 +02:00
|
|
|
return result.DeletedCount, 200, nil
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
|
|
|
|
2024-07-23 16:14:46 +02:00
|
|
|
func (m *MongoDB) DeleteMultiple(f map[string]interface{}, collection_name string) (int64, int, error) {
|
|
|
|
filter := bson.D{}
|
|
|
|
for k, v := range f {
|
|
|
|
filter = append(filter, bson.E{Key: k, Value: v})
|
|
|
|
}
|
|
|
|
targetDBCollection := CollectionMap[collection_name]
|
|
|
|
opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}})
|
|
|
|
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
result, err := targetDBCollection.DeleteMany(MngoCtx, filter, opts)
|
|
|
|
if err != nil {
|
|
|
|
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
|
|
|
|
return 0, 404, err
|
|
|
|
}
|
|
|
|
return result.DeletedCount, 200, nil
|
|
|
|
}
|
|
|
|
|
2024-07-25 09:28:55 +02:00
|
|
|
func (m *MongoDB) UpdateMultiple(set interface{}, filter map[string]interface{}, collection_name string) (int64, int, error) {
|
|
|
|
var doc map[string]interface{}
|
|
|
|
b, _ := bson.Marshal(set)
|
|
|
|
bson.Unmarshal(b, &doc)
|
|
|
|
f := bson.D{}
|
|
|
|
for k, v := range filter {
|
|
|
|
f = append(f, bson.E{Key: k, Value: v})
|
|
|
|
|
|
|
|
}
|
|
|
|
targetDBCollection := CollectionMap[collection_name]
|
|
|
|
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
res, err := targetDBCollection.UpdateMany(MngoCtx, f, dbs.InputToBson(doc, true))
|
|
|
|
if err != nil {
|
|
|
|
m.Logger.Error().Msg("Couldn't update resource: " + err.Error())
|
|
|
|
return 0, 404, err
|
|
|
|
}
|
|
|
|
return res.UpsertedCount, 200, nil
|
|
|
|
}
|
|
|
|
|
2024-07-22 16:12:38 +02:00
|
|
|
func (m *MongoDB) UpdateOne(set interface{}, id string, collection_name string) (string, int, error) {
|
|
|
|
var doc map[string]interface{}
|
|
|
|
b, _ := bson.Marshal(set)
|
|
|
|
bson.Unmarshal(b, &doc)
|
2024-07-22 14:46:49 +02:00
|
|
|
filter := bson.M{"_id": id}
|
2024-07-18 11:51:12 +02:00
|
|
|
targetDBCollection := CollectionMap[collection_name]
|
|
|
|
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
2024-07-22 16:12:38 +02:00
|
|
|
_, err := targetDBCollection.UpdateOne(MngoCtx, filter, dbs.InputToBson(doc, true))
|
2024-07-18 11:51:12 +02:00
|
|
|
if err != nil {
|
2024-07-19 11:27:58 +02:00
|
|
|
m.Logger.Error().Msg("Couldn't update resource: " + err.Error())
|
|
|
|
return "", 404, err
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
2024-07-22 14:46:49 +02:00
|
|
|
return id, 200, nil
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
|
|
|
|
2024-07-22 14:02:48 +02:00
|
|
|
func (m *MongoDB) StoreOne(obj interface{}, id string, collection_name string) (string, int, error) {
|
|
|
|
var doc map[string]interface{}
|
|
|
|
b, _ := bson.Marshal(obj)
|
|
|
|
bson.Unmarshal(b, &doc)
|
|
|
|
doc["_id"] = id
|
2024-07-17 18:02:30 +02:00
|
|
|
targetDBCollection := CollectionMap[collection_name]
|
2024-07-30 08:33:26 +02:00
|
|
|
fmt.Println("DB", targetDBCollection)
|
2024-07-17 18:02:30 +02:00
|
|
|
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-07-22 14:06:53 +02:00
|
|
|
_, err := targetDBCollection.InsertOne(MngoCtx, doc)
|
2024-07-17 18:02:30 +02:00
|
|
|
if err != nil {
|
2024-07-18 13:35:14 +02:00
|
|
|
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
|
2024-07-19 11:27:58 +02:00
|
|
|
return "", 409, err
|
2024-07-17 18:02:30 +02:00
|
|
|
}
|
|
|
|
|
2024-07-22 14:06:53 +02:00
|
|
|
return id, 200, nil
|
2024-07-17 18:02:30 +02:00
|
|
|
}
|
|
|
|
|
2024-07-19 11:27:58 +02:00
|
|
|
func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResult, int, error) {
|
2024-07-22 14:46:49 +02:00
|
|
|
filter := bson.M{"_id": id}
|
2024-07-17 18:02:30 +02:00
|
|
|
targetDBCollection := CollectionMap[collection_name]
|
|
|
|
|
|
|
|
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-07-19 11:27:58 +02:00
|
|
|
res := targetDBCollection.FindOne(MngoCtx, filter)
|
2024-07-17 18:02:30 +02:00
|
|
|
if res.Err() != nil {
|
2024-07-18 13:35:14 +02:00
|
|
|
m.Logger.Error().Msg("Couldn't find resource " + id + ". Error : " + res.Err().Error())
|
2024-07-19 11:27:58 +02:00
|
|
|
err := res.Err()
|
|
|
|
return nil, 404, err
|
2024-07-17 18:02:30 +02:00
|
|
|
}
|
2024-07-19 11:27:58 +02:00
|
|
|
return res, 200, nil
|
2024-07-17 18:02:30 +02:00
|
|
|
}
|
2024-07-23 11:22:50 +02:00
|
|
|
|
2024-07-26 13:45:10 +02:00
|
|
|
func (m *MongoDB) Search(search string, filter []string, collection_name string) (*mongo.Cursor, int, error) {
|
|
|
|
opts := options.Find()
|
|
|
|
opts.SetLimit(100)
|
|
|
|
if strings.TrimSpace(search) == "*" {
|
|
|
|
search = ""
|
|
|
|
}
|
2024-07-29 16:23:52 +02:00
|
|
|
search = ".*" + strings.TrimSpace(search) + ".*"
|
2024-07-26 13:45:10 +02:00
|
|
|
targetDBCollection := CollectionMap[collection_name]
|
|
|
|
list := []bson.M{}
|
|
|
|
for _, k := range filter {
|
2024-07-29 17:05:39 +02:00
|
|
|
list = append(list, bson.M{k: bson.M{"$regex": search, "$options": "i"}})
|
2024-07-26 13:45:10 +02:00
|
|
|
}
|
2024-07-29 15:22:14 +02:00
|
|
|
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-07-26 13:45:10 +02:00
|
|
|
if cursor, err := targetDBCollection.Find(
|
2024-07-29 15:53:15 +02:00
|
|
|
MngoCtx,
|
|
|
|
bson.M{"$or": []bson.M{
|
2024-07-29 17:05:39 +02:00
|
|
|
{"name": bson.M{"$regex": search, "$options": "i"}},
|
|
|
|
{"description": bson.M{"$regex": search, "$options": "i"}},
|
2024-07-29 15:53:15 +02:00
|
|
|
}},
|
2024-07-26 13:45:10 +02:00
|
|
|
opts,
|
|
|
|
); err != nil {
|
2024-07-29 15:07:59 +02:00
|
|
|
fmt.Println(0, bson.M{"$or": list})
|
2024-07-26 13:45:10 +02:00
|
|
|
return nil, 404, err
|
|
|
|
} else {
|
2024-07-29 15:07:59 +02:00
|
|
|
fmt.Println(cursor, bson.M{"$or": list})
|
|
|
|
|
2024-07-26 13:45:10 +02:00
|
|
|
return cursor, 200, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 09:28:55 +02:00
|
|
|
func (m *MongoDB) LoadFilter(filter map[string]interface{}, collection_name string) (*mongo.Cursor, int, error) {
|
|
|
|
f := bson.D{}
|
|
|
|
for k, v := range filter {
|
|
|
|
f = append(f, bson.E{Key: k, Value: v})
|
|
|
|
}
|
|
|
|
targetDBCollection := CollectionMap[collection_name]
|
|
|
|
|
|
|
|
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
res, err := targetDBCollection.Find(MngoCtx, f)
|
|
|
|
if err != nil {
|
|
|
|
m.Logger.Error().Msg("Couldn't find any resources. Error : " + err.Error())
|
|
|
|
return nil, 404, err
|
|
|
|
}
|
|
|
|
return res, 200, nil
|
|
|
|
}
|
|
|
|
|
2024-07-23 11:22:50 +02:00
|
|
|
func (m *MongoDB) LoadAll(collection_name string) (*mongo.Cursor, int, error) {
|
|
|
|
targetDBCollection := CollectionMap[collection_name]
|
|
|
|
|
|
|
|
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
res, err := targetDBCollection.Find(MngoCtx, bson.D{})
|
|
|
|
if err != nil {
|
|
|
|
m.Logger.Error().Msg("Couldn't find any resources. Error : " + err.Error())
|
|
|
|
return nil, 404, err
|
|
|
|
}
|
|
|
|
return res, 200, nil
|
|
|
|
}
|
2024-07-29 15:07:59 +02:00
|
|
|
|
|
|
|
func (m *MongoDB) toOperator(operator string) string {
|
|
|
|
if operator == "like" {
|
|
|
|
return "$regex"
|
|
|
|
} else if operator == "exists" {
|
|
|
|
return "$exists"
|
|
|
|
} else if operator == "in" {
|
|
|
|
return "$in"
|
|
|
|
} else if operator == "gte" {
|
|
|
|
return "$gte"
|
|
|
|
} else if operator == "gt" {
|
|
|
|
return "$gt"
|
|
|
|
} else if operator == "lte" {
|
|
|
|
return "$lte"
|
|
|
|
} else if operator == "lt" {
|
|
|
|
return "$lt"
|
|
|
|
} else if operator == "eq" {
|
|
|
|
return "$match"
|
|
|
|
}
|
|
|
|
return operator
|
|
|
|
}
|