@mastra/cloudflare-d1 0.0.0-error-handler-fix-20251020202607 → 0.0.0-execa-dynamic-import-20260304221256
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 +1477 -3
- package/LICENSE.md +15 -0
- package/README.md +10 -5
- package/dist/docs/SKILL.md +22 -0
- package/dist/docs/assets/SOURCE_MAP.json +6 -0
- package/dist/docs/references/reference-storage-cloudflare-d1.md +218 -0
- package/dist/index.cjs +921 -1262
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +919 -1263
- package/dist/index.js.map +1 -1
- package/dist/storage/{domains/operations → db}/index.d.ts +42 -6
- package/dist/storage/db/index.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +19 -49
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/scores/index.d.ts +16 -38
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +19 -25
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +58 -245
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +19 -15
- package/dist/storage/domains/legacy-evals/index.d.ts +0 -20
- package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
- package/dist/storage/domains/operations/index.d.ts.map +0 -1
- package/dist/storage/domains/traces/index.d.ts +0 -18
- package/dist/storage/domains/traces/index.d.ts.map +0 -1
package/LICENSE.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
Portions of this software are licensed as follows:
|
|
2
|
+
|
|
3
|
+
- All content that resides under any directory named "ee/" within this
|
|
4
|
+
repository, including but not limited to:
|
|
5
|
+
- `packages/core/src/auth/ee/`
|
|
6
|
+
- `packages/server/src/server/auth/ee/`
|
|
7
|
+
is licensed under the license defined in `ee/LICENSE`.
|
|
8
|
+
|
|
9
|
+
- All third-party components incorporated into the Mastra Software are
|
|
10
|
+
licensed under the original license provided by the owner of the
|
|
11
|
+
applicable component.
|
|
12
|
+
|
|
13
|
+
- Content outside of the above-mentioned directories or restrictions is
|
|
14
|
+
available under the "Apache License 2.0" as defined below.
|
|
15
|
+
|
|
1
16
|
# Apache License 2.0
|
|
2
17
|
|
|
3
18
|
Copyright (c) 2025 Kepler Software, Inc.
|
package/README.md
CHANGED
|
@@ -75,24 +75,29 @@ const store = new D1Store({
|
|
|
75
75
|
|
|
76
76
|
- `saveThread(thread)`: Create or update a thread
|
|
77
77
|
- `getThreadById({ threadId })`: Get a thread by ID
|
|
78
|
-
- `
|
|
78
|
+
- `listThreadsByResourceId({ resourceId, offset, limit, orderBy? })`: List paginated threads for a resource
|
|
79
79
|
- `updateThread({ id, title, metadata })`: Update the title and/or metadata of a thread.
|
|
80
80
|
- `deleteThread({ threadId })`: Delete a thread and all its messages.
|
|
81
81
|
|
|
82
82
|
### Message Operations
|
|
83
83
|
|
|
84
84
|
- `saveMessages({ messages })`: Save multiple messages in a batch operation (uses prepared statements).
|
|
85
|
-
- `
|
|
85
|
+
- `listMessages({ threadId, perPage?, page? })`: Retrieve messages for a thread with pagination.
|
|
86
|
+
- `listMessagesById({ messageIds })`: Get specific messages by their IDs
|
|
87
|
+
- `updateMessages({ messages })`: Update existing messages
|
|
86
88
|
|
|
87
89
|
### Workflow Operations
|
|
88
90
|
|
|
89
91
|
- `persistWorkflowSnapshot({ workflowName, runId, snapshot })`: Save workflow state for a given workflow/run.
|
|
90
92
|
- `loadWorkflowSnapshot({ workflowName, runId })`: Load persisted workflow state.
|
|
93
|
+
- `listWorkflowRuns({ workflowName, pagination })`: List workflow runs with pagination
|
|
94
|
+
- `getWorkflowRunById({ workflowName, runId })`: Get a specific workflow run
|
|
91
95
|
|
|
92
|
-
###
|
|
96
|
+
### Operations Not Currently Supported
|
|
93
97
|
|
|
94
|
-
- `
|
|
95
|
-
-
|
|
98
|
+
- `deleteMessages(messageIds)`: Message deletion is not currently supported
|
|
99
|
+
- AI Observability (traces/spans): Not currently supported
|
|
100
|
+
- Evaluation/Scoring: Not currently supported
|
|
96
101
|
|
|
97
102
|
### Utility
|
|
98
103
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mastra-cloudflare-d1
|
|
3
|
+
description: Documentation for @mastra/cloudflare-d1. Use when working with @mastra/cloudflare-d1 APIs, configuration, or implementation.
|
|
4
|
+
metadata:
|
|
5
|
+
package: "@mastra/cloudflare-d1"
|
|
6
|
+
version: "0.0.0-execa-dynamic-import-20260304221256"
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## When to use
|
|
10
|
+
|
|
11
|
+
Use this skill whenever you are working with @mastra/cloudflare-d1 to obtain the domain-specific knowledge.
|
|
12
|
+
|
|
13
|
+
## How to use
|
|
14
|
+
|
|
15
|
+
Read the individual reference documents for detailed explanations and code examples.
|
|
16
|
+
|
|
17
|
+
### Reference
|
|
18
|
+
|
|
19
|
+
- [Reference: Cloudflare D1 Storage](references/reference-storage-cloudflare-d1.md) - Documentation for the Cloudflare D1 SQL storage implementation in Mastra.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
Read [assets/SOURCE_MAP.json](assets/SOURCE_MAP.json) for source code references.
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# Cloudflare D1 Storage
|
|
2
|
+
|
|
3
|
+
The Cloudflare D1 storage implementation provides a serverless SQL database solution using Cloudflare D1, supporting relational operations and transactional consistency.
|
|
4
|
+
|
|
5
|
+
> **Observability Not Supported:** Cloudflare D1 storage **does not support the observability domain**. Traces from the `DefaultExporter` cannot be persisted to D1, and Mastra Studio's observability features won't work with D1 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 or PostgreSQL.
|
|
6
|
+
|
|
7
|
+
> **Row Size Limit:** Cloudflare D1 enforces a **1 MiB maximum row 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/storage) for workarounds including uploading attachments to external storage.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
**npm**:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @mastra/cloudflare-d1@latest
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
**pnpm**:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm add @mastra/cloudflare-d1@latest
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Yarn**:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
yarn add @mastra/cloudflare-d1@latest
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Bun**:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
bun add @mastra/cloudflare-d1@latest
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
### Using with Cloudflare Workers
|
|
38
|
+
|
|
39
|
+
When using D1Store in a Cloudflare Worker, you need to access the D1 binding from the worker's `env` parameter at runtime. The `D1Database` in your type definition is only for TypeScript type checking—the actual binding is provided by the Workers runtime.
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { D1Store } from '@mastra/cloudflare-d1'
|
|
43
|
+
import { Mastra } from '@mastra/core'
|
|
44
|
+
import { CloudflareDeployer } from '@mastra/deployer-cloudflare'
|
|
45
|
+
|
|
46
|
+
type Env = {
|
|
47
|
+
D1Database: D1Database // TypeScript type definition
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Factory function to create Mastra with D1 binding
|
|
51
|
+
function createMastra(env: Env) {
|
|
52
|
+
const storage = new D1Store({
|
|
53
|
+
binding: env.D1Database, // ✅ Access the actual binding from env
|
|
54
|
+
tablePrefix: 'dev_', // Optional: isolate tables per environment
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
return new Mastra({
|
|
58
|
+
storage,
|
|
59
|
+
deployer: new CloudflareDeployer({
|
|
60
|
+
name: 'my-worker',
|
|
61
|
+
d1_databases: [
|
|
62
|
+
{
|
|
63
|
+
binding: 'D1Database', // Must match the property name in Env type
|
|
64
|
+
database_name: 'your-database-name',
|
|
65
|
+
database_id: 'your-database-id',
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
}),
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Cloudflare Worker export
|
|
73
|
+
export default {
|
|
74
|
+
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
|
|
75
|
+
const mastra = createMastra(env)
|
|
76
|
+
|
|
77
|
+
// Your handler logic here
|
|
78
|
+
return new Response('Hello from Mastra with D1!')
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
> **Important: Understanding D1 Bindings:** In the `Env` type definition, `D1Database: D1Database` serves two purposes:
|
|
84
|
+
>
|
|
85
|
+
> - The **property name** (`D1Database`) must match the `binding` name in your `wrangler.toml`
|
|
86
|
+
> - The **type** (`: D1Database`) is from `@cloudflare/workers-types` for TypeScript type checking
|
|
87
|
+
>
|
|
88
|
+
> At runtime, Cloudflare Workers provides the actual D1 database instance via `env.D1Database`. You cannot use `D1Database` directly outside of the worker's context.
|
|
89
|
+
|
|
90
|
+
### Using with REST API
|
|
91
|
+
|
|
92
|
+
For non-Workers environments (Node.js, serverless functions, etc.), use the REST API approach:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { D1Store } from '@mastra/cloudflare-d1'
|
|
96
|
+
|
|
97
|
+
const storage = new D1Store({
|
|
98
|
+
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!, // Cloudflare Account ID
|
|
99
|
+
databaseId: process.env.CLOUDFLARE_D1_DATABASE_ID!, // D1 Database ID
|
|
100
|
+
apiToken: process.env.CLOUDFLARE_API_TOKEN!, // Cloudflare API Token
|
|
101
|
+
tablePrefix: 'dev_', // Optional: isolate tables per environment
|
|
102
|
+
})
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Wrangler Configuration
|
|
106
|
+
|
|
107
|
+
Add the D1 database binding to your `wrangler.toml`:
|
|
108
|
+
|
|
109
|
+
```toml
|
|
110
|
+
[[d1_databases]]
|
|
111
|
+
binding = "D1Database" # Must match the property name in your Env type
|
|
112
|
+
database_name = "your-database-name"
|
|
113
|
+
database_id = "your-database-id"
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Or in `wrangler.jsonc`:
|
|
117
|
+
|
|
118
|
+
```jsonc
|
|
119
|
+
{
|
|
120
|
+
"d1_databases": [
|
|
121
|
+
{
|
|
122
|
+
"binding": "D1Database",
|
|
123
|
+
"database_name": "your-database-name",
|
|
124
|
+
"database_id": "your-database-id",
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Parameters
|
|
131
|
+
|
|
132
|
+
**binding?:** (`D1Database`): Cloudflare D1 Workers binding (for Workers runtime)
|
|
133
|
+
|
|
134
|
+
**accountId?:** (`string`): Cloudflare Account ID (for REST API)
|
|
135
|
+
|
|
136
|
+
**databaseId?:** (`string`): Cloudflare D1 Database ID (for REST API)
|
|
137
|
+
|
|
138
|
+
**apiToken?:** (`string`): Cloudflare API Token (for REST API)
|
|
139
|
+
|
|
140
|
+
**tablePrefix?:** (`string`): Optional prefix for all table names (useful for environment isolation)
|
|
141
|
+
|
|
142
|
+
## Additional Notes
|
|
143
|
+
|
|
144
|
+
### Schema Management
|
|
145
|
+
|
|
146
|
+
The storage implementation handles schema creation and updates automatically. It creates the following tables:
|
|
147
|
+
|
|
148
|
+
- `threads`: Stores conversation threads
|
|
149
|
+
- `messages`: Stores individual messages
|
|
150
|
+
- `metadata`: Stores additional metadata for threads and messages
|
|
151
|
+
|
|
152
|
+
### Initialization
|
|
153
|
+
|
|
154
|
+
When you pass storage to the Mastra class, `init()` is called automatically before any storage operation:
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { Mastra } from '@mastra/core'
|
|
158
|
+
import { D1Store } from '@mastra/cloudflare-d1'
|
|
159
|
+
|
|
160
|
+
type Env = {
|
|
161
|
+
D1Database: D1Database
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// In a Cloudflare Worker
|
|
165
|
+
export default {
|
|
166
|
+
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
|
|
167
|
+
const storage = new D1Store({
|
|
168
|
+
binding: env.D1Database, // ✅ Use env.D1Database
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
const mastra = new Mastra({
|
|
172
|
+
storage, // init() is called automatically
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
// Your handler logic here
|
|
176
|
+
return new Response('Success')
|
|
177
|
+
},
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
If you're using storage directly without Mastra, you must call `init()` explicitly to create the tables:
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
import { D1Store } from '@mastra/cloudflare-d1'
|
|
185
|
+
|
|
186
|
+
type Env = {
|
|
187
|
+
D1Database: D1Database
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// In a Cloudflare Worker
|
|
191
|
+
export default {
|
|
192
|
+
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
|
|
193
|
+
const storage = new D1Store({
|
|
194
|
+
id: 'd1-storage',
|
|
195
|
+
binding: env.D1Database, // ✅ Use env.D1Database
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
// Required when using storage directly
|
|
199
|
+
await storage.init()
|
|
200
|
+
|
|
201
|
+
// Access domain-specific stores via getStore()
|
|
202
|
+
const memoryStore = await storage.getStore('memory')
|
|
203
|
+
const thread = await memoryStore?.getThreadById({ threadId: '...' })
|
|
204
|
+
|
|
205
|
+
return new Response('Success')
|
|
206
|
+
},
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
> **Warning:** If `init()` is not called, tables won't be created and storage operations will fail silently or throw errors.
|
|
211
|
+
|
|
212
|
+
### Transactions & Consistency
|
|
213
|
+
|
|
214
|
+
Cloudflare D1 provides transactional guarantees for single-row operations. This means that multiple operations can be executed as a single, all-or-nothing unit of work.
|
|
215
|
+
|
|
216
|
+
### Table Creation & Migrations
|
|
217
|
+
|
|
218
|
+
Tables are created automatically when storage is initialized (and can be isolated per environment using the `tablePrefix` option), but advanced schema changes—such as adding columns, changing data types, or modifying indexes—require manual migration and careful planning to avoid data loss.
|