packages/core/src/storage/README.md
The storage package provides a flexible and extensible storage system for Mastra, implementing persistent storage capabilities for workflows, threads, messages, and evaluation data.
Base Storage Class (MastraStorage)
LibSQL Implementation (DefaultStorage)
The storage system manages four primary tables:
Workflow Snapshots (workflow_snapshot)
Messages (messages)
Threads (threads)
Evaluations (evals)
When using Memory, DefaultStorage is used automatically if no storage is provided:
import { Memory } from '@mastra/memory';
const memory = new Memory({
options: {
lastMessages: 10,
semanticRecall: true,
},
});
For direct storage usage or custom configurations:
import { DefaultStorage } from '@mastra/core/storage';
const storage = new DefaultStorage({
name: 'my-storage',
config: {
url: 'file:my-database.db', // or 'file::memory:' for in-memory
},
});
// Storage will auto-initialize tables on first use
await storage.init();
const memoryStorage = await storage.getStore('memory');
// Create a new thread
const thread = await memoryStorage.createThread({
resourceId: 'resource-123',
title: 'My Thread',
metadata: { key: 'value' },
});
// Get thread by ID
const retrievedThread = await memoryStorage.getThreadById({
threadId: thread.id,
});
// Update thread
await memoryStorage.updateThread({
id: thread.id,
title: 'Updated Title',
metadata: { newKey: 'newValue' },
});
const memoryStorage = await storage.getStore('memory');
// Save messages
await memoryStorage.saveMessages({
messages: [
{
id: 'msg-1',
threadId: thread.id,
role: 'user',
content: [{ type: 'text', text: 'Hello' }],
createdAt: new Date(),
},
],
});
// Get thread messages with pagination
const result = await memoryStorage.listMessages({
threadId: thread.id,
page: 0,
perPage: 50,
});
console.log(result.messages); // MastraDBMessage[]
console.log(result.total); // Total count
console.log(result.hasMore); // Whether more pages exist
const workflowsStorage = await storage.getStore('workflows');
// Save workflow state
await workflowsStorage.persistWorkflowSnapshot({
workflowName: 'my-workflow',
runId: 'run-123',
snapshot: {
value: { currentState: 'running' },
context: {
stepResults: {},
attempts: {},
triggerData: {},
},
activePaths: [],
runId: 'run-123',
timestamp: Date.now(),
},
});
// Load workflow state
const snapshot = await workflowsStorage.loadWorkflowSnapshot({
workflowName: 'my-workflow',
runId: 'run-123',
});