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

@askelephant/mastra-firestore

v0.1.0

Published

Mastra storage plugin for Firestore

Readme

Mastra Firestore Storage Plugin

npm version License: MIT

A Firestore storage adapter for the Mastra AI framework, providing persistent storage for agents, workflows, memory, and observability.

Features

  • 🔥 Firebase/Firestore Integration - Leverage Google Cloud Firestore for scalable, real-time storage
  • 🧠 Memory Management - Persistent storage for threads, messages, and agent memory
  • 🔄 Workflow State - Save and restore workflow execution states
  • 📊 Observability - Store and query AI traces and spans for monitoring
  • 📈 Scoring & Evals - Track evaluation results and scores
  • 🔍 Advanced Queries - Paginated queries with filtering and sorting
  • Batch Operations - Efficient batch inserts and updates

Installation

npm install @askelephant/mastra-firestore firebase-admin

Usage

Basic Setup

import { FirestoreStore } from "@askelephant/mastra-firestore";
import { initializeApp, cert } from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";
import { Mastra } from "@mastra/core";

// Initialize Firebase Admin
const app = initializeApp();

const db = getFirestore(app);

// Create the Firestore storage instance
const storage = new FirestoreStore({ db });

// Use with Mastra
const mastra = new Mastra({
  storage,
  // ... other configuration
});

Supported Features

The Firestore storage plugin supports all major Mastra storage features:

storage.supports = {
  selectByIncludeResourceScope: true,
  resourceWorkingMemory: true,
  hasColumn: true,
  createTable: true,
  deleteMessages: true,
  aiTracing: true,
  getScoresBySpan: true,
};

API Overview

Memory & Threads

// Save and retrieve threads
const thread = await storage.saveThread({
  thread: {
    id: "thread-123",
    resourceId: "user-456",
    title: "Customer Support Chat",
    metadata: { channel: "web" },
  },
});

// Get threads by resource with pagination
const { threads, ...pagination } =
  await storage.getThreadsByResourceIdPaginated({
    resourceId: "user-456",
    page: 1,
    perPage: 10,
  });

// Save messages to a thread
await storage.saveMessages({
  messages: [
    {
      id: "msg-1",
      threadId: "thread-123",
      role: "user",
      content: { content: "Hello!", metadata: {} },
      createdAt: new Date(),
    },
  ],
  format: "v2",
});

// Get messages with pagination
const { messages, ...paginationInfo } = await storage.getMessagesPaginated({
  threadId: "thread-123",
  pagination: { page: 1, perPage: 20 },
});

Workflows

// Persist workflow state
await storage.persistWorkflowSnapshot({
  workflowName: "customer-onboarding",
  runId: "run-123",
  resourceId: "user-456",
  snapshot: {
    status: "running",
    results: {},
    error: null,
    // ... workflow state
  },
});

// Load workflow state
const state = await storage.loadWorkflowSnapshot({
  workflowName: "customer-onboarding",
  runId: "run-123",
});

// Query workflow runs
const runs = await storage.getWorkflowRuns({
  workflowName: "customer-onboarding",
  fromDate: new Date("2024-01-01"),
  limit: 10,
});

Observability & Tracing

// Create AI span for observability
await storage.createAISpan({
  spanId: "span-123",
  traceId: "trace-456",
  name: "llm.completion",
  startTime: Date.now(),
  attributes: {
    model: "gpt-4",
    tokens: 150,
  },
});

// Get traces with pagination
const { spans, pagination } = await storage.getAITracesPaginated({
  filters: { model: "gpt-4" },
  pagination: { page: 1, perPage: 50 },
});

// Get specific trace
const trace = await storage.getAITrace("trace-456");

Scoring & Evaluation

// Save evaluation score
const { score } = await storage.saveScore({
  scorerId: "quality-scorer",
  runId: "run-123",
  entityType: "message",
  entityId: "msg-456",
  value: 0.95,
  metadata: { criteria: "helpfulness" },
});

// Get scores by scorer
const { scores, pagination } = await storage.getScoresByScorerId({
  scorerId: "quality-scorer",
  pagination: { page: 1, perPage: 10 },
});

// Get scores for specific span
const spanScores = await storage.getScoresBySpan({
  traceId: "trace-456",
  spanId: "span-123",
  pagination: { page: 1, perPage: 10 },
});

Generic Operations

// Create table/collection
await storage.createTable({
  tableName: "custom_data",
  schema: {
    id: { type: "string", primaryKey: true },
    data: { type: "json" },
  },
});

// Insert record
await storage.insert({
  tableName: "custom_data",
  record: { id: "123", data: { key: "value" } },
});

// Batch insert
await storage.batchInsert({
  tableName: "custom_data",
  records: [
    { id: "1", data: { foo: "bar" } },
    { id: "2", data: { baz: "qux" } },
  ],
});

// Load record
const record = await storage.load({
  tableName: "custom_data",
  keys: { id: "123" },
});

Firestore Collections

The plugin uses the following Firestore collections:

  • threads - Conversation threads
  • messages - Thread messages
  • resources - Resource metadata and working memory
  • workflows - Workflow execution states
  • traces - OpenTelemetry traces
  • ai_spans - AI operation spans for observability
  • scores - Evaluation scores
  • evals - Legacy evaluation results

Development

Prerequisites

  • Node.js >= 22
  • Firebase Admin SDK credentials
  • npm

Setup

# Clone the repository
git clone https://github.com/AskElephant/mastra-firestore.git
cd mastra-firestore

# Install dependencies
npm install

# Build the project
npm run build

# Run tests
npm test

# Lint code
npm run lint

Running Tests

Tests use Vitest and require a Firebase project configured:

# Create .env.test with your test Firebase credentials
npm test

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Links

Support

For issues and questions:

Author

kdawgwilk


Made with ❤️ by AskElephant for the Mastra community