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

firestore-index-gen

v0.3.1

Published

**CLI that generates a Firestore Index file from queries executed during a Firestore emulator session.**

Downloads

234

Readme

firestore-index-gen

CLI that generates a Firestore Index file from queries executed during a Firestore emulator session.

github license npm version

WARNING: The following CLI is built on undocumented functionality that can break without any advance notice so read below to understand what you are getting into.

Problem

Some Firebase Firestore queries require indexes to be defined before they can be used, but there is no easy way to figure out what those indexes should look like ahead of time.

Firestore's current solution is to fail any queries that require an index and to log a link that automatically creates said index.

The current solution has some drawbacks:

  • no easy way to generate the index entries prior to deploying to Firebase
  • requires indexes to be deployed one-by-one
  • easy to deploy queries without the index

There is an issue on Github, opened March 2020, to add support for indexes to the emulator. The issue is still open and unresolved.

"Undocumented Solution"

Within that issue there is a comment by one of the contributors that mentions an undocumented feature of the emulator that generates a report that includes a list of indexes that would have been required in order to execute the queries executed during a Firestore emulator session.

URL of the report: http://127.0.0.1:8080/emulator/v1/projects/{projectId}:indexUsage?database=projects/{projectId}/databases/(default)

The author of the comment also makes the following notes:

Note that the emulator doesn't know about your application's indexes, it simply attempts to create these from a set of theoretically optimal indexes. If your indexes don't match exactly it's still possible that all of your queries are covered by other indexes and will not fail in production.

Edit: The queries need to be executed to be stored at runtime and it must be a query that isn't automatically indexed (like a single-field query with no ordering). See https://firebase.google.com/docs/firestore/query-data/indexing for more info.

Disclaimer: this is unlaunched and the functionality might change in backward-incompatible ways.

Possible solution for generating the indexes with firestore-index-gen

This assumes that all of your queries are executed during your testing.

Generate the index by using a command like firebase --project=firestore-indexes emulators:exec --only auth,firestore 'yarn test /path/to/tests && fig --projectId firestore-indexes --overwrite'

Ensure that the projectId matched the ID used when initializing Firebase in your tests.

firebase --project=firestore-indexes emulators:exec --only auth,firestore <command> will launch the auth and firestore emulators and execute the provided command.

yarn test /path/to/tests should execute all tests that execute queries against Firestore. It is possible to abstract your queries so that they all live in a sub-directory (e.g. /util/queries/) to reduce the execution time.

Possible solution for checking the indexes during CI with firestore-index-gen

Run a command like firebase --project=firestore-indexes emulators:exec --only auth,firestore 'yarn test /path/to/tests && fig --projectId firestore-indexes --check'

This works just like the command above, except that instead of overwriting the content of firestore.indexes.json it compares the indexes from the emulator report and generates a diff. Also, the exit code is set to 1 (or fail) which would block your CI if there are differences.

CLI

Install

To install globally (can also be added as a dev dependency to your project):

npm install --global firestore-index-gen

Usage

Usage:
  fig --projectId <projectId> [--overwrite | --check]

Options:
  --help         Print this help message
  --projectId    The Firebase project ID used with the emulator
  --overwrite    Overwrite firestore.indexes.json with the new indexes
  --check        Check if firestore.indexes.json is up to date with the new indexes

check

Using fig --projectId <projectId> --check within your project, the CLI will printout a diff between the report and your current firestore.indexes.json and exit with 1. If there are no differences the CLI will just print a success message and exit with 0. This can be used as part of your CI to ensure no code makes it to production without the proper indexes.

overwrite

Using fig --projectId <projectId> --overwrite will overwrite your current firestore.indexes.json with the indexes reported by the emulator.

v.0