@mastra/mongodb 1.12.1 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +52 -0
- package/dist/docs/SKILL.md +4 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-memory-semantic-recall.md +401 -0
- package/dist/docs/references/docs-memory-working-memory.md +3 -3
- package/dist/docs/references/docs-rag-vector-databases.md +7 -2
- package/dist/docs/references/docs-storage-overview.md +214 -0
- package/dist/docs/references/reference-storage-composite.md +337 -0
- package/dist/docs/references/reference-storage-mongodb.md +46 -46
- package/dist/docs/references/reference-vectors-mongodb.md +93 -4
- package/dist/index.cjs +68 -66
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +69 -67
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/datasets/index.d.ts.map +1 -1
- package/package.json +10 -10
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Storage overview
|
|
4
|
+
|
|
5
|
+
Storage is the persistence layer for the Mastra runtime. It keeps memory, workflow state, observability data, eval results, schedules, and long-running agent state available after a process restarts.
|
|
6
|
+
|
|
7
|
+
Storage powers:
|
|
8
|
+
|
|
9
|
+
- [Memory](https://mastra.ai/docs/memory/overview): Message history, threads, resources, and working memory.
|
|
10
|
+
- [Workflows](https://mastra.ai/docs/workflows/overview): Durable snapshots for suspended and resumed workflow runs.
|
|
11
|
+
- [Observability](https://mastra.ai/docs/observability/overview): Traces, spans, metrics, logs, and feedback.
|
|
12
|
+
- [Evals](https://mastra.ai/docs/evals/overview): Scores, datasets, experiments, and evaluation results.
|
|
13
|
+
- [Long-running agents](https://mastra.ai/docs/long-running-agents/durable-agents): Background tasks, schedules, goals, and thread state.
|
|
14
|
+
|
|
15
|
+
## When to configure storage
|
|
16
|
+
|
|
17
|
+
Configure a persistent storage adapter when state must survive restarts, be shared across processes, or be visible in Studio across sessions. The default in-memory store is useful for tests and short local experiments, but it loses data when the process exits.
|
|
18
|
+
|
|
19
|
+
Use storage when your application needs any of these behaviors:
|
|
20
|
+
|
|
21
|
+
- Agents remember past messages or user facts.
|
|
22
|
+
- Workflows suspend and resume after a restart.
|
|
23
|
+
- Traces, metrics, logs, scores, or feedback stay available for analysis.
|
|
24
|
+
- Schedules and background tasks continue across deployments.
|
|
25
|
+
- Multiple runtime processes read and write the same state.
|
|
26
|
+
|
|
27
|
+
## How storage works
|
|
28
|
+
|
|
29
|
+
Mastra storage is organized into **domains**. A domain owns one type of runtime data, and a storage adapter implements one or more domains.
|
|
30
|
+
|
|
31
|
+
| Domain | What it stores |
|
|
32
|
+
| ----------------- | --------------------------------------------------------------------------- |
|
|
33
|
+
| `memory` | Threads, messages, resources, working memory, and other agent memory state. |
|
|
34
|
+
| `workflows` | Workflow snapshots used to suspend and resume runs. |
|
|
35
|
+
| `observability` | Traces, spans, metrics, logs, and feedback. |
|
|
36
|
+
| `scores` | Eval score records. |
|
|
37
|
+
| `datasets` | Dataset records and dataset items used by evals and experiments. |
|
|
38
|
+
| `experiments` | Experiment runs and per-item experiment results. |
|
|
39
|
+
| `backgroundTasks` | Background task records and execution state. |
|
|
40
|
+
| `schedules` | Schedule definitions and trigger history. |
|
|
41
|
+
| `threadState` | Durable task, goal, and thread state. |
|
|
42
|
+
|
|
43
|
+
Adapter support varies by domain. For the full domain list and built-in schemas, see the [storage overview reference](https://mastra.ai/reference/storage/overview).
|
|
44
|
+
|
|
45
|
+
## Choose a backend by data shape
|
|
46
|
+
|
|
47
|
+
Different domains write and query different kinds of data. Pick a backend based on the domain's access pattern:
|
|
48
|
+
|
|
49
|
+
- `memory`: Reads and writes rows during every remembered agent call. Use a transactional database such as libSQL, PostgreSQL, or MongoDB.
|
|
50
|
+
- `observability`: Writes high-volume telemetry and often queries aggregations. Use a dedicated observability store or an online analytical processing (OLAP) backend such as ClickHouse or DuckDB.
|
|
51
|
+
- `workflows`: Stores durable snapshots that must be available when a run resumes. Use a reliable persistent database.
|
|
52
|
+
- `scores`, `datasets`, and `experiments`: Store lower-frequency evaluation data that's often read later for analysis.
|
|
53
|
+
- `schedules`: Stores schedule definitions and fire history. Use an adapter that implements the schedules domain.
|
|
54
|
+
|
|
55
|
+
When domains have different operational needs, use [composite storage](#composite-storage) to route each domain to the right backend.
|
|
56
|
+
|
|
57
|
+
## Get started locally
|
|
58
|
+
|
|
59
|
+
For local development, use libSQL with a file-backed database. It doesn't require a separate database server and persists state between restarts.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { Mastra } from '@mastra/core'
|
|
63
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
64
|
+
|
|
65
|
+
export const mastra = new Mastra({
|
|
66
|
+
storage: new LibSQLStore({
|
|
67
|
+
id: 'mastra-storage',
|
|
68
|
+
url: 'file:./mastra.db',
|
|
69
|
+
}),
|
|
70
|
+
})
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
> **Sharing the database with Studio:** When running `mastra dev` alongside your application, use an absolute path so both processes access the same database:
|
|
74
|
+
>
|
|
75
|
+
> ```typescript
|
|
76
|
+
> url: 'file:/absolute/path/to/your/project/mastra.db'
|
|
77
|
+
> ```
|
|
78
|
+
>
|
|
79
|
+
> Relative paths like `file:./mastra.db` resolve based on each process's working directory, which may differ.
|
|
80
|
+
|
|
81
|
+
Mastra initializes the required storage structures on first use.
|
|
82
|
+
|
|
83
|
+
## Configure for production
|
|
84
|
+
|
|
85
|
+
For production, use a persistent managed database. PostgreSQL is a good default for most teams because it works well for transactional runtime state and is widely available as a managed service.
|
|
86
|
+
|
|
87
|
+
Production guidance:
|
|
88
|
+
|
|
89
|
+
- Use a managed database with backups, monitoring, and connection pooling.
|
|
90
|
+
- Keep local file databases such as `file:./mastra.db` out of multi-process production deployments.
|
|
91
|
+
- Route high-volume domains, especially `observability`, to a dedicated backend with [composite storage](#composite-storage).
|
|
92
|
+
- Configure [retention](https://mastra.ai/reference/storage/retention) policies on the storage adapter or composite store, then call `storage.prune()` from a scheduler or maintenance job.
|
|
93
|
+
- Choose providers based on the domains your application uses. For example, schedules require an adapter that implements the `schedules` domain.
|
|
94
|
+
|
|
95
|
+
## Configuration scope
|
|
96
|
+
|
|
97
|
+
Storage can be configured at the Mastra instance level or at the agent level.
|
|
98
|
+
|
|
99
|
+
### Instance-level storage
|
|
100
|
+
|
|
101
|
+
Instance-level storage is shared by agents, workflows, observability, evals, schedules, and other runtime features registered on the same Mastra instance.
|
|
102
|
+
|
|
103
|
+
**PostgreSQL**:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { Mastra } from '@mastra/core'
|
|
107
|
+
import { PostgresStore } from '@mastra/pg'
|
|
108
|
+
|
|
109
|
+
export const mastra = new Mastra({
|
|
110
|
+
storage: new PostgresStore({
|
|
111
|
+
id: 'mastra-storage',
|
|
112
|
+
connectionString: process.env.DATABASE_URL,
|
|
113
|
+
}),
|
|
114
|
+
})
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**MongoDB**:
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import { Mastra } from '@mastra/core'
|
|
121
|
+
import { MongoDBStore } from '@mastra/mongodb'
|
|
122
|
+
|
|
123
|
+
export const mastra = new Mastra({
|
|
124
|
+
storage: new MongoDBStore({
|
|
125
|
+
id: 'mastra-storage',
|
|
126
|
+
uri: process.env.MONGODB_URI,
|
|
127
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
128
|
+
}),
|
|
129
|
+
})
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Use instance-level storage when most runtime domains can share the same database.
|
|
133
|
+
|
|
134
|
+
### Agent-level storage
|
|
135
|
+
|
|
136
|
+
Agent-level storage is configured on a `Memory` instance. It overrides instance-level storage for that agent's memory data only.
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { Agent } from '@mastra/core/agent'
|
|
140
|
+
import { Memory } from '@mastra/memory'
|
|
141
|
+
import { PostgresStore } from '@mastra/pg'
|
|
142
|
+
|
|
143
|
+
export const supportAgent = new Agent({
|
|
144
|
+
id: 'support-agent',
|
|
145
|
+
name: 'Support agent',
|
|
146
|
+
instructions: 'Answer customer support questions.',
|
|
147
|
+
model: 'openai/gpt-5.5',
|
|
148
|
+
memory: new Memory({
|
|
149
|
+
storage: new PostgresStore({
|
|
150
|
+
id: 'support-agent-storage',
|
|
151
|
+
connectionString: process.env.SUPPORT_AGENT_DATABASE_URL,
|
|
152
|
+
}),
|
|
153
|
+
}),
|
|
154
|
+
})
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Use agent-level storage when an agent needs an isolated memory boundary or a different memory backend.
|
|
158
|
+
|
|
159
|
+
## Composite storage
|
|
160
|
+
|
|
161
|
+
[`MastraCompositeStore`](https://mastra.ai/reference/storage/composite) routes domains to different backends. Use it when one database isn't the right fit for every domain.
|
|
162
|
+
|
|
163
|
+
The following example uses libSQL as the default store and routes workflow state to PostgreSQL:
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
import { Mastra } from '@mastra/core'
|
|
167
|
+
import { MastraCompositeStore } from '@mastra/core/storage'
|
|
168
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
169
|
+
import { WorkflowsPG } from '@mastra/pg'
|
|
170
|
+
|
|
171
|
+
export const mastra = new Mastra({
|
|
172
|
+
storage: new MastraCompositeStore({
|
|
173
|
+
id: 'composite-storage',
|
|
174
|
+
default: new LibSQLStore({
|
|
175
|
+
id: 'default-storage',
|
|
176
|
+
url: 'file:./mastra.db',
|
|
177
|
+
}),
|
|
178
|
+
domains: {
|
|
179
|
+
workflows: new WorkflowsPG({
|
|
180
|
+
connectionString: process.env.DATABASE_URL,
|
|
181
|
+
}),
|
|
182
|
+
},
|
|
183
|
+
}),
|
|
184
|
+
})
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
You can also route `observability` to a dedicated analytics backend. See [observability storage](https://mastra.ai/docs/observability/storage) for an observability-specific example.
|
|
188
|
+
|
|
189
|
+
## Supported providers
|
|
190
|
+
|
|
191
|
+
Each provider page includes installation instructions, configuration parameters, and usage examples:
|
|
192
|
+
|
|
193
|
+
- [libSQL](https://mastra.ai/reference/storage/libsql)
|
|
194
|
+
- [PostgreSQL](https://mastra.ai/reference/storage/postgresql)
|
|
195
|
+
- [MongoDB](https://mastra.ai/reference/storage/mongodb)
|
|
196
|
+
- [Upstash](https://mastra.ai/reference/storage/upstash)
|
|
197
|
+
- [Redis](https://mastra.ai/reference/storage/redis)
|
|
198
|
+
- [Cloudflare D1](https://mastra.ai/reference/storage/cloudflare-d1)
|
|
199
|
+
- [Cloudflare KV & Durable Objects](https://mastra.ai/reference/storage/cloudflare)
|
|
200
|
+
- [Convex](https://mastra.ai/reference/storage/convex)
|
|
201
|
+
- [DynamoDB](https://mastra.ai/reference/storage/dynamodb)
|
|
202
|
+
- [LanceDB](https://mastra.ai/reference/storage/lance)
|
|
203
|
+
- [Microsoft SQL Server](https://mastra.ai/reference/storage/mssql)
|
|
204
|
+
- [Google Cloud Spanner](https://mastra.ai/reference/storage/spanner)
|
|
205
|
+
|
|
206
|
+
> **Tip:** libSQL is the fastest path for local development because it doesn't require running a separate database server.
|
|
207
|
+
|
|
208
|
+
## Next steps
|
|
209
|
+
|
|
210
|
+
- [Composite storage](https://mastra.ai/reference/storage/composite)
|
|
211
|
+
- [Storage retention](https://mastra.ai/reference/storage/retention)
|
|
212
|
+
- [Storage schemas](https://mastra.ai/reference/storage/overview)
|
|
213
|
+
- [Memory](https://mastra.ai/docs/memory/overview)
|
|
214
|
+
- [Observability storage](https://mastra.ai/docs/observability/storage)
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Composite storage
|
|
4
|
+
|
|
5
|
+
`MastraCompositeStore` can compose storage domains from different providers. Use it when you need different databases for different purposes. For example, use LibSQL for memory and PostgreSQL for workflows.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
`MastraCompositeStore` is included in `@mastra/core`:
|
|
10
|
+
|
|
11
|
+
**npm**:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @mastra/core@latest
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
**pnpm**:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm add @mastra/core@latest
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Yarn**:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
yarn add @mastra/core@latest
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Bun**:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
bun add @mastra/core@latest
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
You'll also need to install the storage providers you want to compose:
|
|
36
|
+
|
|
37
|
+
**npm**:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install @mastra/pg@latest @mastra/libsql@latest @mastra/mongodb@latest
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**pnpm**:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pnpm add @mastra/pg@latest @mastra/libsql@latest @mastra/mongodb@latest
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
**Yarn**:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
yarn add @mastra/pg@latest @mastra/libsql@latest @mastra/mongodb@latest
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**Bun**:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
bun add @mastra/pg@latest @mastra/libsql@latest @mastra/mongodb@latest
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Storage domains
|
|
62
|
+
|
|
63
|
+
Mastra organizes storage into domains, each handling a specific type of data. Each domain can be backed by a different storage adapter, and domain classes are exported from each storage package.
|
|
64
|
+
|
|
65
|
+
| Domain | Description |
|
|
66
|
+
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
67
|
+
| `memory` | Conversation persistence for agents. Stores threads (conversation sessions), messages, resources (user identities), and working memory (persistent context across conversations). |
|
|
68
|
+
| `workflows` | Workflow execution state. When workflows suspend for human input, external events, or scheduled resumption, their state is persisted here to enable resumption after server restarts. |
|
|
69
|
+
| `scores` | Evaluation results from Mastra's evals system. Scores and metrics are persisted here for analysis and comparison over time. |
|
|
70
|
+
| `observability` | Telemetry data including traces and spans. Agent interactions, tool calls, and LLM requests generate spans collected into traces for debugging and performance analysis. |
|
|
71
|
+
| `agents` | Agent configurations for stored agents. Enables agents to be defined and updated at runtime without code deployments. |
|
|
72
|
+
| `datasets` | Evaluation datasets used for experiment runs. Stores dataset definitions, schemas, and versioned items. |
|
|
73
|
+
| `experiments` | Experiment runs and per-item experiment results linked to datasets and targets. |
|
|
74
|
+
|
|
75
|
+
> **Note:** `MastraCompositeStore` accepts all of the domain keys above, but storage adapter support varies by package. You can mix adapters per domain, but only for domains implemented and exported by those adapters. For example, `memory: new MemoryLibSQL(...)` and `workflows: new WorkflowsPG(...)` is valid because both packages export those domain classes.
|
|
76
|
+
|
|
77
|
+
## Usage
|
|
78
|
+
|
|
79
|
+
### Basic composition
|
|
80
|
+
|
|
81
|
+
Import domain classes directly from each store package and compose them:
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { MastraCompositeStore } from '@mastra/core/storage'
|
|
85
|
+
import { WorkflowsPG, ScoresPG } from '@mastra/pg'
|
|
86
|
+
import { MemoryLibSQL } from '@mastra/libsql'
|
|
87
|
+
import { Mastra } from '@mastra/core'
|
|
88
|
+
|
|
89
|
+
export const mastra = new Mastra({
|
|
90
|
+
storage: new MastraCompositeStore({
|
|
91
|
+
id: 'composite',
|
|
92
|
+
domains: {
|
|
93
|
+
memory: new MemoryLibSQL({ url: 'file:./local.db' }),
|
|
94
|
+
workflows: new WorkflowsPG({ connectionString: process.env.DATABASE_URL }),
|
|
95
|
+
scores: new ScoresPG({ connectionString: process.env.DATABASE_URL }),
|
|
96
|
+
},
|
|
97
|
+
}),
|
|
98
|
+
})
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### With a default storage
|
|
102
|
+
|
|
103
|
+
Use `default` to specify a fallback storage, then override specific domains:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { MastraCompositeStore } from '@mastra/core/storage'
|
|
107
|
+
import { PostgresStore } from '@mastra/pg'
|
|
108
|
+
import { MemoryLibSQL } from '@mastra/libsql'
|
|
109
|
+
import { Mastra } from '@mastra/core'
|
|
110
|
+
|
|
111
|
+
const pgStore = new PostgresStore({
|
|
112
|
+
id: 'pg',
|
|
113
|
+
connectionString: process.env.DATABASE_URL,
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
export const mastra = new Mastra({
|
|
117
|
+
storage: new MastraCompositeStore({
|
|
118
|
+
id: 'composite',
|
|
119
|
+
default: pgStore,
|
|
120
|
+
domains: {
|
|
121
|
+
memory: new MemoryLibSQL({ url: 'file:./local.db' }),
|
|
122
|
+
},
|
|
123
|
+
}),
|
|
124
|
+
})
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Mixed backends
|
|
128
|
+
|
|
129
|
+
Use domain classes from each storage package to route different domains to different backends. The following example stores memory and workflow state in MongoDB, then routes observability to ClickHouse:
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import { Mastra } from '@mastra/core'
|
|
133
|
+
import { MastraCompositeStore } from '@mastra/core/storage'
|
|
134
|
+
import { ObservabilityStorageClickhouse } from '@mastra/clickhouse'
|
|
135
|
+
import { MemoryStorageMongoDB, WorkflowsStorageMongoDB } from '@mastra/mongodb'
|
|
136
|
+
|
|
137
|
+
export const mastra = new Mastra({
|
|
138
|
+
storage: new MastraCompositeStore({
|
|
139
|
+
id: 'composite',
|
|
140
|
+
domains: {
|
|
141
|
+
memory: new MemoryStorageMongoDB({
|
|
142
|
+
uri: process.env.MONGODB_URI,
|
|
143
|
+
dbName: 'mastra_memory',
|
|
144
|
+
}),
|
|
145
|
+
workflows: new WorkflowsStorageMongoDB({
|
|
146
|
+
uri: process.env.MONGODB_URI,
|
|
147
|
+
dbName: 'mastra_workflows',
|
|
148
|
+
}),
|
|
149
|
+
observability: new ObservabilityStorageClickhouse({
|
|
150
|
+
url: process.env.CLICKHOUSE_URL,
|
|
151
|
+
username: process.env.CLICKHOUSE_USERNAME,
|
|
152
|
+
password: process.env.CLICKHOUSE_PASSWORD,
|
|
153
|
+
}),
|
|
154
|
+
},
|
|
155
|
+
}),
|
|
156
|
+
})
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Disabling a domain
|
|
160
|
+
|
|
161
|
+
Set a domain to `false` to disable it. A disabled domain doesn't fall back to `default`, so data for that domain isn't persisted:
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
import { MastraCompositeStore } from '@mastra/core/storage'
|
|
165
|
+
import { PostgresStore } from '@mastra/pg'
|
|
166
|
+
import { Mastra } from '@mastra/core'
|
|
167
|
+
|
|
168
|
+
const pgStore = new PostgresStore({
|
|
169
|
+
id: 'pg',
|
|
170
|
+
connectionString: process.env.DATABASE_URL,
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
export const mastra = new Mastra({
|
|
174
|
+
storage: new MastraCompositeStore({
|
|
175
|
+
id: 'composite',
|
|
176
|
+
default: pgStore,
|
|
177
|
+
domains: {
|
|
178
|
+
// don't persist traces and spans
|
|
179
|
+
observability: false,
|
|
180
|
+
},
|
|
181
|
+
}),
|
|
182
|
+
})
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Options
|
|
186
|
+
|
|
187
|
+
**id** (`string`): Unique identifier for this storage instance.
|
|
188
|
+
|
|
189
|
+
**default** (`MastraCompositeStore`): Default storage adapter. Domains not explicitly specified in domains will use this storage's domains as fallbacks.
|
|
190
|
+
|
|
191
|
+
**disableInit** (`boolean`): When true, automatic initialization is disabled. You must call init() explicitly.
|
|
192
|
+
|
|
193
|
+
**domains** (`object`): Individual domain overrides. Each domain can come from a different storage adapter. These take precedence over both editor and default storage. Set a domain to false to disable it entirely; a disabled domain does not fall back to editor or default.
|
|
194
|
+
|
|
195
|
+
**domains.memory** (`MemoryStorage`): Storage for threads, messages, and resources.
|
|
196
|
+
|
|
197
|
+
**domains.workflows** (`WorkflowsStorage`): Storage for workflow snapshots.
|
|
198
|
+
|
|
199
|
+
**domains.scores** (`ScoresStorage`): Storage for evaluation scores.
|
|
200
|
+
|
|
201
|
+
**domains.observability** (`ObservabilityStorage`): Storage for traces and spans.
|
|
202
|
+
|
|
203
|
+
**domains.agents** (`AgentsStorage`): Storage for stored agent configurations.
|
|
204
|
+
|
|
205
|
+
**domains.datasets** (`DatasetsStorage`): Storage for dataset metadata, dataset items, and dataset versions.
|
|
206
|
+
|
|
207
|
+
**domains.experiments** (`ExperimentsStorage`): Storage for experiment runs and per-item experiment results.
|
|
208
|
+
|
|
209
|
+
## Initialization
|
|
210
|
+
|
|
211
|
+
`MastraCompositeStore` initializes each configured domain independently. When passed to the Mastra class, `init()` is called automatically:
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
import { MastraCompositeStore } from '@mastra/core/storage'
|
|
215
|
+
import { MemoryPG, WorkflowsPG, ScoresPG } from '@mastra/pg'
|
|
216
|
+
import { Mastra } from '@mastra/core'
|
|
217
|
+
|
|
218
|
+
const storage = new MastraCompositeStore({
|
|
219
|
+
id: 'composite',
|
|
220
|
+
domains: {
|
|
221
|
+
memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
|
|
222
|
+
workflows: new WorkflowsPG({ connectionString: process.env.DATABASE_URL }),
|
|
223
|
+
scores: new ScoresPG({ connectionString: process.env.DATABASE_URL }),
|
|
224
|
+
},
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
export const mastra = new Mastra({
|
|
228
|
+
storage, // init() called automatically
|
|
229
|
+
})
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
If using storage directly, call `init()` explicitly:
|
|
233
|
+
|
|
234
|
+
```typescript
|
|
235
|
+
import { MastraCompositeStore } from '@mastra/core/storage'
|
|
236
|
+
import { MemoryPG } from '@mastra/pg'
|
|
237
|
+
|
|
238
|
+
const storage = new MastraCompositeStore({
|
|
239
|
+
id: 'composite',
|
|
240
|
+
domains: {
|
|
241
|
+
memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
|
|
242
|
+
},
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
await storage.init()
|
|
246
|
+
|
|
247
|
+
// Access domain-specific stores via getStore()
|
|
248
|
+
const memoryStore = await storage.getStore('memory')
|
|
249
|
+
const thread = await memoryStore?.getThreadById({ threadId: '...' })
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Use cases
|
|
253
|
+
|
|
254
|
+
### Separate databases for different workloads
|
|
255
|
+
|
|
256
|
+
Use a local database for development while keeping production data in a managed service:
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
import { MastraCompositeStore } from '@mastra/core/storage'
|
|
260
|
+
import { MemoryPG, WorkflowsPG, ScoresPG } from '@mastra/pg'
|
|
261
|
+
import { MemoryLibSQL } from '@mastra/libsql'
|
|
262
|
+
|
|
263
|
+
const storage = new MastraCompositeStore({
|
|
264
|
+
id: 'composite',
|
|
265
|
+
domains: {
|
|
266
|
+
// Use local SQLite for development, PostgreSQL for production
|
|
267
|
+
memory:
|
|
268
|
+
process.env.NODE_ENV === 'development'
|
|
269
|
+
? new MemoryLibSQL({ url: 'file:./dev.db' })
|
|
270
|
+
: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
|
|
271
|
+
workflows: new WorkflowsPG({ connectionString: process.env.DATABASE_URL }),
|
|
272
|
+
scores: new ScoresPG({ connectionString: process.env.DATABASE_URL }),
|
|
273
|
+
},
|
|
274
|
+
})
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
### Specialized storage for observability
|
|
278
|
+
|
|
279
|
+
Observability data can quickly overwhelm general-purpose databases in production. A single agent interaction can generate hundreds of spans, and high-traffic applications can produce thousands of traces per day.
|
|
280
|
+
|
|
281
|
+
**[ClickHouse](https://mastra.ai/reference/storage/clickhouse)** is recommended for production observability because it's optimized for high-volume, write-heavy analytics workloads. Use composite storage to route observability to ClickHouse while keeping other data in your primary database:
|
|
282
|
+
|
|
283
|
+
```typescript
|
|
284
|
+
import { MastraCompositeStore } from '@mastra/core/storage'
|
|
285
|
+
import { MemoryPG, WorkflowsPG, ScoresPG } from '@mastra/pg'
|
|
286
|
+
import { ObservabilityStorageClickhouseVNext } from '@mastra/clickhouse'
|
|
287
|
+
|
|
288
|
+
const storage = new MastraCompositeStore({
|
|
289
|
+
id: 'composite',
|
|
290
|
+
domains: {
|
|
291
|
+
memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
|
|
292
|
+
workflows: new WorkflowsPG({ connectionString: process.env.DATABASE_URL }),
|
|
293
|
+
scores: new ScoresPG({ connectionString: process.env.DATABASE_URL }),
|
|
294
|
+
observability: new ObservabilityStorageClickhouseVNext({
|
|
295
|
+
url: process.env.CLICKHOUSE_URL,
|
|
296
|
+
username: process.env.CLICKHOUSE_USERNAME,
|
|
297
|
+
password: process.env.CLICKHOUSE_PASSWORD,
|
|
298
|
+
}),
|
|
299
|
+
},
|
|
300
|
+
})
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
> **Note:** `ObservabilityStorageClickhouseVNext` is the current observability domain implementation. The legacy `ObservabilityStorageClickhouse` class is also exported and remains supported for projects that haven't migrated. See the [ClickHouse storage reference](https://mastra.ai/reference/storage/clickhouse) for details.
|
|
304
|
+
|
|
305
|
+
### Replicated ClickHouse for multi-replica clusters
|
|
306
|
+
|
|
307
|
+
For self-managed ClickHouse clusters with multiple replicas, set `replication` so Mastra emits `ReplicatedMergeTree` engines and applies `ON CLUSTER` to its DDL:
|
|
308
|
+
|
|
309
|
+
```typescript
|
|
310
|
+
import { MastraCompositeStore } from '@mastra/core/storage'
|
|
311
|
+
import { MemoryPG, WorkflowsPG, ScoresPG } from '@mastra/pg'
|
|
312
|
+
import { ObservabilityStorageClickhouseVNext } from '@mastra/clickhouse'
|
|
313
|
+
|
|
314
|
+
const storage = new MastraCompositeStore({
|
|
315
|
+
id: 'composite',
|
|
316
|
+
domains: {
|
|
317
|
+
memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
|
|
318
|
+
workflows: new WorkflowsPG({ connectionString: process.env.DATABASE_URL }),
|
|
319
|
+
scores: new ScoresPG({ connectionString: process.env.DATABASE_URL }),
|
|
320
|
+
observability: new ObservabilityStorageClickhouseVNext({
|
|
321
|
+
url: process.env.CLICKHOUSE_URL,
|
|
322
|
+
username: process.env.CLICKHOUSE_USERNAME,
|
|
323
|
+
password: process.env.CLICKHOUSE_PASSWORD,
|
|
324
|
+
replication: {
|
|
325
|
+
cluster: 'production_cluster',
|
|
326
|
+
// Optional (defaults shown):
|
|
327
|
+
// zookeeperPath: '/clickhouse/tables/{shard}/{database}/{table}',
|
|
328
|
+
// replicaName: '{replica}',
|
|
329
|
+
},
|
|
330
|
+
}),
|
|
331
|
+
},
|
|
332
|
+
})
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
Don't set `replication` on ClickHouse Cloud. Cloud rewrites `MergeTree` to `SharedMergeTree` server-side. See the [ClickHouse storage reference](https://mastra.ai/reference/storage/clickhouse) for the full config shape and operator notes.
|
|
336
|
+
|
|
337
|
+
> **Info:** This approach is also required when using storage providers that don't support observability (like Convex, DynamoDB, or Cloudflare). See the [MastraStorageExporter documentation](https://mastra.ai/docs/observability/integrations/exporters/mastra-storage) for the full list of supported providers.
|