unistore-fire
v1.0.1
Published
Map your Firebase database to unistore Stores.
Downloads
5
Readme
Unistore Fire
Peer Dependencies
This library relies on having firebase (web, admin or react-native) and unistore installed and initialized. If you haven't installed them previously :
yarn add unistore firebase
# or yarn add unistore firebase-admin
# or yarn add unistore react-native-firebase
# Or npm i unistore firebase
Install
yarn add unistore-fire
# Or npm i unistore-fire
Usage
Realtime Database
Map your Firebase database to unistore Stores.
This module exports 3 methods :
- toBox
- toObject
- toArray
Usage
import { toBox, toObject, toArray } from "unistore-fire";
const ref = firebase.database().ref("path/to/data");
const box = toBox(ref);
box.getState(); // { value: valueFromFirebase }
const map = toObject(ref);
map.getState(); // { value: { [key:keyFromFirebase]: valueFromFirebase } }
const array = toArray(ref);
array.getState(); // { value: Array<{ key:keyFromFirebase, value:valueFromFirebase }> }
API
Input
Any firebase ref, with or without sorting, limiting
Output
A unistore store that always has the latest value of the ref inside it.
toBox
The state has the following shape :
type State = {
value: ValueFromFirebase | null;
};
toObject
The state has the following shape :
type State = {
value: { [key: keyFromFirebase]: ValueFromFirebase } | null;
};
toArray
The state has the following shape :
type State = {
value: Array<{ key: keyFromFirebase; value: ValueFromFirebase }>;
};
Check the tests for more examples !