Skip to content

Commit

Permalink
feat: add context to POST operations to be used with transactions (#104)
Browse files Browse the repository at this point in the history
* add context to POST MANY operation to be used with transactions

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

* add post

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

---------

Signed-off-by: Patricia Reinoso <[email protected]>
  • Loading branch information
patriciareinoso authored Jan 21, 2025
1 parent 493828c commit adb63b1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.11-dev
1.2.11
10 changes: 10 additions & 0 deletions mongoapi/dbadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ type DBInterface interface {
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)
RestfulAPIPostWithContext(collName string, filter bson.M, postData map[string]interface{}, context context.Context) (bool, error)
RestfulAPIPostMany(collName string, filter bson.M, postDataArray []interface{}) error
RestfulAPIPostManyWithContext(collName string, filter bson.M, postDataArray []interface{}, context context.Context) error
GetUniqueIdentity(idName string) int32
CreateIndex(collName string, keyField string) (bool, error)
StartSession() (mongo.Session, error)
Expand Down Expand Up @@ -132,10 +134,18 @@ func (db *MongoDBClient) RestfulAPIPost(collName string, filter bson.M, postData
return db.MongoClient.RestfulAPIPost(collName, filter, postData)
}

func (db *MongoDBClient) RestfulAPIPostWithContext(collName string, filter bson.M, postData map[string]interface{}, context context.Context) (bool, error) {
return db.MongoClient.RestfulAPIPostWithContext(collName, filter, postData, context)
}

func (db *MongoDBClient) RestfulAPIPostMany(collName string, filter bson.M, postDataArray []interface{}) error {
return db.MongoClient.RestfulAPIPostMany(collName, filter, postDataArray)
}

func (db *MongoDBClient) RestfulAPIPostManyWithContext(collName string, filter bson.M, postDataArray []interface{}, context context.Context) error {
return db.MongoClient.RestfulAPIPostManyWithContext(collName, filter, postDataArray, context)
}

func (db *MongoDBClient) GetUniqueIdentity(idName string) int32 {
return db.MongoClient.GetUniqueIdentity(idName)
}
Expand Down
10 changes: 9 additions & 1 deletion mongoapi/mongoapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,18 @@ func (c *MongoClient) RestfulAPIPost(collName string, filter bson.M, postData ma
return c.RestfulAPIPutOne(collName, filter, postData)
}

func (c *MongoClient) RestfulAPIPostWithContext(collName string, filter bson.M, postData map[string]interface{}, context context.Context) (bool, error) {
return c.RestfulAPIPutOneWithContext(collName, filter, postData, context)
}

func (c *MongoClient) RestfulAPIPostMany(collName string, filter bson.M, postDataArray []interface{}) error {
return c.RestfulAPIPostManyWithContext(collName, filter, postDataArray, context.TODO())
}

func (c *MongoClient) RestfulAPIPostManyWithContext(collName string, filter bson.M, postDataArray []interface{}, context context.Context) error {
collection := c.Client.Database(c.dbName).Collection(collName)

if _, err := collection.InsertMany(context.TODO(), postDataArray); err != nil {
if _, err := collection.InsertMany(context, postDataArray); err != nil {
return fmt.Errorf("RestfulAPIPostMany err: %+v", err)
}
return nil
Expand Down

0 comments on commit adb63b1

Please sign in to comment.