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

@nehorai/credits-firestore

v1.0.2

Published

Firestore implementation of the [`@nehorai/credits`](https://www.npmjs.com/package/@nehorai/credits) repository interface. Provides atomic credit operations using Firestore transactions.

Downloads

36

Readme

@nehorai/credits-firestore

Firestore implementation of the @nehorai/credits repository interface. Provides atomic credit operations using Firestore transactions.

Features

  • Atomic operations -- All credit mutations use Firestore transactions to prevent race conditions
  • Two-phase commit -- Reserve, commit, and release credits atomically
  • Monthly reset -- Optimistic-locking-based monthly credit reset
  • Subscription management -- Tier upgrades/downgrades with grace periods
  • Reservation cleanup -- Batch expiration of stale reservations
  • Journal and audit trail -- Full audit log of every credit change
  • Re-exports @nehorai/credits -- All core types and utilities available from this package

Installation

pnpm add @nehorai/credits-firestore firebase-admin

firebase-admin >= 12.0.0 is a peer dependency.

Quick Start

import {
  createFirestoreCreditRepository,
  CreditsService,
} from "@nehorai/credits-firestore";
import { getFirestore } from "firebase-admin/firestore";

// Create the repository with your Firestore instance
const db = getFirestore();
const repository = createFirestoreCreditRepository(db);

// Use with CreditsService from @nehorai/credits
const service = new CreditsService(repository);

// All operations are now backed by Firestore
const credits = await service.getOrCreateUserCredits("user-123");
const reservation = await service.reserveCredits("user-123", 10, "story_generation");

Firestore Collections

The repository uses subcollections under each user document:

| Path | Description | |------|-------------| | users/{userId}/credits/balance | User's credit balance document | | users/{userId}/transactions | Credit transaction history | | users/{userId}/reservations | In-flight credit reservations | | users/{userId}/credits/data/journal | Audit trail journal entries | | usage_logs | Global usage log collection |

API Reference

FirestoreCreditRepository

Implements ICreditRepository from @nehorai/credits. All methods use Firestore transactions for atomicity.

import { FirestoreCreditRepository } from "@nehorai/credits-firestore";

const repository = new FirestoreCreditRepository(db, {
  getMonthlyLimit: (tier) => tierLimits[tier], // Optional custom tier limits
});

createFirestoreCreditRepository(db, options?)

Factory function that returns an ICreditRepository instance:

import { createFirestoreCreditRepository } from "@nehorai/credits-firestore";

const repository = createFirestoreCreditRepository(db);

Options

| Option | Type | Description | |--------|------|-------------| | getMonthlyLimit | (tier: SubscriptionTier) => number | Custom monthly limit resolver for tier-based resets |

Module Functions

For advanced use cases, individual operation modules are exported:

import {
  BalanceOps,
  TransactionOps,
  ReservationAtomicOps,
  ReservationCrudOps,
  UsageLogOps,
  CleanupOps,
  SubscriptionOps,
  JournalOps,
} from "@nehorai/credits-firestore";

// Use module functions directly with a Firestore instance
const credits = await BalanceOps.getUserCredits(db, userId);

Utility Exports

import {
  // Collection helpers
  COLLECTIONS,
  BALANCE_DOC_ID,
  getUserCreditsCollection,
  getUserTransactionsCollection,
  getUserReservationsCollection,
  getUsageLogsCollection,
  // Conversion utilities
  toISOString,
  toDate,
  timestampToISO,
  timestampToDate,
  calculateCreditDeduction,
  // State machine
  isValidTransition,
  validateTransition,
  getValidNextStates,
  isTerminalState,
  // Validation
  validateBalanceUpdate,
  assertValidBalanceUpdate,
  // Path helpers
  getUserCreditsPath,
  getUserTransactionsPath,
  getUserReservationsPath,
  getUserJournalPath,
} from "@nehorai/credits-firestore";

Related Packages

| Package | Description | |---------|-------------| | @nehorai/credits | Core credit system (types, service, in-memory repository) | | @nehorai/credits-nextjs | Next.js adapter with NextAuth integration |

Repository

https://github.com/NehoraiHadad/nehorai-plugins

License

MIT