# cloudant-upsert

> ![](https://img.shields.io/badge/status-stable-green.svg) ![](https://img.shields.io/badge/license-MIT-blue.svg)

Latest version **0.11.1** (published 2018-09-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install cloudant-upsert
pnpm add cloudant-upsert
yarn add cloudant-upsert
bun add cloudant-upsert
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.11.1 |
| Published | 2018-09-24 |
| First published | 2017-05-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 29 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Carmine DiMascio |
| Maintainers | dimascio |
| Keywords | cloudant, couchdb, upsert, database |

## Links

- npm: https://www.npmjs.com/package/cloudant-upsert
- Repository: https://github.com/cdimascio/cloudant-upsert
- Homepage: https://github.com/cdimascio/cloudant-upsert#readme
- Issues: https://github.com/cdimascio/cloudant-upsert/issues
- npm.io page: https://npm.io/package/cloudant-upsert

## Recent versions

- 0.11.1 (latest) — 2018-09-24
- 0.11.0 — 2018-09-24
- 0.9.4 — 2017-05-13

## README

# cloudant-upsert

![](https://img.shields.io/badge/status-stable-green.svg) ![](https://img.shields.io/badge/license-MIT-blue.svg)

A no-dependency module that adds an `upsert` function to the [nodejs-cloudant](https://github.com/cloudant/nodejs-cloudant/blob/master/cloudant.js) module.

<p align="center">
<img src="https://raw.githubusercontent.com/cdimascio/cloudant-upsert/master/assets/cloudant-upsert.png"/>
</p>

## 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('<YOUR_CLOUDANT_URI>');
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)

---
_Source: https://npm.io/package/cloudant-upsert · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
