Skip to content

Commit fc21892

Browse files
authored
Merge branch 'main' into thakurajayL-patch
2 parents 61b7fe7 + 09af132 commit fc21892

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

mongoapi/dbadapter.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
package mongoapi
66

77
import (
8+
"context"
89
"time"
910

1011
"go.mongodb.org/mongo-driver/bson"
1112
"go.mongodb.org/mongo-driver/bson/primitive"
13+
"go.mongodb.org/mongo-driver/mongo"
1214
)
1315

1416
type DBInterface interface {
@@ -19,14 +21,18 @@ type DBInterface interface {
1921
RestfulAPIPutOneNotUpdate(collName string, filter bson.M, putData map[string]interface{}) (bool, error)
2022
RestfulAPIPutMany(collName string, filterArray []primitive.M, putDataArray []map[string]interface{}) error
2123
RestfulAPIDeleteOne(collName string, filter bson.M) error
24+
RestfulAPIDeleteOneWithContext(collName string, filter bson.M, context context.Context) error
2225
RestfulAPIDeleteMany(collName string, filter bson.M) error
2326
RestfulAPIMergePatch(collName string, filter bson.M, patchData map[string]interface{}) error
2427
RestfulAPIJSONPatch(collName string, filter bson.M, patchJSON []byte) error
28+
RestfulAPIJSONPatchWithContext(collName string, filter bson.M, patchJSON []byte, context context.Context) error
2529
RestfulAPIJSONPatchExtend(collName string, filter bson.M, patchJSON []byte, dataName string) error
2630
RestfulAPIPost(collName string, filter bson.M, postData map[string]interface{}) (bool, error)
2731
RestfulAPIPostMany(collName string, filter bson.M, postDataArray []interface{}) error
2832
GetUniqueIdentity(idName string) int32
2933
CreateIndex(collName string, keyField string) (bool, error)
34+
StartSession() (mongo.Session, error)
35+
SupportsTransactions() (bool, error)
3036
}
3137

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

102+
func (db *MongoDBClient) RestfulAPIDeleteOneWithContext(collName string, filter bson.M, context context.Context) error {
103+
return db.MongoClient.RestfulAPIDeleteOneWithContext(collName, filter, context)
104+
}
105+
96106
func (db *MongoDBClient) RestfulAPIDeleteMany(collName string, filter bson.M) error {
97107
return db.MongoClient.RestfulAPIDeleteMany(collName, filter)
98108
}
@@ -105,6 +115,10 @@ func (db *MongoDBClient) RestfulAPIJSONPatch(collName string, filter bson.M, pat
105115
return db.MongoClient.RestfulAPIJSONPatch(collName, filter, patchJSON)
106116
}
107117

118+
func (db *MongoDBClient) RestfulAPIJSONPatchWithContext(collName string, filter bson.M, patchJSON []byte, context context.Context) error {
119+
return db.MongoClient.RestfulAPIJSONPatchWithContext(collName, filter, patchJSON, context)
120+
}
121+
108122
func (db *MongoDBClient) RestfulAPIJSONPatchExtend(collName string, filter bson.M, patchJSON []byte, dataName string) error {
109123
return db.MongoClient.RestfulAPIJSONPatchExtend(collName, filter, patchJSON, dataName)
110124
}
@@ -124,3 +138,11 @@ func (db *MongoDBClient) GetUniqueIdentity(idName string) int32 {
124138
func (db *MongoDBClient) CreateIndex(collName string, keyField string) (bool, error) {
125139
return db.MongoClient.CreateIndex(collName, keyField)
126140
}
141+
142+
func (db *MongoDBClient) StartSession() (mongo.Session, error) {
143+
return db.MongoClient.StartSession()
144+
}
145+
146+
func (db *MongoDBClient) SupportsTransactions() (bool, error) {
147+
return db.MongoClient.SupportsTransactions()
148+
}

mongoapi/mongoapi.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,13 @@ func (c *MongoClient) RestfulAPIPutMany(collName string, filterArray []bson.M, p
194194
}
195195

196196
func (c *MongoClient) RestfulAPIDeleteOne(collName string, filter bson.M) error {
197+
return c.RestfulAPIDeleteOneWithContext(collName, filter, context.TODO())
198+
}
199+
200+
func (c *MongoClient) RestfulAPIDeleteOneWithContext(collName string, filter bson.M, context context.Context) error {
197201
collection := c.Client.Database(c.dbName).Collection(collName)
198202

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

247251
func (c *MongoClient) RestfulAPIJSONPatch(collName string, filter bson.M, patchJSON []byte) error {
252+
return c.RestfulAPIJSONPatchWithContext(collName, filter, patchJSON, context.TODO())
253+
}
254+
255+
func (c *MongoClient) RestfulAPIJSONPatchWithContext(collName string, filter bson.M, patchJSON []byte, context context.Context) error {
248256
collection := c.Client.Database(c.dbName).Collection(collName)
249257

250258
originalData, err := getOrigData(collection, filter)
@@ -271,7 +279,7 @@ func (c *MongoClient) RestfulAPIJSONPatch(collName string, filter bson.M, patchJ
271279
if err := json.Unmarshal(modified, &modifiedData); err != nil {
272280
return fmt.Errorf("RestfulAPIJSONPatch Unmarshal err: %+v", err)
273281
}
274-
if _, err := collection.UpdateOne(context.TODO(), filter, bson.M{"$set": modifiedData}); err != nil {
282+
if _, err := collection.UpdateOne(context, filter, bson.M{"$set": modifiedData}); err != nil {
275283
return fmt.Errorf("RestfulAPIJSONPatch UpdateOne err: %+v", err)
276284
}
277285
return nil
@@ -826,3 +834,23 @@ func (c *MongoClient) RestfulAPIPutOnly(collName string, filter bson.M, putData
826834
err = fmt.Errorf("failed to update document: %s", err)
827835
return err
828836
}
837+
838+
func (c *MongoClient) StartSession() (mongo.Session, error) {
839+
return c.Client.StartSession()
840+
}
841+
842+
func (c *MongoClient) SupportsTransactions() (bool, error) {
843+
command := bson.D{{"hello", 1}}
844+
result := c.Client.Database(c.dbName).RunCommand(context.Background(), command)
845+
var status bson.M
846+
if err := result.Decode(&status); err != nil {
847+
return false, fmt.Errorf("failed to get server status: %v", err)
848+
}
849+
if msg, ok := status["msg"]; ok && msg == "isdbgrid" {
850+
return true, nil // Sharded clusters support transactions
851+
}
852+
if _, ok := status["setName"]; ok {
853+
return true, nil
854+
}
855+
return false, nil
856+
}

0 commit comments

Comments
 (0)