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

@ridedott/firestore-extensions

v6.19.4

Published

A collection of Firestore utilities.

Downloads

960

Readme

firestore-extensions

Coverage Status GitHub Actions Status GitHub Actions Status code style: prettier Commitizen friendly

A collection of Firestore utilities.

Quick start

Installation

npm install @ridedott/firestore-extensions

Usage

Firestore ID to UUID converter

An extension provides methods to convert Firestore IDs to UUID v4 and back.

const { idToUuid, uuidToId } = require('@ridedott/firestore-extensions');
const uuid = idToUuid('74NS2s2OIUH5KZRLFto4'); // ef8352da-cd8e-4214-87e4-a6512c5b68e0
const firestoreId = uuidToId('ef8352da-cd8e-4214-87e4-a6512c5b68e0'); // 74NS2s2OIUH5KZRLFto4

Subscriptions

An extension that maintains an in memory cache of a Firestore collection's data with a backing subscription ensuring updates as they occur in the database.

Supports pausing and resuming subscriptions, which can optimize database usage. These operations are handled automatically, based on attached event listeners and pending promises. If there are no attached event listeners and no pending promises, the subscription is paused.

Collections expose methods to learn about the current state and better optimize usage, such as isActive and statistics.

Listen to data changes
import { subscriptions } from '@ridedott/firestore-extensions';

const repository = new subscriptions.Repository({ projectId: 'my-project' });

const myCollectionSubscription = repository.makeCollectionSubscription(
  { structuredQuery: { from: [{ collectionId: 'my-collection' }] } },
  (document) => document,
);

myCollectionSubscription.on('documentAdded', (document): void => {
  // Added document data is available here.
});
Wait for data synchronization
import { subscriptions } from '@ridedott/firestore-extensions';

const repository = new subscriptions.Repository({ projectId: 'my-project' });

const myCollectionSubscription = repository.makeCollectionSubscription(
  { structuredQuery: { from: [{ collectionId: 'my-collection' }] } },
  (document) => document,
);

await myCollectionSubscription.synchronize();

// Contains current documents.
myCollectionSubscription.data();
Use filters
import { subscriptions } from '@ridedott/firestore-extensions';

const repository = new subscriptions.Repository({ projectId: 'my-project' });

const myCollectionSubscription = repository.makeCollectionSubscription(
  {
    structuredQuery: {
      from: [{ collectionId: 'my-collection' }],
      where: {
        fieldFilter: {
          field: { fieldPath: 'my-field-path' },
          op: 'EQUAL',
          value: { booleanValue: true },
        },
      },
    },
    },
  },
  (document) => document,
);
Use projections (field masks)
import { subscriptions } from '@ridedott/firestore-extensions';

const repository = new subscriptions.Repository({ projectId: 'my-project' });

const myCollectionSubscription = repository.makeCollectionSubscription(
  {
    structuredQuery: {
      from: [{ collectionId: 'my-collection' }],
      select: {
        fields: [{ fieldPath: 'flat' }, { fieldPath: 'nested.field' }],
      },
    },
  },
  (document) => document,
);
Access statistics
import { subscriptions } from '@ridedott/firestore-extensions';

const repository = new subscriptions.Repository({ projectId: 'my-project' });

const myCollectionSubscription = repository.makeCollectionSubscription(
  { structuredQuery: { from: [{ collectionId: 'my-collection' }] } },
  (document) => document,
);

const statistics = myCollectionSubscription.statistics();
Access usage metrics

Firestore usage metrics are collected per subscription. Metrics are reset after each call to the metrics() method. Returned object has to be logged to be used as a source for log-based metrics.

import { subscriptions } from '@ridedott/firestore-extensions';

const repository = new subscriptions.Repository({ projectId: 'my-project' });

const myCollectionSubscription = repository.makeCollectionSubscription(
  { structuredQuery: { from: [{ collectionId: 'my-collection' }] } },
  (document) => document,
);

await myCollectionSubscription.synchronize();

const metrics = myCollectionSubscription.metrics();

logInfo(`Subscription synchronized.`, {
  metrics: {
    myCollectionSubscription: myCollectionSubscription.metrics(),
  },
});
Canonical representation

For best performance, subscriptions default to use the native (API) representation of documents. This is a recommended way to interact with the library.

For convenience purposes, especially when migrating existing code, conversion helpers are provided.

import { subscriptions } from '@ridedott/firestore-extensions';

const repository = new subscriptions.Repository({ projectId: 'my-project' });

const myCollectionSubscription = repository.makeCollectionSubscription(
  { structuredQuery: { from: [{ collectionId: 'my-collection' }] } },
  (native: subscriptions.types.ToNativeDocument<MyType>): MyType =>
    subscriptions.converters.toCanonical<MyType>({
      mapValue: { fields: native.fields },
      valueType: 'mapValue',
    }),
);
Migrating from Firestore SDK

It is possible to convert Firestore SDK references to native query format with the use of private APIs. This is not recommended, however it can be useful to run locally when migrating.

import { Firestore } from '@google-cloud/firestore';
import { google } from '@google-cloud/firestore/types/protos/firestore_v1_proto_api';
import { subscriptions } from '@ridedott/firestore-extensions';

const firestore = new Firestore();
const reference = (
  firestore.collection('my-collection') as unknown as {
    toProto: () => google.firestore.v1.IRunQueryRequest;
  }
).toProto();

// Log the structured query.
console.log(reference.structuredQuery);

// Use directly (not recommended in production environments).
const repository = new subscriptions.Repository({ projectId: 'my-project' });
const subscription = repository.makeCollectionSubscription(
  reference,
  (document) => document,
);

Getting started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See usage notes on how to consume this package in your project.

Prerequisites

Minimal requirements to set up the project:

  • Node.js v16, installation instructions can be found on the official website, a recommended installation option is to use Node Version Manager. It can be installed in a few commands.
  • A package manager npm. All instructions in the documentation will follow the npm syntax.
  • Optionally a Git client.

Installing

Start by cloning the repository:

git clone [email protected]:ridedott/firestore-extensions.git

In case you don't have a git client, you can get the latest version directly by using this link and extracting the downloaded archive.

Go the the right directory and install dependencies:

cd ./firestore-extensions
npm install

That's it! You can now go to the next step.

Tests

Formatting

This project uses Prettier to automate formatting. All supported files are being reformatted in a pre-commit hook. You can also use one of the two scripts to validate and optionally fix all of the files:

npm run format
npm run format:fix

Linting

This project uses ESLint to enable static analysis. TypeScript files are linted using a custom configuration. You can use one of the following scripts to validate and optionally fix all of the files:

npm run lint
npm run lint:fix

Coverage

Coveralls.io

Publishing

Publishing is handled in an automated way and must not be performed manually.

Each commit to the master branch is automatically deployed to the NPM registry with a version specified in package.json. All other commits are published as pre-releases.

Contributing

See CONTRIBUTING.md.

Built with

Runtime libraries

Automation

Source

Delivery

Versioning

This project adheres to Semantic Versioning v2.