set method
Sets a data item. If the data item exists it updates it, otherwise it create a new data item.
correlation_id
(optional) transaction id to trace execution through call chain.item
a item to be set. Return Future that receives updated item Throws error.
Implementation
@override
Future<T?> set(String? correlationId, T? item) async {
if (item == null) {
return null;
}
var jsonMap = convertFromPublic(item, createUid: true);
var query = {'_id': item.id};
var update = {r'$set': jsonMap};
// Bug with ObjectId when returnNew true
// var result = await collection?.findAndModify(
// query: query, update: update, returnNew: true, upsert: true);
var result = await collection?.updateOne(query, update, upsert: true);
if (result != null && result.ok == 1.0) {
logger.trace(
correlationId, 'Set in %s with id = %s', [collectionName, item.id]);
return item; // convertToPublic(result);
}
// if (result != null) {
// }
return null;
}