Storage Providers
Relevant source files
The following files were used as context for generating this wiki page:The Mastra storage system provides a flexible and extensible persistence layer for core framework components. Its primary purpose is to store and manage the state of workflows, conversation threads, messages, evaluation data, and other resources. This allows for features like long-term conversation memory and the ability to suspend and resume workflows.
The system is designed around a pluggable architecture, where different storage backends can be used by implementing a common interface. The core of this architecture is the MastraStorage abstract base class, which defines the contract for all storage providers. Mastra ships with several official providers for popular databases, and the architecture allows for the creation of custom providers.
Sources: packages/core/src/storage/README.md, packages/core/src/storage/base.ts
Core Architecture
The storage architecture is centered around the MastraStorage abstract base class. Any class that extends MastraStorage can be used as a storage provider within the Mastra framework. This design decouples the core logic of components like Memory and Workflows from the specific database implementation.
Sources: packages/core/src/storage/base.ts:24-29
MastraStorage Abstract Class
This class defines the standard interface for all storage operations. Each concrete implementation (e.g., for PostgreSQL or MongoDB) must provide the logic for these methods.
The following diagram illustrates the MastraStorage class and its key abstract methods.
Diagram showing the abstract MastraStorage class and its methods that must be implemented by concrete storage providers.
Key Methods
| Method | Description |
|---|---|
init() | Initializes the storage, typically by creating necessary tables or collections if they don't exist. |
createThread() | Creates a new conversation thread. |
getThreadById() | Retrieves a specific thread by its ID. |
updateThread() | Updates a thread's properties, such as its title or metadata. |
listThreads() | Lists all threads, with support for pagination. |
saveMessages() | Saves one or more messages to a thread. |
listMessages() | Retrieves messages from a thread, with support for pagination. |
persistWorkflowSnapshot() | Saves the state of a running workflow, enabling suspend/resume functionality. |
loadWorkflowSnapshot() | Loads a previously saved workflow state. |
saveResource() | Saves a generic resource, often used for working memory. |
getResourceById() | Retrieves a resource by its ID. |
updateResource() | Updates a resource's content or metadata. If the resource doesn't exist, it creates a new one. |
Sources: packages/core/src/storage/base.ts:34-128, stores/_test-utils/src/domains/memory/resources.ts:121-137
Data Models
The storage layer is responsible for persisting several key data entities. The relational schema is defined by the Drizzle ORM configurations in the SQL-based providers.
The diagram below shows the relationships between the primary data entities.
Entity-Relationship diagram for the core data models managed by the storage layer.
Sources: stores/libsql/src/storage/index.ts:32-110, packages/core/src/storage/base.ts:11-22
Table Schemas
threads
Stores metadata about conversation threads.
| Column | Type | Description |
|---|---|---|
id | string | Unique identifier for the thread (PK). |
resourceId | string | An optional external resource identifier. |
title | string | The title of the thread. |
metadata | json | Arbitrary JSON data associated with the thread. |
createdAt | datetime | Timestamp of creation. |
updatedAt | datetime | Timestamp of the last update. |
Sources: stores/libsql/src/storage/index.ts:46-56
messages
Stores individual messages within a thread.
| Column | Type | Description |
|---|---|---|
id | string | Unique identifier for the message (PK). |
threadId | string | Foreign key linking to the threads table. |
role | string | The role of the message author (e.g., 'user', 'assistant'). |
content | json | The content of the message. |
createdAt | datetime | Timestamp of creation. |
Sources: stores/libsql/src/storage/index.ts:58-67
workflow_snapshot
Stores the state of suspended workflows.
| Column | Type | Description |
|---|---|---|
workflowName | string | Name of the workflow (Composite PK). |
runId | string | Unique ID for the workflow run (Composite PK). |
snapshot | json | The complete state of the workflow. |
createdAt | datetime | Timestamp of creation. |
Sources: stores/libsql/src/storage/index.ts:32-44
resources
Stores generic resources, primarily used for an agent's working memory.
| Column | Type | Description |
|---|---|---|
id | string | Unique identifier for the resource (PK). |
workingMemory | text | The main content of the resource. |
metadata | json | Arbitrary JSON data. |
createdAt | datetime | Timestamp of creation. |
updatedAt | datetime | Timestamp of the last update. |
Sources: stores/libsql/src/storage/index.ts:83-92
Storage Provider Implementations
Mastra provides official support for several databases. Each provider extends MastraStorage and implements the data access logic for its specific backend.
LibSQL / SQLite (LibSQLStore)
The default storage provider, using LibSQL for both file-based and in-memory SQLite databases. It uses drizzle-orm/libsql for its implementation.
Configuration:
import { LibSQLStore } from '@mastra/libsql';
const storage = new LibSQLStore({
id: 'my-libsql-storage',
// For a file-based database
url: 'file:./mastra.db',
// For an in-memory database
// url: 'file::memory:',
});
Sources: stores/libsql/src/storage/index.ts:12-29, packages/core/src/storage/README.md:32-35
PostgreSQL (PgStore)
A provider for PostgreSQL databases, using the postgres library via drizzle-orm/postgres-js.
Configuration:
import { PgStore } from '@mastra/pg';
const storage = new PgStore({
id: 'my-pg-storage',
connectionString: process.env.DB_URL, // e.g., 'postgresql://user:pass@host:port/db'
});
Sources: stores/pg/src/storage/index.ts:12-28
MongoDB (MongoDBStore)
A provider for MongoDB, which adapts the relational schema to a NoSQL document model. It uses the official mongodb driver.
Configuration:
import { MongoDBStore } from '@mastra/mongodb';
const storage = new MongoDBStore({
id: 'my-mongodb-storage',
connectionString: process.env.MONGO_URI, // e.g., 'mongodb://...
databaseName: 'mastra-db',
});
Sources: stores/mongodb/src/storage/index.ts:12-32
Cloudflare D1 (D1Store)
A provider for Cloudflare's serverless D1 database, designed to be used within a Cloudflare Worker environment. It uses drizzle-orm/d1.
Configuration:
The D1Database instance is typically provided by the Cloudflare Workers runtime environment.
import { D1Store } from '@mastra/cloudflare-d1';
// 'env.DB' is provided by the Cloudflare Worker environment
const storage = new D1Store({
id: 'my-d1-storage',
db: env.DB,
});
Sources: stores/cloudflare-d1/src/storage/index.ts:12-28
Usage
A storage provider is configured on the main Mastra instance. If no provider is specified, LibSQLStore is used by default for components that require persistence, like Memory.
The following diagram shows the flow of configuring and using a storage provider.
This diagram shows how a storage provider is passed to the Mastra instance and subsequently used by other components like Memory and Workflows.
Example Configuration:
import { Mastra } from '@mastra/core/mastra';
import { PgStore } from '@mastra/pg';
import { Memory } from '@mastra/memory';
export const mastra = new Mastra({
// Configure the storage provider
storage: new PgStore({
id: 'mastra-storage',
connectionString: process.env.DB_URL,
}),
// Components like Memory will automatically use the configured storage
memory: new Memory({
options: {
lastMessages: 10,
semanticRecall: true,
},
}),
// ... other agents and workflows
});
Sources: packages/core/src/storage/README.md:28-39, CLAUDE.md:76-80
Summary
The Mastra storage layer is a vital component for enabling stateful and persistent AI applications. Its pluggable design, based on the MastraStorage abstract class, offers developers the flexibility to choose a database that fits their infrastructure while maintaining a consistent API for interacting with core Mastra entities. This system is fundamental to features like conversation history, working memory, and resumable workflows.