Skip to content

Instantly share code, notes, and snippets.

@karlwestin
Created April 1, 2017 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlwestin/81fa111781350e2f77010f10c9e94cc1 to your computer and use it in GitHub Desktop.
Save karlwestin/81fa111781350e2f77010f10c9e94cc1 to your computer and use it in GitHub Desktop.
public save() {
let db = this.get_db();
let data = this.get_data();
let t = this;
console.log('Save:' + this._id);
db.get(this._id, {conflicts: false})
.catch(function (err) {
if (err.status === 404) {
return {}; // you can just return an empty map if docs not found
}
throw err; // re-throw any other err to propagate
})
.then(function (doc) {
// You probably don't need to handle 409s, unless you're trying to do multiple saves at the same time
data._rev = doc._rev;
return db.put(data); // put updated doc, will create new revision
})
.then(saveSuccessful)
.catch(function (err) {
console.log(err);
});
}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment