{"library":"pouch-redux-middleware","title":"PouchDB Redux Middleware","description":"pouch-redux-middleware is a Redux middleware designed to synchronize a specified segment of the Redux store's state with a PouchDB database. It enables bi-directional data flow: changes in the PouchDB database (e.g., via replication or direct writes) are automatically propagated to the Redux store as dispatched actions, and conversely, modifications to the synchronized part of the Redux state are persisted back into PouchDB. The current stable version is 1.2.0. This package's primary differentiator is its abstraction of PouchDB's change feed into Redux actions, allowing developers to manage PouchDB data through standard Redux patterns. However, its development has ceased, meaning it is not actively maintained and may have compatibility issues with modern JavaScript ecosystems.","language":"javascript","status":"abandoned","last_verified":"Thu Apr 23","install":{"commands":["npm install pouch-redux-middleware"],"cli":null},"imports":["import PouchMiddleware from 'pouch-redux-middleware';","import { createStore, applyMiddleware } from 'redux';","import PouchDB from 'pouchdb';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import * as types from './constants/ActionTypes';\nimport PouchMiddleware from 'pouch-redux-middleware';\nimport { createStore, applyMiddleware } from 'redux';\nimport rootReducer from './reducers'; // Assume this file defines your root reducer\nimport PouchDB from 'pouchdb';\n\n// Placeholder for a simple reducer if rootReducer doesn't exist for quick testing\n// If you don't have a 'reducers' file, create a dummy one or replace 'rootReducer'\n// with a simple function like (state = { todos: [] }, action) => state;\nconst dummyReducer = (state = { todos: [] }, action) => {\n  switch (action.type) {\n    case types.INSERT_TODO:\n      return { ...state, todos: [...state.todos, action.todo] };\n    case types.UPDATE_TODO:\n      return { ...state, todos: state.todos.map(t => (t._id === action.todo._id ? action.todo : t)) };\n    case types.DELETE_TODO:\n      return { ...state, todos: state.todos.filter(t => t._id !== action.id) };\n    case types.BATCH_INSERT_TODOS:\n      return { ...state, todos: [...state.todos, ...action.todos] };\n    default:\n      return state;\n  }\n};\n\n// Placeholder for ActionTypes if not defined\nconst dummyActionTypes = {\n  DELETE_TODO: 'DELETE_TODO',\n  INSERT_TODO: 'INSERT_TODO',\n  BATCH_INSERT_TODOS: 'BATCH_INSERT_TODOS',\n  UPDATE_TODO: 'UPDATE_TODO'\n};\n\nexport default function configureStore() {\n  // Using in-memory PouchDB for simple demonstration. For persistence, use a file path or URL.\n  const db = new PouchDB('todos-db-' + Math.random()); \n\n  const pouchMiddleware = PouchMiddleware({\n    path: '/todos', // Path in Redux state where todos will be stored\n    db,\n    actions: {\n      remove: doc => { return { type: (types.DELETE_TODO || dummyActionTypes.DELETE_TODO), id: doc._id } },\n      insert: doc => { return { type: (types.INSERT_TODO || dummyActionTypes.INSERT_TODO), todo: doc } },\n      batchInsert: docs => { return { type: (types.BATCH_INSERT_TODOS || dummyActionTypes.BATCH_INSERT_TODOS), todos: docs } },\n      update: doc => { return { type: (types.UPDATE_TODO || dummyActionTypes.UPDATE_TODO), todo: doc } }\n    },\n    // Optional: changeFilter to only sync specific document types\n    // changeFilter: (doc) => doc.type === 'todo'\n  });\n\n  const store = createStore(\n    rootReducer || dummyReducer, // Use actual rootReducer or dummy one\n    undefined,\n    applyMiddleware(pouchMiddleware)\n  );\n\n  // Example: Manually add a todo to PouchDB, it should sync to Redux\n  setTimeout(() => {\n    db.post({ _id: 'item-1', text: 'Buy milk', completed: false, createdAt: new Date().toISOString() })\n      .then(response => console.log('Doc added to PouchDB:', response))\n      .catch(err => console.error('Error adding doc to PouchDB:', err));\n  }, 1000);\n  \n  // Example: Subscribe to Redux store changes to see the sync\n  store.subscribe(() => {\n    console.log('Redux state updated:', store.getState());\n  });\n\n  return store;\n}","lang":"javascript","description":"This quickstart demonstrates how to configure a Redux store with `pouch-redux-middleware`, connecting a specific state path (`/todos`) to a PouchDB instance. It shows how to map PouchDB changes to Redux actions for insert, update, and remove events, and includes basic PouchDB interaction to illustrate bi-directional sync.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}