Skip to content

Observable Firestore documents and collections using MobX

License

Notifications You must be signed in to change notification settings

0x80/firestore-mobx

Repository files navigation

Firestore MobX

WARNING Do not use this yet. There are still some fundamental issues to be solved and breaking API changes are likely to happen at any time.

This library was inspired by Firestorter. Read the migration docs if you are interested in the motivation and differences.

You should be able to use this in any Javascript application including React, React-Native and Node.js.

NOTE This library is based on my personal experience using Firestorter. If there are any features that you miss and deem essential, please let me know. It is well possible that I have overlooked some valid use-cases.

Features

  • A minimal and un-opinionated API surface
  • Written in Typescript with strict typings
  • Minimal dependencies (only Firebase and MobX really)

Install

yarn add firestore-mobx or npm install firestore-mobx

Usage

Restrictions on Dynamic Data Sourcing

Observable documents and collections are flexible because they can change their data source and query dynamically at any time. In order to reduce complexity and offer strong typing some restrictions are enforced.

Document

  1. An observable document always links to the Firestore collection passed into the constructor. A document can change its id after it was created, switching to a different document in Firestore, but the collection reference will never change. With Typescript we get compile-time type checks based on the schema you use to declare the instance with. If the source would be allowed to switch to a different collection this type would have no practical meaning.

    Also I do not think there is a need for this in a real-life application. If you need to observe documents from different collections just create multiple ObservableDocument instances.

Collection

  1. An observable collection always links to the Firestore collection passed into the constructor. The query can be changed after the object was created, influencing the number of documents in the collection, but it can not switch to a different collection dynamically. The motivation for this is the same as restriction 1 on observable documents.

  2. A collection without a query produces no documents. Retrieving all documents from a collection is not typically something you would do in a client-side application. By placing this restriction on collections it not only simplifies the logic but we can avoid fetching a large collection by accident. If you have a relatively small collection and you do want to fetch all of it, you can simply pass in a Firestore query that would include all documents. For example .orderBy("updatedAt", "desc"), .limit(999) or .after("0")

API

See the API docs.