diff --git a/VERSION b/VERSION index f2b2cf9..9d4f823 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.2.9-dev +1.2.9 diff --git a/mongoapi/dbadapter.go b/mongoapi/dbadapter.go index 59f97c6..b048cb6 100644 --- a/mongoapi/dbadapter.go +++ b/mongoapi/dbadapter.go @@ -18,6 +18,7 @@ type DBInterface interface { RestfulAPIGetMany(collName string, filter bson.M) ([]map[string]interface{}, error) RestfulAPIPutOneTimeout(collName string, filter bson.M, putData map[string]interface{}, timeout int32, timeField string) bool RestfulAPIPutOne(collName string, filter bson.M, putData map[string]interface{}) (bool, error) + RestfulAPIPutOneWithContext(collName string, filter bson.M, putData map[string]interface{}, context context.Context) (bool, error) 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 @@ -87,6 +88,10 @@ func (db *MongoDBClient) RestfulAPIPutOne(collName string, filter bson.M, putDat return db.MongoClient.RestfulAPIPutOne(collName, filter, putData) } +func (db *MongoDBClient) RestfulAPIPutOneWithContext(collName string, filter bson.M, putData map[string]interface{}, context context.Context) (bool, error) { + return db.MongoClient.RestfulAPIPutOneWithContext(collName, filter, putData, context) +} + func (db *MongoDBClient) RestfulAPIPutOneNotUpdate(collName string, filter bson.M, putData map[string]interface{}) (bool, error) { return db.MongoClient.RestfulAPIPutOneNotUpdate(collName, filter, putData) } diff --git a/mongoapi/mongoapi.go b/mongoapi/mongoapi.go index 6e49726..a444160 100644 --- a/mongoapi/mongoapi.go +++ b/mongoapi/mongoapi.go @@ -125,6 +125,11 @@ func (c *MongoClient) RestfulAPIGetMany(collName string, filter bson.M) ([]map[s // if no error happened, return true means data existed and false means data not existed func (c *MongoClient) RestfulAPIPutOne(collName string, filter bson.M, putData map[string]interface{}) (bool, error) { + return c.RestfulAPIPutOneWithContext(collName, filter, putData, context.TODO()) +} + +// if no error happened, return true means data existed and false means data not existed +func (c *MongoClient) RestfulAPIPutOneWithContext(collName string, filter bson.M, putData map[string]interface{}, context context.Context) (bool, error) { collection := c.Client.Database(c.dbName).Collection(collName) existed, err := checkDataExisted(collection, filter) if err != nil { @@ -132,13 +137,13 @@ func (c *MongoClient) RestfulAPIPutOne(collName string, filter bson.M, putData m } if existed { - if _, err := collection.UpdateOne(context.TODO(), filter, bson.M{"$set": putData}); err != nil { + if _, err := collection.UpdateOne(context, filter, bson.M{"$set": putData}); err != nil { return false, fmt.Errorf("RestfulAPIPutOne UpdateOne err: %+v", err) } return true, nil } - if _, err := collection.InsertOne(context.TODO(), putData); err != nil { + if _, err := collection.InsertOne(context, putData); err != nil { return false, fmt.Errorf("RestfulAPIPutOne InsertOne err: %+v", err) } return false, nil