# @abhinavzspace/redux-pouchdb-plus

> Synchronize Redux store with PouchDB to have a persistent store.

Latest version **0.8.4** (published 2016-10-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @abhinavzspace/redux-pouchdb-plus
pnpm add @abhinavzspace/redux-pouchdb-plus
yarn add @abhinavzspace/redux-pouchdb-plus
bun add @abhinavzspace/redux-pouchdb-plus
```

## 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.8.4 |
| Published | 2016-10-09 |
| First published | 2016-10-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 75 |
| Author | Kai Schlamp |
| Maintainers | abhinavzspace |
| Keywords | redux, react, pouchdb |

## Links

- npm: https://www.npmjs.com/package/@abhinavzspace/redux-pouchdb-plus
- Repository: https://github.com/medihack/redux-pouchdb-plus
- Homepage: https://github.com/medihack/redux-pouchdb-plus#readme
- Issues: https://github.com/medihack/redux-pouchdb-plus/issues
- npm.io page: https://npm.io/package/@abhinavzspace/redux-pouchdb-plus

## Dependencies (5)

- [node-uuid](https://npm.io/package/node-uuid.md) ^1.4.7
- [transit-js](https://npm.io/package/transit-js.md) ^0.8.846
- [lodash.isequal](https://npm.io/package/lodash.isequal.md) ^4.0.0
- [lodash.clonedeep](https://npm.io/package/lodash.clonedeep.md) ^4.1.0
- [transit-immutable-js](https://npm.io/package/transit-immutable-js.md) ^0.5.2

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.8.4 (latest) — 2016-10-09
- 0.8.3 — 2016-10-07
- 0.8.2 — 2016-10-06
- 0.8.1 — 2016-10-04

## README

# Redux PouchDB Plus

## About

**Redux PouchDB Plus** synchronizes a [Redux](rackt.github.io/redux) store with a [PouchDB](http://pouchdb.com/) database.

This code is heavily inspired (and some code reused) by Vicente de Alencar's [redux-pouchdb](https://github.com/vicentedealencar/redux-pouchdb).
So all Kudos to him. The rewrite was necessary to allow the following extras:

- Have different Pouch databases for different reducers.
- Allow to switch databases dynamically.
- Support for [Immutable](https://facebook.github.io/immutable-js/) states beside pure Javascript types.
- Provide several callbacks (when initialization and database access happens).

The code is quite well tested using [tape](https://github.com/substack/tape).

## Usage

### General setup

The reducers you wish to persist should be enhanced with this higher order reducer (`persistentReducer`).

``` js
import { persistentReducer } from 'redux-pouchdb-plus';

const counter = (state = {count: 0}, action) => {
  switch(action.type) {
  case INCREMENT:
    return { count: state.count + 1 };
  case DECREMENT:
    return { count: state.count - 1 };
  default:
    return state;
  }
};

const finalReducer = persistentReducer(counter);
```

Compose a store enhancer (`persistentStore`) with other enhancers in order to initialize the persistence.

``` js
import { persistentStore } from 'redux-pouchdb-plus';

const db = new PouchDB('dbname');

//optional
const applyMiddlewares = applyMiddleware(
  thunkMiddleware,
  loggerMiddleware
);

const createStoreWithMiddleware = compose(
  applyMiddlewares,
  persistentStore({db})
)(createStore);

const store = createStoreWithMiddleware(finalReducer, initialState);
```

You may also provide a specific database for this reducer (it is prioritized over
the provided database to the store).

```js
const db2 = new PouchDB('another_dbname');
const finalReducer = persistentReducer(counter, {db: db2});
```

### Switching databases during runtime

You may also provide a function that return a database connector instead of the
connector itself. This makes it possible to switch databases dynamically during runtime.

```js
import { reinit } from 'redux-pouchdb-plus';

let dbChoice = 1;
const db = (reducerName, store, additionalOptions) => {
  if (dbChoice === 1)
    return new PouchDB('dbname1');
  else
    return new PouchDB('dbname2');
}

// uses 'dbname1' database
const finalReducer = persistentReducer(counter, {db});

// switch to 'dbname2' database
dbChoice = 2;
// reinitialize reducer counter
store.dispatch(reinit('counter'));
// reinitialize all reducers
store.dispatch(reinit());
```

### Check if database is in sync

With the `inSync` method you can check if all state changes of persistent
reducers are saved to the database or if there is some saving in progress.

```js
import { inSync } from 'redux-pouchdb-plus';

if (inSync()) {
  // do something if the reducer states and the database are in sync
}
```

### Use Immutable js states

You can use [Immutable.js](https://facebook.github.io/immutable-js/) states
in your reducers. This works automatically if the **initial state is an
Immutable.js data type**.

```js
// automatically serializes Immutable.js data types to PouchDB
// when the initial state is an Immutable
const counter = (state = Immutable.Map({count: 0}), action) => {
  switch(action.type) {
  case INCREMENT:
    return { count: state.count + 1 };
  case DECREMENT:
    return { count: state.count - 1 };
  default:
    return state;
  }
};

const finalReducer = persistentReducer(counter);
```

It is even possible to mix immutable and plain Javascript data
as internally [transit-immutable-js](https://github.com/glenjamin/transit-immutable-js)
is used for serialization to the database. Just make sure that the top
state container of the reducer is an immutable.

```js
// this is a valid state
Immutable.Map({x: [1, 2, 3]});

// this will open the doors to hell
[1, 2, Immutable.Map({x: 3})];
```

### Provided callback functions

You may provide the following callback functions as addition options to
`persistentReducer` or `persistentReducer`:

```js
// example for persistentStore, but works the same for persistentReducer function.
persistentStore(counter, {
  db,
  onInit: (reducerName, reducerState, store) => {
    // Called when this reducer was initialized
    // (the state was loaded from or saved to the
    // database for the first time or after a reinit action).
  },
  onUpdate: (reducerName, reducerState, store) => {
    // Called when the state of reducer was updated with
    // data from the database.
    // Cave! The store still contains the state before
    // the updated reducer state was applied to it.
  },
  onSave: (reducerName, reducerState, store) => {
    // Called every time the state of this reducer was
    // saved to the database.
  }
});
```

Additionally you may provide an `onReady` callback on the store that is called
every time all persistent recuders finished the initialization.

```js
persistentStore(counter, {
  db,
  onReady: (store) => {
    // Called when all reducers are initialized (also after
    // a reinit for all reducers is finished).
  }
}
```

## Notes

The current behavior is to have one document for each persisted reducer that looks like:

``` js
{
  _id: 'reducerName', // the name the reducer function
  state: {}|[], // the state of the reducer
  _rev: '' // pouchdb keeps track of the revisions
}
```

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