Skip to content

Commit

Permalink
Merge pull request #45 from vnguyen94/patch-1
Browse files Browse the repository at this point in the history
show upsert on README
  • Loading branch information
bsiddiqui authored Nov 2, 2016
2 parents 7bc5a28 + 3a5399d commit 5ce72cc
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,23 @@ update: function (data, options) {
})
}
```

### model.upsert
```js
/**
* Upsert - select a model based on data and update if found, insert if not found
* @param {Object} selectData Data for select
* @param {Object} updateData Data for update
* @param {Object} [options] Options for model.save
* @return {Promise(bookshelf.Model)} edited Model
*/
upsert: function (selectData, updateData, options) {
return this.findOne(selectData, extend(options, { require: false }))
.bind(this)
.then(function (model) {
return model
? model.save(updateData, extend({ patch: true }, options))
: this.create(extend(selectData, updateData), options)
})
}
```

0 comments on commit 5ce72cc

Please sign in to comment.