@mastra/libsql 0.0.0-error-handler-fix-20251020202607 → 0.0.0-esbuild-bundle-worker-20260807173433

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.
Files changed (104) hide show
  1. package/CHANGELOG.md +4789 -3
  2. package/LICENSE.md +15 -0
  3. package/README.md +30 -20
  4. package/dist/docs/SKILL.md +52 -0
  5. package/dist/docs/assets/SOURCE_MAP.json +6 -0
  6. package/dist/docs/references/docs-agents-agent-approval.md +664 -0
  7. package/dist/docs/references/docs-agents-networks.md +184 -0
  8. package/dist/docs/references/docs-deployment-workers.md +137 -0
  9. package/dist/docs/references/docs-editor-overview.md +349 -0
  10. package/dist/docs/references/docs-memory-memory-processors.md +385 -0
  11. package/dist/docs/references/docs-memory-message-history.md +348 -0
  12. package/dist/docs/references/docs-memory-multi-user-threads.md +208 -0
  13. package/dist/docs/references/docs-memory-overview.md +266 -0
  14. package/dist/docs/references/docs-memory-semantic-recall.md +401 -0
  15. package/dist/docs/references/docs-memory-working-memory.md +431 -0
  16. package/dist/docs/references/docs-storage-overview.md +214 -0
  17. package/dist/docs/references/docs-workflows-snapshots.md +240 -0
  18. package/dist/docs/references/guides-agent-frameworks-ai-sdk.md +142 -0
  19. package/dist/docs/references/guides-rag-retrieval.md +520 -0
  20. package/dist/docs/references/reference-core-getMemory.md +51 -0
  21. package/dist/docs/references/reference-core-listMemory.md +57 -0
  22. package/dist/docs/references/reference-core-mastra-class.md +153 -0
  23. package/dist/docs/references/reference-file-based-agents-memory.md +58 -0
  24. package/dist/docs/references/reference-file-based-agents-storage.md +30 -0
  25. package/dist/docs/references/reference-memory-memory-class.md +148 -0
  26. package/dist/docs/references/reference-storage-composite.md +339 -0
  27. package/dist/docs/references/reference-storage-dynamodb.md +284 -0
  28. package/dist/docs/references/reference-storage-libsql.md +143 -0
  29. package/dist/docs/references/reference-storage-retention.md +248 -0
  30. package/dist/docs/references/reference-vectors-libsql.md +307 -0
  31. package/dist/index.cjs +13164 -3390
  32. package/dist/index.cjs.map +1 -1
  33. package/dist/index.js +13134 -3383
  34. package/dist/index.js.map +1 -1
  35. package/dist/storage/db/index.d.ts +389 -0
  36. package/dist/storage/db/index.d.ts.map +1 -0
  37. package/dist/storage/db/utils.d.ts +84 -0
  38. package/dist/storage/db/utils.d.ts.map +1 -0
  39. package/dist/storage/db/write-lock.d.ts +8 -0
  40. package/dist/storage/db/write-lock.d.ts.map +1 -0
  41. package/dist/storage/domains/agents/index.d.ts +30 -0
  42. package/dist/storage/domains/agents/index.d.ts.map +1 -0
  43. package/dist/storage/domains/background-tasks/index.d.ts +27 -0
  44. package/dist/storage/domains/background-tasks/index.d.ts.map +1 -0
  45. package/dist/storage/domains/blobs/index.d.ts +17 -0
  46. package/dist/storage/domains/blobs/index.d.ts.map +1 -0
  47. package/dist/storage/domains/channels/index.d.ts +20 -0
  48. package/dist/storage/domains/channels/index.d.ts.map +1 -0
  49. package/dist/storage/domains/datasets/index.d.ts +43 -0
  50. package/dist/storage/domains/datasets/index.d.ts.map +1 -0
  51. package/dist/storage/domains/experiments/index.d.ts +54 -0
  52. package/dist/storage/domains/experiments/index.d.ts.map +1 -0
  53. package/dist/storage/domains/favorites/index.d.ts +17 -0
  54. package/dist/storage/domains/favorites/index.d.ts.map +1 -0
  55. package/dist/storage/domains/harness/index.d.ts +17 -0
  56. package/dist/storage/domains/harness/index.d.ts.map +1 -0
  57. package/dist/storage/domains/mcp-clients/index.d.ts +26 -0
  58. package/dist/storage/domains/mcp-clients/index.d.ts.map +1 -0
  59. package/dist/storage/domains/mcp-servers/index.d.ts +26 -0
  60. package/dist/storage/domains/mcp-servers/index.d.ts.map +1 -0
  61. package/dist/storage/domains/memory/index.d.ts +58 -54
  62. package/dist/storage/domains/memory/index.d.ts.map +1 -1
  63. package/dist/storage/domains/notifications/index.d.ts +23 -0
  64. package/dist/storage/domains/notifications/index.d.ts.map +1 -0
  65. package/dist/storage/domains/observability/index.d.ts +49 -29
  66. package/dist/storage/domains/observability/index.d.ts.map +1 -1
  67. package/dist/storage/domains/prompt-blocks/index.d.ts +25 -0
  68. package/dist/storage/domains/prompt-blocks/index.d.ts.map +1 -0
  69. package/dist/storage/domains/schedules/index.d.ts +27 -0
  70. package/dist/storage/domains/schedules/index.d.ts.map +1 -0
  71. package/dist/storage/domains/scorer-definitions/index.d.ts +26 -0
  72. package/dist/storage/domains/scorer-definitions/index.d.ts.map +1 -0
  73. package/dist/storage/domains/scores/index.d.ts +29 -31
  74. package/dist/storage/domains/scores/index.d.ts.map +1 -1
  75. package/dist/storage/domains/skills/index.d.ts +26 -0
  76. package/dist/storage/domains/skills/index.d.ts.map +1 -0
  77. package/dist/storage/domains/thread-state/index.d.ts +38 -0
  78. package/dist/storage/domains/thread-state/index.d.ts.map +1 -0
  79. package/dist/storage/domains/tool-provider-connections/index.d.ts +14 -0
  80. package/dist/storage/domains/tool-provider-connections/index.d.ts.map +1 -0
  81. package/dist/storage/domains/utils.d.ts +28 -54
  82. package/dist/storage/domains/utils.d.ts.map +1 -1
  83. package/dist/storage/domains/workflow-definitions/index.d.ts +14 -0
  84. package/dist/storage/domains/workflow-definitions/index.d.ts.map +1 -0
  85. package/dist/storage/domains/workflows/index.d.ts +27 -32
  86. package/dist/storage/domains/workflows/index.d.ts.map +1 -1
  87. package/dist/storage/domains/workspaces/index.d.ts +26 -0
  88. package/dist/storage/domains/workspaces/index.d.ts.map +1 -0
  89. package/dist/storage/factory-storage.d.ts +28 -0
  90. package/dist/storage/factory-storage.d.ts.map +1 -0
  91. package/dist/storage/index.d.ts +128 -255
  92. package/dist/storage/index.d.ts.map +1 -1
  93. package/dist/storage/retention.d.ts +77 -0
  94. package/dist/storage/retention.d.ts.map +1 -0
  95. package/dist/vector/index.d.ts +31 -4
  96. package/dist/vector/index.d.ts.map +1 -1
  97. package/dist/vector/sql-builder.d.ts.map +1 -1
  98. package/package.json +22 -16
  99. package/dist/storage/domains/legacy-evals/index.d.ts +0 -18
  100. package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
  101. package/dist/storage/domains/operations/index.d.ts +0 -110
  102. package/dist/storage/domains/operations/index.d.ts.map +0 -1
  103. package/dist/storage/domains/traces/index.d.ts +0 -21
  104. package/dist/storage/domains/traces/index.d.ts.map +0 -1
@@ -0,0 +1,339 @@
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
+ **editor** (`MastraCompositeStore`): Storage adapter for Editor-owned domains, including agents, prompt blocks, scorers, MCP clients and servers, workspaces, and skills. Takes precedence over default storage but not explicit domain overrides.
192
+
193
+ **disableInit** (`boolean`): When true, automatic initialization is disabled. You must call init() explicitly.
194
+
195
+ **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.
196
+
197
+ **domains.memory** (`MemoryStorage`): Storage for threads, messages, and resources.
198
+
199
+ **domains.workflows** (`WorkflowsStorage`): Storage for workflow snapshots.
200
+
201
+ **domains.scores** (`ScoresStorage`): Storage for evaluation scores.
202
+
203
+ **domains.observability** (`ObservabilityStorage`): Storage for traces and spans.
204
+
205
+ **domains.agents** (`AgentsStorage`): Storage for stored agent configurations.
206
+
207
+ **domains.datasets** (`DatasetsStorage`): Storage for dataset metadata, dataset items, and dataset versions.
208
+
209
+ **domains.experiments** (`ExperimentsStorage`): Storage for experiment runs and per-item experiment results.
210
+
211
+ ## Initialization
212
+
213
+ `MastraCompositeStore` initializes each configured domain independently. When passed to the Mastra class, `init()` is called automatically:
214
+
215
+ ```typescript
216
+ import { MastraCompositeStore } from '@mastra/core/storage'
217
+ import { MemoryPG, WorkflowsPG, ScoresPG } from '@mastra/pg'
218
+ import { Mastra } from '@mastra/core'
219
+
220
+ const storage = new MastraCompositeStore({
221
+ id: 'composite',
222
+ domains: {
223
+ memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
224
+ workflows: new WorkflowsPG({ connectionString: process.env.DATABASE_URL }),
225
+ scores: new ScoresPG({ connectionString: process.env.DATABASE_URL }),
226
+ },
227
+ })
228
+
229
+ export const mastra = new Mastra({
230
+ storage, // init() called automatically
231
+ })
232
+ ```
233
+
234
+ If using storage directly, call `init()` explicitly:
235
+
236
+ ```typescript
237
+ import { MastraCompositeStore } from '@mastra/core/storage'
238
+ import { MemoryPG } from '@mastra/pg'
239
+
240
+ const storage = new MastraCompositeStore({
241
+ id: 'composite',
242
+ domains: {
243
+ memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
244
+ },
245
+ })
246
+
247
+ await storage.init()
248
+
249
+ // Access domain-specific stores via getStore()
250
+ const memoryStore = await storage.getStore('memory')
251
+ const thread = await memoryStore?.getThreadById({ threadId: '...' })
252
+ ```
253
+
254
+ ## Use cases
255
+
256
+ ### Separate databases for different workloads
257
+
258
+ Use a local database for development while keeping production data in a managed service:
259
+
260
+ ```typescript
261
+ import { MastraCompositeStore } from '@mastra/core/storage'
262
+ import { MemoryPG, WorkflowsPG, ScoresPG } from '@mastra/pg'
263
+ import { MemoryLibSQL } from '@mastra/libsql'
264
+
265
+ const storage = new MastraCompositeStore({
266
+ id: 'composite',
267
+ domains: {
268
+ // Use local SQLite for development, PostgreSQL for production
269
+ memory:
270
+ process.env.NODE_ENV === 'development'
271
+ ? new MemoryLibSQL({ url: 'file:./dev.db' })
272
+ : new MemoryPG({ connectionString: process.env.DATABASE_URL }),
273
+ workflows: new WorkflowsPG({ connectionString: process.env.DATABASE_URL }),
274
+ scores: new ScoresPG({ connectionString: process.env.DATABASE_URL }),
275
+ },
276
+ })
277
+ ```
278
+
279
+ ### Specialized storage for observability
280
+
281
+ 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.
282
+
283
+ **[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:
284
+
285
+ ```typescript
286
+ import { MastraCompositeStore } from '@mastra/core/storage'
287
+ import { MemoryPG, WorkflowsPG, ScoresPG } from '@mastra/pg'
288
+ import { ObservabilityStorageClickhouseVNext } from '@mastra/clickhouse'
289
+
290
+ const storage = new MastraCompositeStore({
291
+ id: 'composite',
292
+ domains: {
293
+ memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
294
+ workflows: new WorkflowsPG({ connectionString: process.env.DATABASE_URL }),
295
+ scores: new ScoresPG({ connectionString: process.env.DATABASE_URL }),
296
+ observability: new ObservabilityStorageClickhouseVNext({
297
+ url: process.env.CLICKHOUSE_URL,
298
+ username: process.env.CLICKHOUSE_USERNAME,
299
+ password: process.env.CLICKHOUSE_PASSWORD,
300
+ }),
301
+ },
302
+ })
303
+ ```
304
+
305
+ > **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.
306
+
307
+ ### Replicated ClickHouse for multi-replica clusters
308
+
309
+ For self-managed ClickHouse clusters with multiple replicas, set `replication` so Mastra emits `ReplicatedMergeTree` engines and applies `ON CLUSTER` to its DDL:
310
+
311
+ ```typescript
312
+ import { MastraCompositeStore } from '@mastra/core/storage'
313
+ import { MemoryPG, WorkflowsPG, ScoresPG } from '@mastra/pg'
314
+ import { ObservabilityStorageClickhouseVNext } from '@mastra/clickhouse'
315
+
316
+ const storage = new MastraCompositeStore({
317
+ id: 'composite',
318
+ domains: {
319
+ memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
320
+ workflows: new WorkflowsPG({ connectionString: process.env.DATABASE_URL }),
321
+ scores: new ScoresPG({ connectionString: process.env.DATABASE_URL }),
322
+ observability: new ObservabilityStorageClickhouseVNext({
323
+ url: process.env.CLICKHOUSE_URL,
324
+ username: process.env.CLICKHOUSE_USERNAME,
325
+ password: process.env.CLICKHOUSE_PASSWORD,
326
+ replication: {
327
+ cluster: 'production_cluster',
328
+ // Optional (defaults shown):
329
+ // zookeeperPath: '/clickhouse/tables/{shard}/{database}/{table}',
330
+ // replicaName: '{replica}',
331
+ },
332
+ }),
333
+ },
334
+ })
335
+ ```
336
+
337
+ 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.
338
+
339
+ > **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.
@@ -0,0 +1,284 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # DynamoDB storage
4
+
5
+ The DynamoDB storage implementation provides a high-capacity and performant NoSQL database solution for Mastra, using a single-table design pattern with [ElectroDB](https://electrodb.dev/).
6
+
7
+ > **Observability Not Supported:** DynamoDB storage **doesn't support the observability domain**. Traces from the `MastraStorageExporter` can't be persisted to DynamoDB, and [Studio's](https://mastra.ai/docs/studio/overview) observability features won't work with DynamoDB as your only storage provider. To enable observability, use [composite storage](https://mastra.ai/reference/storage/composite) to route observability data to a supported provider like ClickHouse.
8
+
9
+ > **Item Size Limit:** DynamoDB enforces a **400 KB maximum item size**. This limit can be exceeded when storing messages with base64-encoded attachments such as images. See [Handling large attachments](https://mastra.ai/docs/memory/memory-processors) for workarounds including uploading attachments to external storage.
10
+
11
+ ## Features
12
+
13
+ - Efficient single-table design for all Mastra storage needs
14
+ - Based on ElectroDB for type-safe DynamoDB access
15
+ - Support for AWS credentials, regions, and endpoints
16
+ - Compatible with AWS DynamoDB Local for development
17
+ - Stores Thread, Message, Eval, and Workflow data
18
+ - Optimized for serverless environments
19
+ - Configurable TTL (Time To Live) for automatic data expiration per entity type
20
+
21
+ ## Installation
22
+
23
+ **npm**:
24
+
25
+ ```bash
26
+ npm install @mastra/dynamodb@latest
27
+ ```
28
+
29
+ **pnpm**:
30
+
31
+ ```bash
32
+ pnpm add @mastra/dynamodb@latest
33
+ ```
34
+
35
+ **Yarn**:
36
+
37
+ ```bash
38
+ yarn add @mastra/dynamodb@latest
39
+ ```
40
+
41
+ **Bun**:
42
+
43
+ ```bash
44
+ bun add @mastra/dynamodb@latest
45
+ ```
46
+
47
+ ## Prerequisites
48
+
49
+ Before using this package, you **must** create a DynamoDB table with a specific structure, including primary keys and Global Secondary Indexes (GSIs). This adapter expects the DynamoDB table and its GSIs to be provisioned externally.
50
+
51
+ Detailed instructions for setting up the table using AWS CloudFormation or AWS CDK are available in [TABLE\_SETUP.md](https://github.com/mastra-ai/mastra/blob/main/stores/dynamodb/TABLE_SETUP.md). Please ensure your table is configured according to those instructions before proceeding.
52
+
53
+ ## Usage
54
+
55
+ ### Basic Usage
56
+
57
+ ```typescript
58
+ import { Memory } from '@mastra/memory'
59
+ import { DynamoDBStore } from '@mastra/dynamodb'
60
+
61
+ // Initialize the DynamoDB storage
62
+ const storage = new DynamoDBStore({
63
+ id: 'dynamodb', // Unique identifier for this storage instance
64
+ config: {
65
+ tableName: 'mastra-single-table', // Name of your DynamoDB table
66
+ region: 'us-east-1', // Optional: AWS region, defaults to 'us-east-1'
67
+ // endpoint: "http://localhost:8000", // Optional: For local DynamoDB
68
+ // credentials: { accessKeyId: "YOUR_ACCESS_KEY", secretAccessKey: "YOUR_SECRET_KEY" } // Optional
69
+ },
70
+ })
71
+
72
+ // Example: Initialize Memory with DynamoDB storage
73
+ const memory = new Memory({
74
+ storage,
75
+ options: {
76
+ lastMessages: 10,
77
+ },
78
+ })
79
+ ```
80
+
81
+ ### Local Development with DynamoDB Local
82
+
83
+ For local development, you can use [DynamoDB Local](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html).
84
+
85
+ 1. **Run DynamoDB Local (e.g., using Docker):**
86
+
87
+ ```bash
88
+ docker run -p 8000:8000 amazon/dynamodb-local
89
+ ```
90
+
91
+ 2. **Configure `DynamoDBStore` to use the local endpoint:**
92
+
93
+ ```typescript
94
+ import { DynamoDBStore } from '@mastra/dynamodb'
95
+
96
+ const storage = new DynamoDBStore({
97
+ id: 'dynamodb-local',
98
+ config: {
99
+ tableName: 'mastra-single-table', // Ensure this table is created in your local DynamoDB
100
+ region: 'localhost', // Can be any string for local, 'localhost' is common
101
+ endpoint: 'http://localhost:8000',
102
+ // For DynamoDB Local, credentials are not typically required unless configured.
103
+ // If you've configured local credentials:
104
+ // credentials: { accessKeyId: "fakeMyKeyId", secretAccessKey: "fakeSecretAccessKey" }
105
+ },
106
+ })
107
+ ```
108
+
109
+ You will still need to create the table and GSIs in your local DynamoDB instance, for example, using the AWS CLI pointed to your local endpoint.
110
+
111
+ ## Parameters
112
+
113
+ **id** (`string`): Unique identifier for this storage instance.
114
+
115
+ **config.tableName** (`string`): The name of your DynamoDB table.
116
+
117
+ **config.region** (`string`): AWS region. Defaults to 'us-east-1'. For local development, can be set to 'localhost' or similar.
118
+
119
+ **config.endpoint** (`string`): Custom endpoint for DynamoDB (e.g., 'http\://localhost:8000' for local development).
120
+
121
+ **config.credentials** (`object`): AWS credentials object with accessKeyId and secretAccessKey. If not provided, the AWS SDK will attempt to source credentials from environment variables, IAM roles (e.g., for EC2/Lambda), or the shared AWS credentials file.
122
+
123
+ **config.ttl** (`object`): TTL (Time To Live) configuration for automatic data expiration. Configure per entity type: thread, message, trace, eval, workflow\_snapshot, resource, score. Each entity config includes: enabled (boolean), attributeName (string, default: 'ttl'), defaultTtlSeconds (number).
124
+
125
+ ## TTL (time to live) configuration
126
+
127
+ DynamoDB TTL allows you to automatically delete items after a specified duration for these use cases:
128
+
129
+ - **Cost optimization**: Automatically remove old data to reduce storage costs
130
+ - **Data lifecycle management**: Implement retention policies for compliance
131
+ - **Performance**: Prevent tables from growing indefinitely
132
+ - **Privacy compliance**: Automatically purge personal data after specified periods
133
+
134
+ ### Enabling TTL
135
+
136
+ To use TTL, you must:
137
+
138
+ 1. **Configure TTL in DynamoDBStore** (shown below)
139
+ 2. **Enable TTL on your DynamoDB table** via AWS Console or CLI, specifying the attribute name (default: `ttl`)
140
+
141
+ ```typescript
142
+ import { DynamoDBStore } from '@mastra/dynamodb'
143
+
144
+ const storage = new DynamoDBStore({
145
+ name: 'dynamodb',
146
+ config: {
147
+ tableName: 'mastra-single-table',
148
+ region: 'us-east-1',
149
+ ttl: {
150
+ // Messages expire after 30 days
151
+ message: {
152
+ enabled: true,
153
+ defaultTtlSeconds: 30 * 24 * 60 * 60, // 30 days
154
+ },
155
+ // Threads expire after 90 days
156
+ thread: {
157
+ enabled: true,
158
+ defaultTtlSeconds: 90 * 24 * 60 * 60, // 90 days
159
+ },
160
+ // Traces expire after 7 days with custom attribute name
161
+ trace: {
162
+ enabled: true,
163
+ attributeName: 'expiresAt', // Custom TTL attribute
164
+ defaultTtlSeconds: 7 * 24 * 60 * 60, // 7 days
165
+ },
166
+ // Workflow snapshots don't expire
167
+ workflow_snapshot: {
168
+ enabled: false,
169
+ },
170
+ },
171
+ },
172
+ })
173
+ ```
174
+
175
+ ### Supported Entity Types
176
+
177
+ TTL can be configured for these entity types:
178
+
179
+ | Entity | Description |
180
+ | ------------------- | ------------------------ |
181
+ | `thread` | Conversation threads |
182
+ | `message` | Messages within threads |
183
+ | `trace` | Observability traces |
184
+ | `eval` | Evaluation results |
185
+ | `workflow_snapshot` | Workflow state snapshots |
186
+ | `resource` | User/resource data |
187
+ | `score` | Scoring results |
188
+
189
+ ### TTL Entity Configuration
190
+
191
+ Each entity type accepts the following configuration:
192
+
193
+ **enabled** (`boolean`): Whether TTL is enabled for this entity type.
194
+
195
+ **attributeName** (`string`): The DynamoDB attribute name to use for TTL. Must match the TTL attribute configured on your DynamoDB table. Defaults to 'ttl'.
196
+
197
+ **defaultTtlSeconds** (`number`): Default TTL in seconds from item creation time. Items will be automatically deleted by DynamoDB after this duration.
198
+
199
+ ### Enabling TTL on Your DynamoDB Table
200
+
201
+ After configuring TTL in your code, you must enable TTL on the DynamoDB table itself:
202
+
203
+ **Using AWS CLI:**
204
+
205
+ ```bash
206
+ aws dynamodb update-time-to-live \
207
+ --table-name mastra-single-table \
208
+ --time-to-live-specification "Enabled=true, AttributeName=ttl"
209
+ ```
210
+
211
+ **Using AWS Console:**
212
+
213
+ 1. Go to the DynamoDB console
214
+ 2. Select your table
215
+ 3. Go to "Additional settings" tab
216
+ 4. Under "Time to Live (TTL)", select "Manage TTL"
217
+ 5. Enable TTL and specify the attribute name (default: `ttl`)
218
+
219
+ > **Note:** DynamoDB deletes expired items within 48 hours after expiration. Items remain queryable until actually deleted.
220
+
221
+ ## AWS IAM permissions
222
+
223
+ The IAM role or user executing the code needs appropriate permissions to interact with the specified DynamoDB table and its indexes. Below is a sample policy. Replace `${YOUR_TABLE_NAME}` with your actual table name and `${YOUR_AWS_REGION}` and `${YOUR_AWS_ACCOUNT_ID}` with appropriate values.
224
+
225
+ ```json
226
+ {
227
+ "Version": "2012-10-17",
228
+ "Statement": [
229
+ {
230
+ "Effect": "Allow",
231
+ "Action": [
232
+ "dynamodb:DescribeTable",
233
+ "dynamodb:GetItem",
234
+ "dynamodb:PutItem",
235
+ "dynamodb:UpdateItem",
236
+ "dynamodb:DeleteItem",
237
+ "dynamodb:Query",
238
+ "dynamodb:Scan",
239
+ "dynamodb:BatchGetItem",
240
+ "dynamodb:BatchWriteItem"
241
+ ],
242
+ "Resource": [
243
+ "arn:aws:dynamodb:${YOUR_AWS_REGION}:${YOUR_AWS_ACCOUNT_ID}:table/${YOUR_TABLE_NAME}",
244
+ "arn:aws:dynamodb:${YOUR_AWS_REGION}:${YOUR_AWS_ACCOUNT_ID}:table/${YOUR_TABLE_NAME}/index/*"
245
+ ]
246
+ }
247
+ ]
248
+ }
249
+ ```
250
+
251
+ ## Key considerations
252
+
253
+ Before diving into the architectural details, keep these key points in mind when working with the DynamoDB storage adapter:
254
+
255
+ - **External Table Provisioning:** This adapter _requires_ you to create and configure the DynamoDB table and its Global Secondary Indexes (GSIs) yourself, prior to using the adapter. Follow the guide in [TABLE\_SETUP.md](https://github.com/mastra-ai/mastra/blob/main/stores/dynamodb/TABLE_SETUP.md).
256
+ - **Single-Table Design:** All Mastra data (threads, messages, etc.) is stored in one DynamoDB table. This is a deliberate design choice optimized for DynamoDB, differing from relational database approaches.
257
+ - **Understanding GSIs:** Familiarity with how the GSIs are structured (as per `TABLE_SETUP.md`) is important for understanding data retrieval and potential query patterns.
258
+ - **ElectroDB:** The adapter uses ElectroDB to manage interactions with DynamoDB, providing a layer of abstraction and type safety over raw DynamoDB operations.
259
+
260
+ ## Architectural approach
261
+
262
+ This storage adapter utilizes a **single-table design pattern** with [ElectroDB](https://electrodb.dev/), a common and recommended approach for DynamoDB. This differs architecturally from relational database adapters (like `@mastra/pg` or `@mastra/libsql`) that typically use multiple tables, each dedicated to a specific entity (threads, messages, etc.).
263
+
264
+ Key aspects of this approach:
265
+
266
+ - **DynamoDB Native:** The single-table design is optimized for DynamoDB's key-value and query capabilities, often leading to better performance and capacity compared to mimicking relational models.
267
+ - **External Table Management:** Unlike some adapters that might offer helper functions to create tables via code, this adapter **expects the DynamoDB table and its associated Global Secondary Indexes (GSIs) to be provisioned externally** before use. Please refer to [TABLE\_SETUP.md](https://github.com/mastra-ai/mastra/blob/main/stores/dynamodb/TABLE_SETUP.md) for detailed instructions using tools like AWS CloudFormation or CDK. The adapter focuses solely on interacting with the pre-existing table structure.
268
+ - **Consistency via Interface:** While the underlying storage model differs, this adapter adheres to the same `MastraStorage` interface as other adapters, ensuring it can be used interchangeably within the Mastra `Memory` component.
269
+
270
+ ### Mastra Data in the Single Table
271
+
272
+ Within the single DynamoDB table, different Mastra data entities (such as Threads, Messages, Traces, Evals, and Workflows) are managed and distinguished using ElectroDB. ElectroDB defines specific models for each entity type, which include unique key structures and attributes. It allows the adapter to store and retrieve diverse data types efficiently within the same table.
273
+
274
+ For example, a `Thread` item might have a primary key like `THREAD#<threadId>`, while a `Message` item belonging to that thread might use `THREAD#<threadId>` as a partition key and `MESSAGE#<messageId>` as a sort key. The Global Secondary Indexes (GSIs), detailed in `TABLE_SETUP.md`, are strategically designed to support common access patterns across these different entities, such as fetching all messages for a thread or querying traces associated with a workflow.
275
+
276
+ ### Advantages of Single-Table Design
277
+
278
+ This implementation uses a single-table design pattern with ElectroDB, which offers several advantages within the context of DynamoDB:
279
+
280
+ 1. **Lower cost (potentially):** Fewer tables can simplify Read/Write Capacity Unit (RCU/WCU) provisioning and management, especially with on-demand capacity.
281
+ 2. **Better performance:** Related data can be co-located or accessed efficiently through GSIs, enabling fast lookups for common access patterns.
282
+ 3. **Simplified administration:** Fewer distinct tables to monitor and back up, with less to manage.
283
+ 4. **Reduced complexity in access patterns:** ElectroDB helps manage the complexity of item types and access patterns on a single table.
284
+ 5. **Transaction support:** DynamoDB transactions can be used across different "entity" types stored within the same table if needed.