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

@caleblawson/cloudflare

v0.10.5-alpha.0

Published

Cloudflare provider for Mastra - includes db storage capabilities

Downloads

7

Readme

@mastra/cloudflare

Cloudflare KV store for Mastra, providing scalable and serverless storage for threads, messages, workflow snapshots, and evaluations. Supports both Cloudflare Workers KV Bindings and the REST API for flexible deployment in serverless and Node.js environments.

Installation

npm install @mastra/cloudflare

Prerequisites

  • Cloudflare account with KV namespaces set up
  • Node.js 16 or higher
  • (Optional) Cloudflare Worker for Workers API mode

Usage

import { CloudflareStore } from '@mastra/cloudflare';

// Using Workers Binding API
const store = new CloudflareStore({
  bindings: {
    threads: THREADS_KV_NAMESPACE,
    messages: MESSAGES_KV_NAMESPACE,
    workflow_snapshot: WORKFLOW_KV_NAMESPACE,
    evals: EVALS_KV_NAMESPACE,
    traces: TRACES_KV_NAMESPACE,
  },
  keyPrefix: 'myapp_', // Optional
});

// Or using REST API
const store = new CloudflareStore({
  accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
  apiToken: process.env.CLOUDFLARE_API_TOKEN!,
  namespacePrefix: 'myapp_', // Optional
});

// Save a thread
await store.saveThread({
  id: 'thread-123',
  resourceId: 'resource-456',
  title: 'My Thread',
  metadata: { key: 'value' },
  createdAt: new Date(),
  updatedAt: new Date(),
});

// Add messages
await store.saveMessages({
  messages: [
    {
      id: 'msg-1',
      threadId: 'thread-123',
      content: 'Hello Cloudflare!',
      role: 'user',
      createdAt: new Date(),
    },
  ],
});

// Query messages
const messages = await store.getMessages({ threadId: 'thread-123' });

Configuration

  • Workers API: Use the bindings option to pass KV namespaces directly (for Cloudflare Workers).
  • REST API: Use accountId, apiToken, and (optionally) namespacePrefix for server-side usage.
  • keyPrefix/namespacePrefix: Useful for isolating environments (e.g., dev/test/prod).

Features

Storage Features

  • Thread and message storage with JSON support
  • Rich metadata support (JSON-encoded)
  • Timestamp tracking for all records
  • Workflow snapshot persistence
  • Trace and evaluation storage
  • Sorted message order using simulated sorted sets
  • Supports both Cloudflare Workers KV Bindings and REST API
  • Automatic JSON serialization/deserialization for metadata and custom fields
  • Error handling and logging for all operations

Consistency & Performance

  • Eventually consistent (see Limitations)
  • Low-latency access via Workers Binding API
  • Scalable and serverless

Supported Methods

Thread Operations

  • saveThread(thread): Create or update a thread
  • getThreadById({ threadId }): Get a thread by ID
  • getThreadsByResourceId({ resourceId }): Fetch all threads associated with a resource.
  • updateThread({ id, title, metadata }): Update thread title and metadata
  • deleteThread({ threadId }): Delete a thread and its messages

Message Operations

  • saveMessages({ messages }): Save multiple messages
  • getMessages({ threadId, selectBy? }): Get messages for a thread with optional filtering (last N, includes, etc)

Workflow Operations

  • persistWorkflowSnapshot({ workflowName, runId, snapshot }): Save workflow state for a given workflow/run.
  • loadWorkflowSnapshot({ workflowName, runId }): Load ed workflow state.

Trace Operations

  • getTraces({ name?, scope?, page, perPage, attributes? }): Query trace records with optional filters and pagination.

Utility

  • clearTable({ tableName }): Remove all records from a logical table
  • batchInsert({ tableName, records }): Batch insert multiple records.
  • insert({ tableName, record }): Insert a single record into a table.

Data Types

  • text: String
  • timestamp: ISO8601 string (converted to/from Date)
  • uuid: String
  • jsonb: JSON-encoded object

All records are stored as JSON in KV, with automatic serialization/deserialization for metadata, arrays, and custom fields.

Configuration Reference

  • Workers Binding API: Use the bindings option to pass KV namespaces directly (for Cloudflare Workers).
  • REST API: Use accountId, apiToken, and (optionally) namespacePrefix for server-side usage.
  • keyPrefix/namespacePrefix: Useful for isolating environments (e.g., dev/test/prod).

Example:

const store = new CloudflareStore({
  bindings: { ... }, // for Workers
  keyPrefix: 'dev_',
});
// or
const store = new CloudflareStore({
  accountId: '...',
  apiToken: '...',
  namespacePrefix: 'prod_',
});

Table/Namespace Mapping

Each logical Mastra table (threads, messages, workflow_snapshot, evals, traces) maps to a separate KV namespace. Keys are structured as ${prefix}${tableName}:${primaryKey} or ${prefix}${tableName}:${threadId}:${messageId} for messages. The prefix is set by keyPrefix/namespacePrefix.

Limitations

  • Eventual Consistency: Cloudflare KV is eventually consistent; concurrent operations may not be immediately visible across all reads.
  • No Transactions: Atomic multi-key operations are not supported.
  • Rate Limits: Large objects or high-frequency updates may be subject to Cloudflare KV rate limits.
  • Query Limitations: No native querying; all filtering is done in-memory after fetching keys/records.
  • Best for: Use for serverless, low-latency, and moderate-volume workloads. For relational or strongly consistent needs, consider D1 or a SQL backend.

Cloudflare-Specific Notes

  • Workers Binding API is recommended for production Workers deployments (low-latency, no API token required at runtime).
  • REST API is ideal for server-side Node.js or test environments.
  • Ensure your KV namespaces are provisioned and accessible by your Worker or API token.
  • Namespaces and keys are automatically created as needed.

Cleanup/Disconnect

No explicit cleanup or disconnect is required; Cloudflare KV is fully managed.