• This repository has been archived on 03/Dec/2019
  • Stars
    star
    184
  • Rank 209,187 (Top 5 %)
  • Language
    JavaScript
  • Created almost 9 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PouchDB Redux Middleware

pouch-redux-middleware

By Build Status

Redux Middleware for syncing state and a PouchDB database.

Propagates changes made to a state into PouchDB. Propagates changes made to PouchDB into the state.

Install

$ npm install pouch-redux-middleware --save

Overview

pouch-redux-middleware will automatically populate a part of the store, specified by path, with the documents using the specified actions. This "sub-state" will be a list of the documents straight out of the database. When the database is modified by a 3rd party (e.g. by replication) a Redux action will be dispatched to update the "sub-state". Conversely, if you alter a document within the "sub-state", then the document will be updated in the database.

  • If a new document is created in the database (e.g. by replication or directly using db.post), then the corresponding insert action will be dispatched. If a document is updated in the database (e.g. by replication or directly), then the corresponding update action will be dispatched. If a document is deleted in the database (e.g. by replication or directly), then the corresponding remove action will be dispatched.
  • If you add a document to the "sub-state", then the document will be added to the database automatically (you should specify keys such as _id). If you alter a document in the "sub-state", the document will be updated in the database automatically. If you remove a document from the "sub-state", the document will be removed from the database automatically.
  • You may specify that that only a subset of the database's documents should populate the store by using changeFilter which effectively filters the documents under consideration.

Example

Example of configuring a store:

import * as types from '../constants/ActionTypes'
import PouchMiddleware from 'pouch-redux-middleware'
import { createStore, applyMiddleware } from 'redux'
import rootReducer from '../reducers'
import PouchDB from 'pouchdb'

export default function configureStore() {
  const db = new PouchDB('todos');

  const pouchMiddleware = PouchMiddleware({
    path: '/todos',
    db,
    actions: {
      remove: doc => { return { type: types.DELETE_TODO, id: doc._id } },
      insert: doc => { return { type: types.INSERT_TODO, todo: doc } },
      batchInsert: docs => { return { type: types.BATCH_INSERT_TODOS, todos: docs } }
      update: doc => { return { type: types.UPDATE_TODO, todo: doc } },
    }
  })

  const store = createStore(
    rootReducer,
    undefined,
    applyMiddleware(pouchMiddleware)
  )

  return store
}

API

PouchMiddleware(paths)

  • paths: path or array containing path specs

A path spec is an object describing the behaviour of a sub-tree of the state it has the following attributes:

  • path: a JsonPath path where the documents will stored in the state as an array
  • db: a PouchDB database
  • actions: an object describing the actions to perform when initially inserting items and when a change occurs in the db. It's an object with keys containing a function that returns an action for each of the events (remove, insert, batchInsert and update)
  • changeFilter: a filtering function that receives a changed document, and if it returns false, the document will be ignored for the path. This is useful when you have multiple paths in a single database that are differentiated through an attribute (like type).
  • handleResponse a function that is invoked with the direct response of the database, which is useful when metadata is needed or errors need custom handling. Arguments are error, data, callback. callback must be invoked with a potential error after custom handling is done.
  • initialBatchDispatched a function that is invoked once the initial set of data has been read from pouchdb and dispatched to the redux store or if the data fails to be read from pouchdb it is called with an error. This comes handy if you want skip the initial updates to a store subscriber by delaying the subscription to the redux store until the initial state is present. For example, when your application is first loaded you may wish to delay rendering until the store is updated.

Example of a path spec:

{
  path: '/todos',
  db,
  actions: {
    remove: doc => { return { type: types.DELETE_TODO, id: doc._id } },
    insert: doc => { return { type: types.INSERT_TODO, todo: doc } },
    batchInsert: docs => { return { type: types.BATCH_INSERT_TODOS, todos: docs } }
    update: doc => { return { type: types.UPDATE_TODO, todo: doc } },
  }
}

License

ISC

More Repositories

1

skiff

Raft Consensus for Node.js, backed by LevelDB
JavaScript
247
star
2

styled-is

A flag utility for styled-components
JavaScript
210
star
3

graphql-workshop

GraphQL Workshop Material
JavaScript
175
star
4

pouch-websocket-sync-example

Pouch Stream example
JavaScript
147
star
5

normalized-styled-components

normalize.css with a styled-components mindset
JavaScript
50
star
6

pouch-websocket-sync

PouchDB live sync through websockets
JavaScript
46
star
7

react-topology

Create Topology views with SVG and React
JavaScript
45
star
8

joyent-portal

JavaScript
44
star
9

kubernetes-auth

👩‍💻🔐 Log into k8s clusters using dex with a nice UI
Go
34
star
10

career-framework

Framework for career progression @ YLD
28
star
11

react-lazy-youtube

React Component for lazy loading youtube videos
JavaScript
25
star
12

react-native-wearables

React Native common interface for iOS and Android wearables
JavaScript
23
star
13

react-native-offline-first

React Native offline first with pouchdb
JavaScript
23
star
14

react-example

JavaScript
21
star
15

yld.io

YLD Website
JavaScript
19
star
16

serverpush-serviceworkers-example

HTTP/2 Server Push & Service Workers example
JavaScript
18
star
17

message-queue

a standard interface to access message queues
JavaScript
17
star
18

pouch-clerk

PouchDB worker reacting to document changes
JavaScript
17
star
19

native-iso-relay

JavaScript
16
star
20

asap-hub

The monorepo for the backend, frontend, and infrastructure of the ASAP hub.
TypeScript
15
star
21

standards

The standard way of doing things in YLD
12
star
22

pouch-remote-stream

Remote Stream for PouchDB
JavaScript
12
star
23

cloud-pizza-place

Our pizza oven is serverless, hot and always ready for baking cheesy items from the YLD menu!
JavaScript
11
star
24

pouch-stream-multi-sync

Sync various PouchDBs over a stream
JavaScript
11
star
25

copilot

JavaScript
10
star
26

license-bot

🐙🤖 A Github bot to help with Open-Source license conformence
Go
10
star
27

borough

Borough
JavaScript
9
star
28

training-node-angular-source-code

Angluar and Node Training Source Code
JavaScript
8
star
29

bpf-nodejs

Using BPF (http://iovisor.github.io/bcc/) with Node.js
Python
8
star
30

redux-replaceable-middleware

Redux Replaceable Middleware
JavaScript
7
star
31

website

Shiny new website
JavaScript
7
star
32

whisper-ui

Helper native app to run whisper on local machines.
Rust
7
star
33

remcalc

Calculate the rem's from px values.
JavaScript
6
star
34

staff-recommendations

Need help? Need ideas? Do you have ideas? Check our guides!
6
star
35

disclosure

Project dependencies overview
JavaScript
6
star
36

pseudo-yaml-ast

JavaScript
6
star
37

vue-styled-is

Flag utility for styled-components in vue
JavaScript
6
star
38

pouch-clerk-example-app

Pouch Clerk Example App
JavaScript
5
star
39

react-grid-debugger

Visual Tool for debugging grid-based layouts built on React
JavaScript
5
star
40

metrics-yall

Make them pretty metrics 🚀
JavaScript
5
star
41

node-lambda-example

Example of a Node.js Lambda function
Makefile
4
star
42

teg-august-training-node-code

JavaScript
4
star
43

alpine-node-containerpilot

🐳 - 🏔🐢🗃🛩
Shell
3
star
44

oss-stats

JavaScript
3
star
45

react-native-wearables-demo

Objective-C
3
star
46

razzle-plugin-stylable

Get stylable running in your razzle installation
JavaScript
3
star
47

website-contentful-data-transfer

Standalone script that fetched up-to-date info from associated services (Meetup, YouTube) and creates new Contentful entries as necessary
JavaScript
3
star
48

eslint-config-joyent-portal

JavaScript
3
star
49

relay-starter

JavaScript
2
star
50

heart-rate-validating-admission-webhook

Capture your heart-rate in Node.js and perform action based on the data!
JavaScript
2
star
51

chartjs-chart-box-plot

📈 A Box-plot implementation for chart.js
JavaScript
2
star
52

BackendCSharpHomeCodeChallengeBase

Backend C# Challenge
C#
2
star
53

deeplinking-poc

Deeplinking proof of concept
Kotlin
2
star
54

kubernetes-usage

Reporting tool of usage of resources on kubernetes
Shell
2
star
55

joyent-react-scripts

JavaScript
1
star
56

docker-compose-api

Python
1
star
57

coding-challenge

CSS
1
star
58

babel-preset-joyent-portal

JavaScript
1
star
59

react-screenshot-renderer

JavaScript
1
star
60

hello-world

JavaScript
1
star
61

Referral-Links

Referral links
1
star
62

bench-node-cluster

JavaScript
1
star
63

unitcalc

JavaScript
1
star
64

ecs-docker-test-app

ECS Docker Test App
JavaScript
1
star
65

conference-template

Template project to create websites for YLD's conferences
TypeScript
1
star
66

rnd-id

JavaScript
1
star