From adb63b199d6805637e571af99679c2d50841a167 Mon Sep 17 00:00:00 2001 From: Patricia Reinoso Date: Tue, 21 Jan 2025 16:35:10 +0100 Subject: [PATCH] feat: add context to POST operations to be used with transactions (#104) * add context to POST MANY operation to be used with transactions Signed-off-by: Patricia Reinoso * add post Signed-off-by: Patricia Reinoso --------- Signed-off-by: Patricia Reinoso --- VERSION | 2 +- mongoapi/dbadapter.go | 10 ++++++++++ mongoapi/mongoapi.go | 10 +++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 6c4eee7..c114700 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.2.11-dev +1.2.11 diff --git a/mongoapi/dbadapter.go b/mongoapi/dbadapter.go index b048cb6..2ed05ea 100644 --- a/mongoapi/dbadapter.go +++ b/mongoapi/dbadapter.go @@ -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) @@ -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) } diff --git a/mongoapi/mongoapi.go b/mongoapi/mongoapi.go index a444160..5c2d761 100644 --- a/mongoapi/mongoapi.go +++ b/mongoapi/mongoapi.go @@ -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