Firebase with Promises and RxJS
Firebase is awesome, but callbacks suck. hard. This simple module adds to the Firebase prototype so that you can write code like this:
firebase
.child('users')
.child(userId)
.promiseUpdate({
firstName: 'foo',
lastName: 'bar' })
.then( newData => ... )
.catch( error => ... )
or like this:
firebase
.child('userRegistration')
.observe('child_added')
.map(observedData => ({
id: observedData.snapshot.key(),
userInfo: observedData.snapshot.val() }) )
.forEach(request => processUserRegistration(request.id, request.userInfo));
- observe(eventType)
- promiseAuthAnonymously(options)
- promiseAuthWithCustomToken(token)
- promiseAuthWithPassword(email, password, options)
- promiseChangePassword(email, oldPassword, newPassword)
- promiseResetPassword(email)
- promiseCreateUser(email, password)
- promiseSet(value)
- promiseUpdate(value)
- promisePush(value)
- promiseTransaction(updateFunction)
- promiseRemove()
- promiseOnce(eventType)
- promiseValue()
- getTimestamp() (returns Firebase.ServerValue.TIMESTAMP)
- Documentation
- Tests
- Any missing Firebase functions
Pull requests are welcome.
First, install the package with npm:
npm install firebase-promisified --save
Then, simply require firebase-promisified and invoke it after Firebase and optionally Rx are in scope. You can then export a Firebase instance and use this module elsewhere in your code.
'use strict';
const Firebase = require('firebase');
const Rx = require('rx');
const firebasePromisified = require('firebase-promisified');
// adds Rx and Promises to the Firebase prototype
firebasePromisified(Firebase, Promise, Rx);
module.exports = new Firebase('your firebase url');
MIT