Git Product home page Git Product logo

redux-firebase-middleware's Introduction

redux-firebase-middleware NPM version Build Status Dependency Status

Redux middleware for firebase, support native web API or react-native-firebase API.

NOTE: Only support for Firebase realtime database at this moment, welcome PRs for supporting Firestore

Why?

Firebase SDK is hard to achieve strict unidirectional data flow in Redux. If you have a hard time manage your Redux states from Firebase realtime database to your Redux store. This middleware help you seamlessly integrate Firebase with Redux workflow.

Installation

$ npm install --save redux-firebase-middleware

Usage

Store

Setting up in your redux store

Web
/** firebase web api **/
const {applyMiddleware, createStore, compose} = require('redux');
const {firMiddleware} = require('redux-firebase-middleware');

const config = {
  apiKey: 'xxxxxxxxxxx',
  authDomain: 'xxxxxxxxxxx',
  databaseURL: 'xxxxxxxxxxx',
  projectId: 'xxxxxxxxxxx',
  storageBucket: 'xxxxxxxxxxx',
  messagingSenderId: 'xxxxxxxxxxx',
};

firebase.initializeApp(config);

const finalCreateStore = compose(
  applyMiddleware(thunk),
  applyMiddleware(firMiddleware(firebase)) // -----> apply fir middleware in redux store
)(createStore);
React-native
/** react-native-firebase native api **/
import RNFirebase from 'react-native-firebase';

const configOpts = {
  debug: true,
  persistence: true,
};

RNFirebase.initializeApp(configOpts);

const finalCreateStore = compose(
  applyMiddleware(thunk),
  applyMiddleware(firMiddleware(RNFirebase)) // -----> apply fir middleware in redux store
)(createStore);

.....

Basic operations (Read, and write data)

dispatching a firMiddleware action.

const {CALL_FIR_API} = require('redux-firebase-middleware');

export const GET_MY_REF = [
  'GET_MY_REF_REQUEST', // -------> first, must be request type
  'GET_MY_REF_SUCCESS', // -------> second, must be success type
  'GET_MY_REF_FAILURE', // -------> third, must be failure type
];

function callAnAction() {
  return dispatch({[CALL_FIR_API]: {
    types: GET_MY_REF, // -----> normally this should put in constants, see `constants`(next seciton) for more info
    ref: (db) => db.ref('test/path1'), // ----> your firebase reference path
    method: 'once_value',
  }});
}

Reducers

export default function reducer(state: calcState = initialState, action: FSA) {
  const {type, payload} = action;

  switch (type) {
    case 'GET_MY_REF_REQUEST':
      // update request state

    case 'GET_MY_REF_SUCCESS':
      // update success state
      // you can get data from payload.

    case 'GET_MY_REF_FAILURE':
      // update failure state
  }
}

Listener events (Reading and writing lists)

dispatching a firMiddleware listener actions.

  • types (Array<request, success, failure>) : action constants types
  • ref ((firebase.database) => firebase.database.Reference | firebase.database.Query): Instance of firebase reference or firebase query
  • method: could be one of, please reference to: https://firebase.google.com/docs/reference/js/firebase.database.Reference#on
    • on_value
    • on_child_added
    • on_child_changed
    • on_child_removed
    • on_child_moved
const {CALL_FIR_API} = require('redux-firebase-middleware');

export const GET_MY_REF = [
  'GET_MY_REF_REQUEST', // -------> first, must be request type
  'GET_MY_REF_SUCCESS', // -------> second, must be success type
  'GET_MY_REF_FAILURE', // -------> third, must be failure type
];

function callAnAction() {
  return dispatch({[CALL_FIR_API]: {
    types: GET_MY_REF, // -----> normally this should put in constants, see `constants`(next seciton) for more info
    ref: (db) => db.ref('test/path1'), // ----> your firebase reference path
    method: 'on_value',
  }});
}

To remove the listener, you'll get off method in actions' reducer.

Reducers

When the state is successful it'll received data as payload, payload's value is slightly different in different methods.

Payload in methods:

  • on_value: dataSnapshot
  • on_child_added: {childSnapshot, prevChildKey}
  • on_child_changed: {childSnapshot, prevChildKey}
  • on_child_removed: oldChildSnapshot
  • on_child_moved: {childSnapshot, prevChildKey}
export default function reducer(state: calcState = initialState, action: FSA) {
  // or if you're using event listeners you'll get additional `off` method to remove the listening event by calling `off()` 
  const {type, payload, off} = action

  switch (type) {
    case 'GET_MY_REF_REQUEST':
      // update request state

    case 'GET_MY_REF_SUCCESS':
      // update success state
      // you can get data from payload.

    case 'GET_MY_REF_FAILURE':
      // update failure state

    case 'REMOVE_LISTENER':
      // call off method to unlisten the event
      off();
  }
}

Customized payload

export const GET_CALC_CAR_CATEGORY = [
  'GET_MY_REF_REQUEST', // -------> first, must be request type
  {
    type: 'GET_MY_REF_SUCCESS', // ------> second, must be success type
    payload: (action: FirAPI, state: GetState, data: any) => {
      // you can do what ever you want, transforming data or manipulating data .... etc
      // get firebase data called `data.val()`
      return data.val();
    },
  },
  'GET_MY_REF_FAILURE', // -------> third, must be failure type
];

Credits

Inspired by redux-api-middleware

https://github.com/agraboso/redux-api-middleware

License

MIT © chilijung

redux-firebase-middleware's People

Contributors

chilijung avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

redux-firebase-middleware's Issues

Real Time Support

Hello,

This library looks very promising. I'm curious to see if it provides support for Firebase's real time features. Does it allow real time dispatching of actions based on updates made to the data in Firebase?

thanks

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.