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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@flarequery/firebase

v0.1.3

Published

**FlareQuery solves the overfetching and N+1 problem in Firestore.** Declare exactly what you need — fields, filters, relations. FlareQuery builds the execution plan, resolves relations in parallel, and returns only what was asked for. Nothing more.

Readme

FlareQuery

FlareQuery solves the overfetching and N+1 problem in Firestore.
Declare exactly what you need — fields, filters, relations.
FlareQuery builds the execution plan, resolves relations in parallel, and returns only what was asked for. Nothing more.

npm license TypeScript


The Problem

Firestore charges per document read. Every .get() returns the entire document — all fields — whether you needed 2 or 20. And the moment you need related data, you're writing sequential fetch chains — the classic N+1 problem.

// Overfetching — all fields returned, 1 used
const snap = await db
  .collection("posts")
  .where("status", "==", "published")
  .get();
const titles = snap.docs.map((d) => d.data().title);

// N+1 — 1 query for posts + 1 query per author
const posts = await db.collection("posts").get();
for (const post of posts.docs) {
  const author = await db.collection("users").doc(post.data().authorId).get();
}

The Fix

// Only declared fields hit the wire
const result = await flareApp
  .collection("Post")
  .filter("status")
  .equals("published")
  .select("title", "slug", "author.name")
  .get(ctx);

// Relations resolved in parallel — not sequentially
const result = await flareApp
  .collection("Post")
  .doc("post_abc")
  .select("title", "body", "author.name", "tags.label")
  .get(ctx);

// result.data →
// {
//   title: "Hello World",
//   body: "...",
//   author: { name: "Alice" },   ← resolved in parallel
//   tags: [{ label: "dev" }]     ← batch fetched via getAll
// }

No N+1. No overfetching. One declaration.


Install

yarn add @flarequery/firebase

Node 18+, firebase-admin ≥11, firebase-functions ≥4


Setup

// lib/flarequery.ts
import { createServerlessApp, one, many } from "@flarequery/firebase";
import { adminDb, adminAuth } from "@/lib/firebase-admin";

export const flareApp = createServerlessApp({
  firestore: adminDb,
  auth: adminAuth,
});

flareApp.model("Post", {
  source: { path: "posts" },
  fields: {
    title: "string",
    body: "string",
    status: "string",
    author: one("User", { from: "authorId" }),
    tags: many("Tag", { from: "tagIds" }),
  },
  auth: (ctx) => ctx.userId !== null,
});

flareApp.model("User", {
  source: { path: "users" },
  fields: { name: "string", email: "string" },
});

flareApp.model("Tag", {
  source: { path: "tags" },
  fields: { label: "string" },
});

Three Ways to Query

1. Document fetch with relations — solves N+1

const result = await flareApp
  .collection("Post")
  .doc("post_abc")
  .select("title", "body", "author.name", "author.email", "tags.label")
  .get(ctx);

FlareQuery builds an execution plan, resolves author and tags in parallel using Promise.all and getAll batching. One round trip per relation level — not one per document.

2. Collection query with filters — solves overfetching

const result = await flareApp
  .collection("Post")
  .filter("status")
  .equals("published")
  .filter("featured")
  .equals(true)
  .select("title", "slug", "author.name")
  .orderBy("createdAt", "desc")
  .limit(20)
  .get(ctx);

Firestore receives only the fields you declared. Nothing else is read off disk.

3. Aggregation — unique field values with field mask

// Fetch only the "category" field — not full documents
const snapshot = await adminDb
  .collection("posts")
  .where("status", "==", "published")
  .select("category")
  .get();

const categories = [
  ...new Set(snapshot.docs.map((d) => d.get("category")).filter(Boolean)),
].sort();

Filter Operators

.filter("status").equals("published")        // ==
.filter("status").not("archived")            // !=
.filter("views").gt(100)                     // >
.filter("views").gte(100)                    // >=
.filter("views").lt(1000)                    // <
.filter("views").lte(1000)                   // <=
.filter("status").in(["draft", "published"]) // in
.filter("tags").contains("firebase")         // array-contains

Chain as many filters as needed — all ANDed by Firestore:

flareApp
  .collection("Post")
  .filter("status")
  .equals("published")
  .filter("views")
  .gte(100)
  .filter("featured")
  .equals(true)
  .select("title", "author.name")
  .get(ctx);

Field Types

| Type | Usage | | ------------- | ------------------------------------------------- | | "string" | String field | | "number" | Number field | | "boolean" | Boolean field | | "timestamp" | Firestore Timestamp | | one(...) | Single related doc resolved via foreign key | | many(...) | Multiple related docs via ID array, batch fetched |


Auth

Auth rules run before any Firestore read. Unauthorized access throws a PlanError — nothing hits the database.

import { extractContext } from "@flarequery/firebase";

const ctx = await extractContext(req.headers.get("authorization"), auth);
// { userId: string | null, token: DecodedIdToken | null }
flareApp.model("Post", {
  source: { path: "posts" },
  fields: { title: "string", body: "string" },
  auth: (ctx) => ctx.userId !== null,
});

Cloud Function

export const query = createOnRequest(flareApp, getAuth(), { cors: true });
POST /query
Authorization: Bearer <token>

{ "model": "Post", "id": "post_abc", "select": ["title", "author.name", "tags.label"] }
{
  "data": {
    "title": "Hello World",
    "author": { "name": "Alice" },
    "tags": [{ "label": "dev" }, { "label": "firebase" }]
  }
}

License

MIT © Gaurav Paliwal