Implementing promise / ottoman upsert

I’m trying to implement a “create or update” function for my ottoman models, after getting my feet wet and already having built a number of models successfully.

An issue I’m running into is a “natural key” for models that a user might send. Suppose someone POSTs something to my REST API; clearly I don’t know what the couchbase key is going to be. My model might have an auto-assigned uuid, but the user isn’t POSTing that because it’s my job to generate it. So I can’t look something up by that key. How then, given a JSON document from a user and an Ottoman model type, can I figure out whether I already have this item so as to update one in place rather than creating a new duplicate?

Additionally, has anyone looked into wrapping Ottoman functions in promises rather than using the standard callback(error, results)? In our code base we’re already using promises quite a bit, and using Ottoman callbacks introduces a different sort of mixed style; alternatively, if there’s a best practice on how to use something existing like bluebird to “promisify” Ottoman, I’d love to hear about it.


Hey @moxious,
I think I have already this for you elsewhere, but to reiterate here. Ottoman internally implements Couchbase CAS operations which allow you to do a lookup for a document, and then atomically modify it by performing your changes and then saving the document. If another actor has modified the document since you retreived it, an error will be returned indicated this.
Cheers, Brett

Thanks for the response Brett - I think you’re in contact with a colleague of mine about this on another thread (I’m not him, but I know about that and we’re working on similar stuff). This is good to know, thank you for the follow-up.