https://github.com/cdimascio/cloudant-upsert
A no-dependency module that adds 'upsert' to Node.js Cloudant
https://github.com/cdimascio/cloudant-upsert
cloudant ibm-cloud nodejs upsert
Last synced: about 1 year ago
JSON representation
A no-dependency module that adds 'upsert' to Node.js Cloudant
- Host: GitHub
- URL: https://github.com/cdimascio/cloudant-upsert
- Owner: cdimascio
- License: mit
- Created: 2017-05-13T22:17:16.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-09-24T00:56:34.000Z (over 7 years ago)
- Last Synced: 2025-03-30T07:51:14.126Z (about 1 year ago)
- Topics: cloudant, ibm-cloud, nodejs, upsert
- Language: JavaScript
- Homepage:
- Size: 85.9 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cloudant-upsert
 
A no-dependency module that adds an `upsert` function to the [nodejs-cloudant](https://github.com/cloudant/nodejs-cloudant/blob/master/cloudant.js) module.
## Install
```shell
npm install cloudant-upsert
```
## Usage
The following adds the `upsert` function to a `cloudant` instance
```javascript
var Cloudant = require('@cloudant/cloudant');
var cloudant = Cloudant('');
require('cloudant-upsert')(cloudant);
```
## Examples
#### Promises
In the example below `prevdoc` contains `null` or the previous doc
```javascript
cloudant.db.use('mydb').upsert('carmine', prevdoc => ({
"text": "Woop Woop! We're using promises",
}))
.then(r => console.log('result', r))
.catch(e => console.log('error', e.message));
```
#### Callbacks
In the example below `prevdoc` contains `null` or the previous doc
```javascript
cloudant.db.use('mydb').upsert('carmine', prevdoc => ({
"text": "Woop! Woop! We're using callbacks"
}), (err, data) => {
if (err) console.log('error', err.reason)
else console.log('data', data)
})
```
## API
### Promise API
### `upsert(id, function(prevdoc)`)
| param | description |
| ------------- | ------------- |
| `id` | The document id to upsert |
| `function(prevdoc): doc ` | A function that provides the previous document (or null when inserting a new doc) as input. You must return the document to upsert. |
### Callback API
### `upsert(id, function(prevdoc), function(err, res)}`
| param | description |
| ------------- | ------------- |
| `id` | The document id to upsert |
| `function(prevdoc): doc` | A function that provides the previous document (or null when inserting a new doc) and returns a new document to upsert |
| `function(err, res): void` | A Node.js error first callback |
## License
[MIT](LICENSE)