npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mobx-firebase-database

v2.1.3

Published

Tame your firebase database with MobX

Downloads

12

Readme

Mobx Firebase Database

CircleCI NPM BundlePhobia

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.

import getMobxFire from "mobx-firebase-database";
import firebase from "firebase/app";
import "firebase/database";

// Don't worry about calling it more than once.
const { toBox, toMap, getFirebaseRef, destroy } = getMobxFire({
  config,
  firebase
});

// toBox
const postId = `my_post_id`;
const postRef = getFirebaseRef({ path: `posts/${postId}` });
const { value, unsub } = toBox(postRef);

const post = value.get(); // always contains the latest value of posts/${postId}
await postRef.update({ last_seen_at: firebase.database.ServerValue.TIMESTAMP });
value.get(); // {...post, last_seen_at: number}
await postRef.set(null);
value.get(); // null

// toMap
const postId = `my_post_id`;
const postsRef = getFirebaseRef({ path: `posts`, orderByKey: true });
const { value, unsub } = toMap(postRef);
const allPostsIds = value.keys();
const post = value.get(`${postId}`);

API

mobxFirebaseDatabase

Input :

getMobxFire requires as input an object with shape :

  • config:
{
  apiKey: string,
  authDomain: string,
  databaseURL: string,
  storageBucket: string
}
  • firebase : Firebase web client from firebase package

Output :

Object with shape :

type Output<T> = {
  toArray: ToArray;
  toBox: ToBox;
  toMap: ToMap;
  getFirebaseRef: (query: FirebaseQuery) => any;
  destroy: () => void;
};

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
type ToBoxOptions<V> = {
  map?: (m: V) => any;
  filter?: (m: V) => boolean;
  initial?: V | null;
};
toMap
type ToMapOptions<K, V> = {
  mapKey?: (m: K) => any;
  mapValue?: (m: V) => any;
  filter?: (prevValue: V, currentValue: V) => boolean;
  initial?: ObservableMap<K, V>;
};

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
const { value, unsub } = toBox(ref, { initial: "something" });
toMap
const { value: map, keys } = toMap(ref);
// map: ObservableMap<string, any>
// keys: IObservableArray<string>