{"_id":"@caleblawson/cloudflare-d1","name":"@caleblawson/cloudflare-d1","dist-tags":{"latest":"0.11.1-alpha.0"},"versions":{"0.11.1-alpha.0":{"name":"@caleblawson/cloudflare-d1","version":"0.11.1-alpha.0","description":"D1 provider for Mastra - includes db storage capabilities","type":"module","main":"dist/index.js","types":"dist/index.d.ts","exports":{".":{"import":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"require":{"types":"./dist/index.d.cts","default":"./dist/index.cjs"}},"./package.json":"./package.json"},"dependencies":{"cloudflare":"^4.3.0"},"devDependencies":{"@cloudflare/workers-types":"^4.20250610.0","@microsoft/api-extractor":"^7.52.8","@types/node":"^20.19.0","dotenv":"^16.5.0","eslint":"^9.28.0","miniflare":"^4.20250604.1","tsup":"^8.5.0","typescript":"^5.8.3","vitest":"^3.2.3","@internal/lint":"0.0.13","@internal/storage-test-utils":"0.0.9","@mastra/core":"npm:@caleblawson/core@0.10.7-alpha.0"},"peerDependencies":{"@mastra/core":">=0.10.4-0 <0.11.0"},"scripts":{"build":"tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting","build:watch":"pnpm build --watch","test":"vitest run","lint":"eslint ."},"gitHead":"c31e90130a5c9765c23f688ecea70adc6164543d","_id":"@caleblawson/cloudflare-d1@0.11.1-alpha.0","_integrity":"sha512-OLaIChG/Q7p7INzhLUuDVX8Hu8uH+pmrsxcLbo1REogQgixmsv1CYu2YOoyrzsjv2/Wo96teG5wp7oGfgvoGiA==","_resolved":"C:\\Users\\caleb\\AppData\\Local\\Temp\\a99ef51ff49bb5b8451032c86753a60d\\caleblawson-cloudflare-d1-0.11.1-alpha.0.tgz","_from":"file:caleblawson-cloudflare-d1-0.11.1-alpha.0.tgz","_nodeVersion":"21.2.0","_npmVersion":"10.2.3","dist":{"integrity":"sha512-OLaIChG/Q7p7INzhLUuDVX8Hu8uH+pmrsxcLbo1REogQgixmsv1CYu2YOoyrzsjv2/Wo96teG5wp7oGfgvoGiA==","shasum":"351e48746105fd794db1eccb82fffe8900f58dee","tarball":"https://registry.npmjs.org/@caleblawson/cloudflare-d1/-/cloudflare-d1-0.11.1-alpha.0.tgz","fileCount":9,"unpackedSize":137934,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQCVD0S7ITLnls17bnLMx3eot++la28U+WLy85QVn0tLMwIgeRDMQBiO+5JBF3NjQ4BL8ueMo/0n15h9AU2O6cFCZDM="}]},"_npmUser":{"name":"caleblawson","email":"caleb.lawson@dynapt.com","actor":{"name":"caleblawson","email":"caleb.lawson@dynapt.com","type":"user"}},"directories":{},"maintainers":[{"name":"caleblawson","email":"caleb.lawson@dynapt.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/cloudflare-d1_0.11.1-alpha.0_1750619798556_0.1066640893420363"},"_hasShrinkwrap":false}},"time":{"created":"2025-06-22T19:16:38.431Z","0.11.1-alpha.0":"2025-06-22T19:16:38.766Z","modified":"2025-06-22T19:16:39.084Z"},"maintainers":[{"name":"caleblawson","email":"caleb.lawson@dynapt.com"}],"description":"D1 provider for Mastra - includes db storage capabilities","readme":"# @mastra/cloudflare-d1\r\n\r\nA Mastra store for Cloudflare D1 SQL databases, supporting threads, messages, workflows, evaluations, and traces with robust SQL features.\r\n\r\n## Features\r\n\r\n- Thread and message storage using SQL tables\r\n- True sorted order and filtering via SQL queries\r\n- Rich metadata support (JSON-encoded)\r\n- Timestamp tracking for all records\r\n- Workflow snapshot persistence\r\n- Trace and evaluation storage\r\n- Efficient batch operations (with prepared statements)\r\n- Automatic JSON serialization/deserialization for metadata and custom fields\r\n- Error handling and logging for all operations\r\n- Supports both Cloudflare Workers D1 Binding and REST API\r\n\r\n## Prerequisites\r\n\r\n- Access to a Cloudflare account with D1 enabled\r\n- D1 database created and configured\r\n- For Workers binding: Worker configured with D1 binding\r\n- For REST API: Cloudflare API Token with D1 permissions\r\n\r\n## Installation\r\n\r\n```bash\r\npnpm add @mastra/cloudflare-d1\r\n```\r\n\r\n## Usage\r\n\r\n### With Workers D1 Binding\r\n\r\n```typescript\r\nimport { D1Store } from '@mastra/cloudflare-d1';\r\n\r\nconst store = new D1Store({\r\n  binding: env.DB, // D1Database binding from Worker environment\r\n  tablePrefix: 'mastra_', // optional\r\n});\r\n```\r\n\r\n### With REST API\r\n\r\n```typescript\r\nimport { D1Store } from '@mastra/cloudflare-d1';\r\n\r\nconst store = new D1Store({\r\n  accountId: '<your-account-id>',\r\n  databaseId: '<your-d1-database-id>',\r\n  apiToken: '<your-api-token>',\r\n  tablePrefix: 'mastra_', // optional\r\n});\r\n```\r\n\r\n## Supported Methods\r\n\r\n### Thread Operations\r\n\r\n- `saveThread(thread)`: Create or update a thread\r\n- `getThreadById({ threadId })`: Get a thread by ID\r\n- `getThreadsByResourceId({ resourceId })`: Fetch all threads associated with a resource.\r\n- `updateThread({ id, title, metadata })`: Update the title and/or metadata of a thread.\r\n- `deleteThread({ threadId })`: Delete a thread and all its messages.\r\n\r\n### Message Operations\r\n\r\n- `saveMessages({ messages })`: Save multiple messages in a batch operation (uses prepared statements).\r\n- `getMessages({ threadId, selectBy? })`: Retrieve messages for a thread, with optional filtering (e.g., last N, include surrounding messages).\r\n\r\n### Workflow Operations\r\n\r\n- `persistWorkflowSnapshot({ workflowName, runId, snapshot })`: Save workflow state for a given workflow/run.\r\n- `loadWorkflowSnapshot({ workflowName, runId })`: Load persisted workflow state.\r\n\r\n### Trace/Evaluation Operations\r\n\r\n- `getTraces({ name?, scope?, page, perPage, attributes? })`: Query trace records with optional filters and pagination.\r\n- `getEvalsByAgentName({ agentName, type? })`: Query evaluation results by agent name.\r\n\r\n### Utility\r\n\r\n- `clearTable({ tableName })`: Remove all records from a logical table.\r\n- `batchInsert({ tableName, records })`: Batch insert multiple records.\r\n- `insert({ tableName, record })`: Insert a single record into a table.\r\n\r\n---\r\n\r\n## Data Types\r\n\r\nThe D1 store supports the following data types:\r\n\r\n- `text`: String\r\n- `timestamp`: ISO8601 string (converted to/from Date)\r\n- `uuid`: String\r\n- `jsonb`: JSON-encoded object\r\n- `integer`: Integer (for internal counters, etc)\r\n\r\nAll metadata and custom fields are automatically serialized/deserialized as JSON.\r\n\r\n---\r\n\r\n## Configuration Reference\r\n\r\n| Option      | Type       | Description                          |\r\n| ----------- | ---------- | ------------------------------------ |\r\n| binding     | D1Database | D1 Workers binding (for Workers)     |\r\n| accountId   | string     | Cloudflare Account ID (for REST API) |\r\n| databaseId  | string     | D1 Database ID (for REST API)        |\r\n| apiToken    | string     | Cloudflare API Token (for REST API)  |\r\n| tablePrefix | string     | Optional prefix for all table names  |\r\n\r\n---\r\n\r\n## Table/Namespace Mapping\r\n\r\nEach logical Mastra table maps to a SQL table in D1 (with optional prefix):\r\n\r\n- `mastra_threads` — stores threads\r\n- `mastra_messages` — stores messages\r\n- `mastra_workflow_snapshot` — stores workflow snapshots\r\n- `mastra_evals` — stores evaluations\r\n- `mastra_traces` — stores traces\r\n\r\n(The prefix is configurable via `tablePrefix`.)\r\n\r\n---\r\n\r\n## Limitations\r\n\r\n- No multi-statement transactions (D1 currently supports single statements per query)\r\n- No advanced SQL joins (D1 is SQLite-based, but some features may be limited)\r\n- Batch operations are processed in chunks, not truly atomic\r\n- Some REST API operations may be slower than Workers binding\r\n- D1 is in beta and may have evolving limitations\r\n- No vector search capabilities\r\n- Note: D1 has specific limitations and behaviors, please refer to the official Cloudflare D1 documentation for more information.\r\n\r\n## Cleanup / Disconnect\r\n\r\nNo explicit cleanup is required. Connections are managed by the platform.\r\n","readmeFilename":"README.md","_rev":"1-f1030290b7ced9343122f3bfed4653f8"}