mastra-storage
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Configure the Mastra persistence layer: pick and wire a storage adapter (LibSQLStore, PostgresStore, MongoDBStore, and 20 more), set it on the Mastra instance or a single agent's Memory, split storage domains across databases with MastraCompositeStore, and keep tables bounded with retention and storage.prune(). Use this skill whenever the user needs memory, workflow snapshots, traces, scores, or schedules to survive restarts, moves from local SQLite to a production database, sees data vanish between runs, hits a record size limit, or wants observability in a separate database. Triggers on Mastra storage, storage adapter, @mastra/libsql, @mastra/pg, @mastra/mongodb, @mastra/clickhouse, @mastra/duckdb, MastraCompositeStore, storage domains, MemoryLibSQL, WorkflowsPG, retention, prune, disableInit, storage.init, getStore, mastra.shutdown. Also triggers when choosing a database for a Mastra deployment.
- Reads credentials
- Installs packages
In this repository46 other files it gives its agents.
- mastra-a2a-acpSkill
- mastra-agent-approval-hitlSkill
- mastra-agentic-uiSkill
- mastra-agent-skillsSkill
- mastra-agentsSkill
- mastra-authSkill
- mastra-browserSkill
- mastra-build-playbookSkill
- mastra-channelsSkill
- mastra-classifierSkill
- mastra-cli-apiSkill
- mastra-client-sdkSkill
- mastra-datasets-experimentsSkill
- mastra-deploymentSkill
- mastra-evalsSkill
- mastra-file-based-agentsSkill
- mastra-framework-integrationSkill
- mastra-guideSkill
- mastra-harnessSkill
- mastra-knowledgeSkill
- mastra-mcpSkill
- mastra-memorySkill
- mastra-migrationsSkill
- mastra-modelsSkill
- mastra-multi-agentSkill
- mastra-observabilitySkill
- mastra-observational-memorySkill
- mastra-platformSkill
- mastra-processors-guardrailsSkill
- mastra-project-setupSkill
- mastra-ragSkill
- mastra-schedulesSkill
- mastra-semantic-recallSkill
- mastra-serverSkill
- mastra-signalsSkill
- mastra-streamingSkill
- mastra-structured-outputSkill
- mastra-studio-editorSkill
- mastra-tool-integrationsSkill
- mastra-toolsSkill
- mastra-troubleshootingSkill
- mastra-vector-storesSkill
- mastra-voiceSkill
- mastra-workflowsSkill
- mastra-workflow-suspend-resumeSkill
- mastra-workspace-sandboxSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 0 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 104
Verify the API: If node_modules/@mastra/ exists, read node_modules/@mastra/core/dist/docs/ and the adapter's types.
- Very specific: line 110
Set retention: Add retention on the store, and call storage.prune() from your own scheduler.
- Very specific: line 155
Unbounded growth: No retention. Configure retention and schedule prune().
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Mastra Storage
Choose, configure, and operate the database that keeps Mastra state (memory, workflow snapshots, traces, scores, schedules) across restarts and processes.
When to use
- Adding persistence to a new Mastra project
- Moving from
:memory:or a local file to a managed production database - Picking an adapter for a platform (Cloudflare, AWS, Convex, Spanner, Oracle)
- Routing domains to different databases, such as observability to ClickHouse
- Giving one agent's memory its own database
- Bounding table growth with retention policies and
prune() - Running migrations separately from app start (
disableInit) - Debugging lost data, "Memory requires a storage provider", or size limit errors
When NOT to use
- Memory behavior: history, working memory, threads (use
mastra-memory) - Vector databases for embeddings (use
mastra-vector-stores) - Semantic recall configuration (use
mastra-semantic-recall) - Trace exporters and sampling (use
mastra-observability) - Mastra Platform managed database (use
mastra-platform) - Workflow suspend and resume logic (use
mastra-workflow-suspend-resume) - Deploying the server that uses the database (use
mastra-deployment)
Required inputs
- Which features need persistence: memory, workflows, observability, evals, schedules, background tasks
- Environment: local dev, single server, multi-process, serverless, edge
- Existing databases the team already runs
- Expected trace volume (decides whether observability needs its own backend)
- Retention requirements for messages, threads, and spans
- Whether schema migrations may run at app start
The framework: domains, adapters, scope
Storage is split into domains. An adapter implements some of them.
| Domain | Holds | Fits |
memory | Threads, messages, resources, working memory | Transactional DB: libSQL, PostgreSQL, MongoDB |
workflows | Suspend and resume snapshots | Reliable persistent DB |
observability | Traces, spans, metrics, logs, feedback | OLAP: ClickHouse (prod), DuckDB (local) |
scores, datasets, experiments | Eval data | Any persistent DB |
schedules, backgroundTasks, threadState | Harness state | Adapter must implement the domain |
The full list (24 domains, including Editor domains such as agents and skills) is in references/api.md.
Two scopes:
// Instance level: shared by every agent, workflow, and feature
import { Mastra } from '@mastra/core'
import { LibSQLStore } from '@mastra/libsql'
export const mastra = new Mastra({
storage: new LibSQLStore({ id: 'mastra-storage', url: 'file:./mastra.db' }),
})// Agent level: overrides instance storage for this agent's memory only
import { Agent } from '@mastra/core/agent'
import { Memory } from '@mastra/memory'
import { PostgresStore } from '@mastra/pg'
export const supportAgent = new Agent({
id: 'support-agent',
name: 'Support agent',
instructions: 'Answer customer support questions.',
model: 'openai/gpt-6.1-sol',
memory: new Memory({
storage: new PostgresStore({
id: 'support-agent-storage',
connectionString: process.env.SUPPORT_AGENT_DATABASE_URL,
}),
}),
})Mastra creates tables on first use. Passing a store to new Mastra() calls init() for you; using a store directly needs await storage.init().
Workflow
- Verify the API. If
node_modules/@mastra/exists, readnode_modules/@mastra/core/dist/docs/and the adapter's types. Otherwise fetchhttps://mastra.ai/docs/storage.mdand the adapter page, for examplehttps://mastra.ai/integrations/databases/postgresql.md. Trust these over memory. - List the domains you need. Memory only? Add workflows, observability, schedules as features demand.
- Pick the adapter. Local:
LibSQLStorewithfile:. Production default:PostgresStore. Checkreferences/providers.mdfor domain gaps (many adapters lack observability) and features (observational memory and resource working memory need specific adapters). - Install and configure.
npm install @mastra/<adapter>@latest. Always passid. Read secrets from env vars. - Choose scope. Instance storage by default. Agent-level
Memory({ storage })only for an isolated memory boundary. - Split domains if needed.
MastraCompositeStorewithdefaultplusdomainsoverrides, for example observability to ClickHouse. - Set retention. Add
retentionon the store, and callstorage.prune()from your own scheduler. - Plan migrations. For CI-managed schemas set
disableInit: trueand runstorage.init()in a deploy step. - Shut down cleanly.
await mastra.shutdown()closes the composite's stores. Close stores you built only to supply a domain yourself. - Test persistence. Restart the process and confirm a thread and a suspended workflow are still there. Check Studio shows them.
Deep dive: composite storage
import { Mastra } from '@mastra/core'
import { MastraCompositeStore } from '@mastra/core/storage'
import { MemoryPG, WorkflowsPG, ScoresPG } from '@mastra/pg'
import { ObservabilityStorageClickhouseVNext } from '@mastra/clickhouse'
export const mastra = new Mastra({
storage: new MastraCompositeStore({
id: 'composite',
domains: {
memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
workflows: new WorkflowsPG({ connectionString: process.env.DATABASE_URL }),
scores: new ScoresPG({ connectionString: process.env.DATABASE_URL }),
observability: new ObservabilityStorageClickhouseVNext({
url: process.env.CLICKHOUSE_URL,
username: process.env.CLICKHOUSE_USERNAME,
password: process.env.CLICKHOUSE_PASSWORD,
}),
},
}),
})Resolution order per domain: explicit domains entry, then editor store (Editor domains), then default. A domain set to false is disabled and does not fall back. Mix only domain classes the packages export (MemoryLibSQL, WorkflowsPG, MemoryStorageMongoDB, and similar).
Failure patterns
- Data gone after restart.
url: ':memory:'or no storage at all. Usefile:./mastra.dblocally and a server database in production. - Studio and app show different data. Relative
file:paths resolve per process. Use an absolute path. - Missing
id. The memory quickstart says to addidtoLibSQLStore, and most adapters list it. Always set a uniqueid. - Tables never created. Store used directly without
init(), ordisableInit: truewith no migration step. Callawait storage.init(). - Traces missing in Studio. The adapter lacks the observability domain (Convex, DynamoDB, D1, Cloudflare KV/DO, LanceDB, Upstash). Route
observabilityto ClickHouse or DuckDB with composite storage. - Save fails on attachments. DynamoDB 400 KB items, Convex and D1 1 MiB records. Upload attachments in an input processor (see
mastra-memory). - Observational memory or resource working memory errors. Adapter does not support them. See the feature columns in
references/providers.md. - Unbounded growth. No retention. Configure
retentionand scheduleprune(); it never runs on its own. - Process hangs on exit. Open pools. Call
await mastra.shutdown()and close extra stores. - Local file DB in multi-instance production. Each instance writes its own file. Use a shared server database.
- Upstash bill spikes. Pay-as-you-go pricing with high command volume. Use a fixed plan.
Output format
A finished storage delivery contains:
- Adapter installed and configured in
src/mastra/index.tswith anidand env-based credentials - Domain coverage confirmed for every feature in use, or a composite store filling the gaps
.env.examplelisting connection variables- Retention config plus the scheduled job that calls
prune(), if data grows - Shutdown handling (
mastra.shutdown()) - Evidence: data survives a restart and appears in Studio
Reference files
references/api.md- Domains, composite options, store methods, retention config andprune().references/providers.md- Every database adapter: package, class, constructor shape, domain and feature notes.references/examples.md- Local libSQL, production PostgreSQL, composite with DuckDB and ClickHouse, retention job.references/checklist.md- Pre-production checklist for storage.
Live docs
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

