# pouchdb-persist

> PouchDB plugin for persistent replication

Latest version **0.0.6** (published 2014-10-22) · Apache license · 0 weekly downloads

## Install

```sh
npm install pouchdb-persist
pnpm add pouchdb-persist
yarn add pouchdb-persist
bun add pouchdb-persist
```

## 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.0.6 |
| Published | 2014-10-22 |
| First published | 2014-10-10 |
| Weekly downloads | 0 |
| License | Apache |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 28 |
| Author | Geoffrey Cox |
| Maintainers | redgeoff |
| Keywords | pouch, pouchdb, replication, persistent, persistent replication, fault-tolerant, retry, couch, couchdb |

## Links

- npm: https://www.npmjs.com/package/pouchdb-persist
- Repository: https://github.com/redgeoff/pouchdb-persist
- Issues: https://github.com/redgeoff/pouchdb-persist/issues
- npm.io page: https://npm.io/package/pouchdb-persist

## Dependencies (4)

- [lie](https://npm.io/package/lie.md) ^2.6.0
- [es3ify](https://npm.io/package/es3ify.md) ^0.1.3
- [inherits](https://npm.io/package/inherits.md) ~2.0.1
- [argsarray](https://npm.io/package/argsarray.md) 0.0.1

## Recent versions

- 0.0.6 (latest) — 2014-10-22
- 0.0.5 — 2014-10-22
- 0.0.4 — 2014-10-10
- 0.0.3 — 2014-10-10
- 0.0.2 — 2014-10-10

## README

PouchDB Persist
=====

[![Build Status](https://travis-ci.org/redgeoff/pouchdb-persist.svg)](https://travis-ci.org/redgeoff/pouchdb-persist)

Persistent replication in PouchDB

Why?
----
The `replicate()` routines in PouchDB are not fault-tolerant and will stop replicating if there are any network disruptions. PouchDB Persist implements an exponential backoff routine that will keep retrying until your connection is restored.

Live Demo
----
[Demo](http://redgeoff.github.io/pouchdb-persist/examples/index.html)

Example 1
----

```js
var db = new PouchDB('todos');

// Instead of db.replicate()
var persist = db.persist({ url: 'http://localhost:5984/todos' });
```

This will automatically start the replication. 

Example 2
----

```js
var db = new PouchDB('todos');

var persist = db.persist({
  url: 'http://localhost:5984/todos',
  manual: true, // requires explict call to start replication
  to: {
    listeners: [{ method: 'on', event: 'uptodate', listener: function () {
      console.log('uptodate');
    }}]
  }
});

persist.on('connect', function () {
  console.log('connect');
});

persist.on('disconnect', function () {
  console.log('disconnect');
});

persist.start().then(function () {
  persist.stop().then(function () {
    persist.start();
  });
});

```

Usage
----

To use this plugin, include it after `pouchdb.js` in your HTML page:

```html
<script src="pouchdb.js"></script>
<script src="pouchdb-persist.js"></script>
```

Or install it via bower:

```
bower install pouchdb-persist
```

Or to use it in Node.js, just npm install it:

```
npm install pouchdb-persist
```

And then attach it to the `PouchDB` object:

```js
var PouchDB = require('pouchdb');
PouchDB.plugin(require('pouchdb-persist'));
```

API
----

**Create persistence**

```js
var persist = db.persist(opts);
```

where any of the options can be blank except the `url`. Here is an example:

```js
{
  url: 'http://localhost:5984/todos', // remote Couch URL
  maxTimeout: 60000, // max retry timeout, defaulted to 300000
  startingTimeout: 1000, // retry timeout, defaulted to 1000
  backoff: 1.1, // exponential backoff factor, defaulted to 1.1
  manual: false, // when true, start replication with start()
  changes: { // options for changes()
    opts: { live: true }
  },
  to: { // options for replicating to remote source
    opts: { live: true }, // replicate.to() options
    url: 'http://localhost:5984/todos', // remote URL
    onErr: function (err) { }, // error handler
    listeners: [{ method: 'once', event: 'uptodate', listener: function () { } }]
  },
  from: { // options for replicating from remote source
    opts: { live: true }, // replicate.from() options
    url: 'http://localhost:5984/todos', // remote URL
    onErr: function (err) { }, // error handler
    listeners: [{ method: 'once', event: 'uptodate', listener: function () { } }]
  }
}
```

**Start replication**

```js
persist.start([direction]);
```

where direction can be persist.BOTH, persist.TO or persist.FROM and is defaulted to persist.BOTH

**Stop replication**

```js
persist.stop([direction]);
```

where direction can be persist.BOTH, persist.TO or persist.FROM and is defaulted to persist.BOTH

**Listen for connect event**

```js
persist.on('connect', function () {
  console.log('connect');
});
```

Note: persist is also an [EventEmitter](https://www.npmjs.org/package/eventjs) and therefore has methods like `once`, `removeListener`, etc...

**Listen for disconnect event**

```js
persist.on('disconnect', function () {
  console.log('disconnect');
});
```

Running the included examples
----
Note: you must have couchdb installed and running and have Admin Party enabled

    npm install
    npm run dev

Visit the target example in your browser, e.g. http://127.0.0.1:8001/examples

Contributing
----
Interested in [contributing](CONTRIBUTING.md)?

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