Redux-persist: support persisting to pouchdb?

Created on 23 Nov 2016  路  10Comments  路  Source: rt2zz/redux-persist

Hi there, would you consider to support persisting to pouchdb.
pouchdb supports sync wonderfully.
It will be great to combine pouchdb's sync and redux-persist's local persist.

All 10 comments

This is be a great storage engine. A cursory look at the docs https://pouchdb.com/api.html#fetch_document makes me think this will not be hard to implement, just need to conform to the expected storage api a la https://github.com/pellejacobs/redux-persist-node-storage/blob/master/src/index.ts

This could make for some really awesome features with syncing user session to a remote db or coordinating session across two remote peers

Has any work been done on this? There is a library called redux-pouchdb but it seems to be unmaintained and not currently even working.

Just looking into this myself & figured I'd share this relatively new project: https://github.com/medihack/redux-pouchdb-plus

Haven't used it but seems to take advantage of some of the synergies b/t pouch and redux

The most developed PouchDB library for RN seems to be https://github.com/stockulus/pouchdb-react-native but it doesn't use redux. It already handles syncing, though, with work being done on enabling attachments. Could be a good package to collaborate as it seems to have an easier API to work with.

redux-persist@5 no longer relies on getAllKeys which leads me to believe writing an adapter for pouchdb would be trivial, just need getItem, setItem, and removeItem.

There might be one hard thing involved in a pouchdb adapter. When a document is saved, pouchDb returns back a new copy of the doc with a new _rev (so that it'll detect if one tries to update the docs from an outdated version). But setItem has no direct way to add something to the main state...

Any upgrade on that? I'm starting a project that involves pouchDB + Redux and I'm highly interested in contribute with this awesome library!

I'm working on it, as the project evolves.

//WORK IN PROGRESS FUNCTION

import PouchDB from 'pouchdb';

function PouchDBAdapter () {
  const db = new PouchDB('anyNameYoudWant);
  const getItem = async (key) => {
    try {
      const test = await db.get(key);
      return test;
    } catch (err) {
      throw err;
    }
  };
  const setItem = async (key, value) => {
    try {
      const pouchDocument = await getItem(key);
      const localValue = { ...value };
      localValue._rev = pouchDocument._rev;
      localValue._id = pouchDocument._id;
      delete localValue._persist;
      try {
        const update = await db.put(localValue);
        localValue._rev = update.rev;
        return localValue;
      } catch(err) {
        console.log('ERROR ON UPDATE IS : ', err)
        throw err;
      }
    } catch (err) {
      const localValue = { ...value };
      localValue._id = key;
      delete localValue._persist;
      try {
        const insertRow = await db.put(localValue);
        return insertRow;
      } catch(err) {
        throw err;
      }
    }
  };
  return { getItem, setItem };
};

//WORK IN PROGRESS FUNCTION

//USAGE
const storage = new PouchDBAdapter();

const rootPersistConfig = {
key: 'root',
storage: storage,
serialize : false,
debug : true
};

Was this page helpful?
0 / 5 - 0 ratings