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

flatten-subcollection-snapshots

v1.1.1

Published

Interpolates nested subcollections of a Firebase collection into a single object, and listens for changes along all levels.

Downloads

8

Readme

flatten-subcollection-snapshots

Interpolates nested subcollections of a Firestore collection into a single object, and listens for changes along all levels.

This package is useful when you:

  • have a set of nested subcollections in Firestore
  • would like to read all the data in the nested subcollections of a given document
  • need the data from all of a document's [recursive] subcollections to be returned as a property on that object.

Basic usage

import firebase from 'firebase/app';
import 'firebase/firestore';

import flattenSubcollectionSnapshots from 'flatten-subcollection-snapshots';

// an example data structure. This is what will be returned by
// the function, with the `object[]`s populated with subcollection docs.
type User = {
    name: string;
    id: number;

    projects: {
        title: string;
        data: object;

        contributors: {
            userId: number;
            permissionLevel: number;
        }[];

        views: {
            userId: number;
            time: firebase.firestore.Timestamp
        }[];
    }[];
}

// returns a function to unsubscribe _all_ listeners, including those nested in
// lower levels
const unsubscribe = flattenSubcollectionSnapshots<User>(
    // specifies the nesting properties of the data structure
    [
        {
            collection: 'projects',
            subcollections: [
                {
                    collection: 'contributors',
                },
                {
                    collection: 'views',
                }
            ]
        }
    ],
    
    // will be called whenever the document or its subcollections are updated
    (data) => {
        console.log('New update!', data);
    },
    
    // the parent document to search in. Can also be a collection, in
    // which case the fourth parameter should be set to `true`
    firebase.firestore().collection('users').doc('my-user-id'),
    
    // specifies whether the parent reference is a collection. 
    // `false` is assumed as the default value, so can be omitted.
    false,
);

Parameter reference

returns - type: ListenerUnsubscriber

This is an object which contains an "unsubscribe" property, which, when called, will unbind all listeners so as to prevent memory leaks or prevent unnecessary reads once the data is no longer needed.

1. rules (type: SubcollectionFlattenerRuleset<T>)

Specifies the nesting properties of the data structure. This should take the form of an array of objects, with a "collection" property providing the name of the subcollection, and a "subcollections" array providing all the nested subcollections within.

2. updater (type: (data: ExpectedResult<T, typeof rules>) => void)

Is called on every update to the document, or to any documents nested in subcollections within. The data parameter is either an array of objects, if the fourth argument to flattenSubcollectionSnapshots is true, or otherwise just an object.

Note that the type of data is not simply the original type passed, but rather a version of it with all properties made optional (as different requests will load at different times) - you may need to implement your own checks to see if your data has been fully populated before use.

3. parent (type: CollectionReference | DocumentReference)

The parent object in which to search for documents and unpack subcollections. Can be either a collection, in which case all objects in the collection are unpacked, or just a single document, in which case only one document is returned.

4. isCollection (type: boolean = false)

This boolean reflects the type of parent, with a value of true if parent represents a collection, and false otherwise.

5. level (type: number = 1)

This argument is used internally to control the recursion depth, and evaluate which events should be deactivated at any given time. The default value of 1 should work for 99% of use cases.