Skip to content

Commit

Permalink
feat: add MongoDB functions to handle transactions (#96)
Browse files Browse the repository at this point in the history
* add MongoDB functions to handle transactions

Signed-off-by: Patricia Reinoso <[email protected]>

* rename to supports transactions

Signed-off-by: Patricia Reinoso <[email protected]>

---------

Signed-off-by: Patricia Reinoso <[email protected]>
Signed-off-by: Ajay Lotan Thakur <[email protected]>
  • Loading branch information
patriciareinoso authored and thakurajayL committed Jan 16, 2025
1 parent ac6dbbe commit 3ba9b8c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
22 changes: 22 additions & 0 deletions mongoapi/dbadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
package mongoapi

import (
"context"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)

type DBInterface interface {
Expand All @@ -19,14 +21,18 @@ type DBInterface interface {
RestfulAPIPutOneNotUpdate(collName string, filter bson.M, putData map[string]interface{}) (bool, error)
RestfulAPIPutMany(collName string, filterArray []primitive.M, putDataArray []map[string]interface{}) error
RestfulAPIDeleteOne(collName string, filter bson.M) error
RestfulAPIDeleteOneWithContext(collName string, filter bson.M, context context.Context) error
RestfulAPIDeleteMany(collName string, filter bson.M) error
RestfulAPIMergePatch(collName string, filter bson.M, patchData map[string]interface{}) error
RestfulAPIJSONPatch(collName string, filter bson.M, patchJSON []byte) error
RestfulAPIJSONPatchWithContext(collName string, filter bson.M, patchJSON []byte, context context.Context) error
RestfulAPIJSONPatchExtend(collName string, filter bson.M, patchJSON []byte, dataName string) error
RestfulAPIPost(collName string, filter bson.M, postData map[string]interface{}) (bool, error)
RestfulAPIPostMany(collName string, filter bson.M, postDataArray []interface{}) error
GetUniqueIdentity(idName string) int32
CreateIndex(collName string, keyField string) (bool, error)
StartSession() (mongo.Session, error)
SupportsTransactions() (bool, error)
}

var CommonDBClient DBInterface
Expand Down Expand Up @@ -93,6 +99,10 @@ func (db *MongoDBClient) RestfulAPIDeleteOne(collName string, filter bson.M) err
return db.MongoClient.RestfulAPIDeleteOne(collName, filter)
}

func (db *MongoDBClient) RestfulAPIDeleteOneWithContext(collName string, filter bson.M, context context.Context) error {
return db.MongoClient.RestfulAPIDeleteOneWithContext(collName, filter, context)
}

func (db *MongoDBClient) RestfulAPIDeleteMany(collName string, filter bson.M) error {
return db.MongoClient.RestfulAPIDeleteMany(collName, filter)
}
Expand All @@ -105,6 +115,10 @@ func (db *MongoDBClient) RestfulAPIJSONPatch(collName string, filter bson.M, pat
return db.MongoClient.RestfulAPIJSONPatch(collName, filter, patchJSON)
}

func (db *MongoDBClient) RestfulAPIJSONPatchWithContext(collName string, filter bson.M, patchJSON []byte, context context.Context) error {
return db.MongoClient.RestfulAPIJSONPatchWithContext(collName, filter, patchJSON, context)
}

func (db *MongoDBClient) RestfulAPIJSONPatchExtend(collName string, filter bson.M, patchJSON []byte, dataName string) error {
return db.MongoClient.RestfulAPIJSONPatchExtend(collName, filter, patchJSON, dataName)
}
Expand All @@ -124,3 +138,11 @@ func (db *MongoDBClient) GetUniqueIdentity(idName string) int32 {
func (db *MongoDBClient) CreateIndex(collName string, keyField string) (bool, error) {
return db.MongoClient.CreateIndex(collName, keyField)
}

func (db *MongoDBClient) StartSession() (mongo.Session, error) {
return db.MongoClient.StartSession()
}

func (db *MongoDBClient) SupportsTransactions() (bool, error) {
return db.MongoClient.SupportsTransactions()
}
32 changes: 30 additions & 2 deletions mongoapi/mongoapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,13 @@ func (c *MongoClient) RestfulAPIPutMany(collName string, filterArray []bson.M, p
}

func (c *MongoClient) RestfulAPIDeleteOne(collName string, filter bson.M) error {
return c.RestfulAPIDeleteOneWithContext(collName, filter, context.TODO())
}

func (c *MongoClient) RestfulAPIDeleteOneWithContext(collName string, filter bson.M, context context.Context) error {
collection := c.Client.Database(c.dbName).Collection(collName)

if _, err := collection.DeleteOne(context.TODO(), filter); err != nil {
if _, err := collection.DeleteOne(context, filter); err != nil {
return fmt.Errorf("RestfulAPIDeleteOne err: %+v", err)
}
return nil
Expand Down Expand Up @@ -245,6 +249,10 @@ func (c *MongoClient) RestfulAPIMergePatch(collName string, filter bson.M, patch
}

func (c *MongoClient) RestfulAPIJSONPatch(collName string, filter bson.M, patchJSON []byte) error {
return c.RestfulAPIJSONPatchWithContext(collName, filter, patchJSON, context.TODO())
}

func (c *MongoClient) RestfulAPIJSONPatchWithContext(collName string, filter bson.M, patchJSON []byte, context context.Context) error {
collection := c.Client.Database(c.dbName).Collection(collName)

originalData, err := getOrigData(collection, filter)
Expand All @@ -271,7 +279,7 @@ func (c *MongoClient) RestfulAPIJSONPatch(collName string, filter bson.M, patchJ
if err := json.Unmarshal(modified, &modifiedData); err != nil {
return fmt.Errorf("RestfulAPIJSONPatch Unmarshal err: %+v", err)
}
if _, err := collection.UpdateOne(context.TODO(), filter, bson.M{"$set": modifiedData}); err != nil {
if _, err := collection.UpdateOne(context, filter, bson.M{"$set": modifiedData}); err != nil {
return fmt.Errorf("RestfulAPIJSONPatch UpdateOne err: %+v", err)
}
return nil
Expand Down Expand Up @@ -826,3 +834,23 @@ func (c *MongoClient) RestfulAPIPutOnly(collName string, filter bson.M, putData
err = fmt.Errorf("failed to update document: %s", err)
return err
}

func (c *MongoClient) StartSession() (mongo.Session, error) {
return c.Client.StartSession()
}

func (c *MongoClient) SupportsTransactions() (bool, error) {
command := bson.D{{"hello", 1}}
result := c.Client.Database(c.dbName).RunCommand(context.Background(), command)
var status bson.M
if err := result.Decode(&status); err != nil {
return false, fmt.Errorf("failed to get server status: %v", err)
}
if msg, ok := status["msg"]; ok && msg == "isdbgrid" {
return true, nil // Sharded clusters support transactions
}
if _, ok := status["setName"]; ok {
return true, nil
}
return false, nil
}

0 comments on commit 3ba9b8c

Please sign in to comment.