Mobx Firebase Database
mobx-firebase-database allows you to map your Firebase data to MobX observables and interact with/react to it using MobX.
Install
yarn add mobx-firebase-database # If you're using firebase web yarn add firebase # If you're using firebase-admin yarn add firebase-admin # If you're using react-native-firebase yarn add react-native-firebase
Peer Dependencies
mobx-firebase-database works with : firebase
, firebase-admin
and react-native-firebase
Depending on which one you're using you will need to have it installed.
Usage
Web
Before starting to code, you need to get your Firebase Credentials :
Follow the steps here to get your config.
;;; // Don't worry about calling it more than once.; // toBox;;; ; // always contains the latest value of posts/${postId}await postRef.update;value.get; // {...post, last_seen_at: number}await postRef.setnull;value.get; // null // toMap;;;;;
API
mobxFirebaseDatabase
Input :
getMobxFire requires as input an object with shape :
- config:
- firebase : Firebase web client from
firebase
package
Output :
Object with shape :
;
toBox
and toMap
Input
All methods take in 2 arguments, the first is required and the second optional :
ref
: Any firebase ref, with or without sorting and/or limiting.options
: depends on the method
toBox
;
toMap
;
Output
An object with the following shape :
-
value
: Observable box with the latest value of the ref inside, or null if it doesn't exist or is not fetched yet. -
unsub
: A function to turn off the firebase ref listener when you don't need it anymore.
toBox
;
toMap
;// map: ObservableMap<string, any>// keys: IObservableArray<string>