How would I modify the following to fail with concurrent writes (Using If-Match and ETag headers) ?
let sync = Runtime.getSync();
exports.handler = function(context, event, callback) {
let map = sync.syncMaps("MyMap");
map.syncMapItems(event.Digits).fetch().then(item => {
map.syncMapItems(event.Digits).update({key: item.key, data:item.data + 1})
.then(item2 => {
}).catch(err => {
console.log("Update Error:" + err);
});
}).catch(err => {
console.log("Fetch Error:" + err);
});
}
Twilio developer evangelist here.
The documentation on mutating data and protecting against conflicts in Twilio Sync does indeed mention that you can use If-Match
and ETag
headers.
The documentation calls out that:
Please note that If-Match header support is not currently enabled in the REST helper libraries. Support is coming soon.
So, if you want to use the If-Match
header to ensure you are not writing conflicting entries to the Sync Map Item then you will need to build the HTTP requests yourself.
Everything in the documentation for Sync Documents and If-Match
applies to individual Sync Map Items too.
The documentation for the REST API for Sync Map Items includes how to build up the URL you need to make the request yourself.
Let me know if this helps and if you have any trouble making the requests yourself.