@mastra/mysql 0.0.0 → 0.1.0-alpha.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 +24 -0
- package/LICENSE.md +30 -0
- package/README.md +126 -55
- package/dist/index.cjs +34 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +34 -16
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/blobs/index.d.ts +1 -1
- package/dist/storage/domains/blobs/index.d.ts.map +1 -1
- package/dist/storage/domains/favorites/index.d.ts.map +1 -1
- package/dist/storage/domains/mcp-clients/index.d.ts.map +1 -1
- package/dist/storage/domains/operations/index.d.ts.map +1 -1
- package/package.json +20 -21
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @mastra/mysql
|
|
2
|
+
|
|
3
|
+
## 0.1.0-alpha.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Added the MySQL storage adapter for Mastra. Use it as a storage backend with the same domain coverage as the other first-party adapters (memory, threads, workflows, observability, agents, and more). ([#17446](https://github.com/mastra-ai/mastra/pull/17446))
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { MySQLStore } from '@mastra/mysql';
|
|
11
|
+
|
|
12
|
+
const store = new MySQLStore({
|
|
13
|
+
connectionString: 'mysql://user:password@localhost:3306/mastra',
|
|
14
|
+
});
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
This release also makes table and index setup reliable on a brand-new database:
|
|
18
|
+
- Fixed store initialization failing on a fresh database. Idempotency for favorites is now enforced by the table's primary key instead of a separate index that MySQL rejected, which previously aborted setup and left the connection pool unusable.
|
|
19
|
+
- Fixed default performance indexes silently failing to be created. Indexes on text columns now include a key-length prefix so they are created instead of skipped.
|
|
20
|
+
|
|
21
|
+
### Patch Changes
|
|
22
|
+
|
|
23
|
+
- Updated dependencies [[`19a8658`](https://github.com/mastra-ai/mastra/commit/19a86589c788ef48bb6c1b0612cc82a201857379), [`a659a77`](https://github.com/mastra-ai/mastra/commit/a659a779bdebe3a52a518c56d2260592d0240fe0), [`3332be9`](https://github.com/mastra-ai/mastra/commit/3332be9701ecd77aba840959d9a1d1ce7aef02d3)]:
|
|
24
|
+
- @mastra/core@1.38.0-alpha.6
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
|
|
16
|
+
# Apache License 2.0
|
|
17
|
+
|
|
18
|
+
Copyright (c) 2025 Kepler Software, Inc.
|
|
19
|
+
|
|
20
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
21
|
+
you may not use this file except in compliance with the License.
|
|
22
|
+
You may obtain a copy of the License at
|
|
23
|
+
|
|
24
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
25
|
+
|
|
26
|
+
Unless required by applicable law or agreed to in writing, software
|
|
27
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
28
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
29
|
+
See the License for the specific language governing permissions and
|
|
30
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,81 +1,152 @@
|
|
|
1
1
|
# @mastra/mysql
|
|
2
2
|
|
|
3
|
-
MySQL storage
|
|
3
|
+
MySQL storage implementation for Mastra, providing persistent storage for threads, messages, workflows, traces, and more with connection pooling and transaction support.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
```bash
|
|
8
|
+
npm install @mastra/mysql
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Prerequisites
|
|
12
|
+
|
|
13
|
+
- MySQL 8.0 or higher
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { MySQLStore } from '@mastra/mysql';
|
|
19
|
+
|
|
20
|
+
const store = new MySQLStore({
|
|
21
|
+
connectionString: 'mysql://user:password@localhost:3306/mastra',
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Create a thread
|
|
25
|
+
await store.saveThread({
|
|
26
|
+
thread: {
|
|
27
|
+
id: 'thread-123',
|
|
28
|
+
resourceId: 'resource-456',
|
|
29
|
+
title: 'My Thread',
|
|
30
|
+
metadata: { key: 'value' },
|
|
31
|
+
createdAt: new Date(),
|
|
32
|
+
updatedAt: new Date(),
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Add messages to thread
|
|
37
|
+
await store.saveMessages({
|
|
38
|
+
messages: [
|
|
39
|
+
{
|
|
40
|
+
id: 'msg-789',
|
|
41
|
+
threadId: 'thread-123',
|
|
42
|
+
role: 'user',
|
|
43
|
+
content: { content: 'Hello' },
|
|
44
|
+
resourceId: 'resource-456',
|
|
45
|
+
createdAt: new Date(),
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Query threads and messages
|
|
51
|
+
const savedThread = await store.getThreadById({ threadId: 'thread-123' });
|
|
52
|
+
const messages = await store.listMessages({ threadId: 'thread-123' });
|
|
53
|
+
```
|
|
8
54
|
|
|
9
|
-
|
|
10
|
-
- Workflow run management
|
|
11
|
-
- Thread and message persistence
|
|
12
|
-
- Scoring and evaluation storage
|
|
55
|
+
### With Mastra
|
|
13
56
|
|
|
14
|
-
|
|
57
|
+
```typescript
|
|
58
|
+
import { Mastra } from '@mastra/core';
|
|
59
|
+
import { MySQLStore } from '@mastra/mysql';
|
|
15
60
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
61
|
+
export const mastra = new Mastra({
|
|
62
|
+
storage: new MySQLStore({
|
|
63
|
+
connectionString: 'mysql://user:password@localhost:3306/mastra',
|
|
64
|
+
}),
|
|
65
|
+
});
|
|
19
66
|
```
|
|
20
67
|
|
|
21
|
-
##
|
|
68
|
+
## Configuration
|
|
22
69
|
|
|
23
|
-
|
|
70
|
+
`MySQLStore` supports two connection methods:
|
|
24
71
|
|
|
25
|
-
|
|
72
|
+
**1. Connection String**
|
|
26
73
|
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
|
|
74
|
+
```typescript
|
|
75
|
+
const store = new MySQLStore({
|
|
76
|
+
connectionString: 'mysql://user:password@localhost:3306/mastra',
|
|
77
|
+
});
|
|
78
|
+
```
|
|
30
79
|
|
|
31
|
-
|
|
32
|
-
|
|
80
|
+
**2. Host/Port/Database**
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
const store = new MySQLStore({
|
|
84
|
+
host: 'localhost',
|
|
85
|
+
port: 3306,
|
|
86
|
+
user: 'mastra',
|
|
87
|
+
password: 'mastra',
|
|
88
|
+
database: 'mastra',
|
|
89
|
+
});
|
|
33
90
|
```
|
|
34
91
|
|
|
35
|
-
###
|
|
92
|
+
### Optional Configuration
|
|
36
93
|
|
|
37
|
-
|
|
94
|
+
- `ssl`: Enable SSL or provide custom SSL options (`true` | `false` | object)
|
|
95
|
+
- `max`: Maximum pool connections (default: `10`)
|
|
96
|
+
- `database`: Override the database name parsed from the connection string
|
|
97
|
+
- `waitForConnections`: Queue requests when the pool is full (default: `true`, host config only)
|
|
98
|
+
- `queueLimit`: Maximum queued connection requests, `0` for unlimited (default: `0`, host config only)
|
|
99
|
+
- `skipDefaultIndexes`: Skip creating the built-in performance indexes during setup (default: `false`)
|
|
100
|
+
- `indexes`: Additional custom indexes to create during setup
|
|
38
101
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
102
|
+
## Features
|
|
103
|
+
|
|
104
|
+
- Persistent storage for threads, messages, workflows, scores, datasets, and experiments
|
|
105
|
+
- Full observability/tracing storage (spans and traces)
|
|
106
|
+
- Atomic transactions for data consistency
|
|
107
|
+
- Efficient batch operations
|
|
108
|
+
- Connection pooling via `mysql2`
|
|
109
|
+
- Automatic table and index setup on first use
|
|
110
|
+
- Rich metadata support with JSON columns
|
|
111
|
+
- Timestamp tracking and cascading deletes
|
|
112
|
+
|
|
113
|
+
## Storage Methods
|
|
114
|
+
|
|
115
|
+
- `saveThread({ thread })`: Create or update a thread
|
|
116
|
+
- `getThreadById({ threadId })`: Get a thread by ID
|
|
117
|
+
- `deleteThread({ threadId })`: Delete a thread and its messages
|
|
118
|
+
- `saveMessages({ messages })`: Save multiple messages in a transaction
|
|
119
|
+
- `listMessages({ threadId, perPage?, page? })`: Get messages for a thread with pagination
|
|
120
|
+
- `deleteMessages(messageIds)`: Delete specific messages
|
|
121
|
+
|
|
122
|
+
## Development
|
|
123
|
+
|
|
124
|
+
### Environment Variables
|
|
43
125
|
|
|
44
|
-
|
|
126
|
+
The test suite reads the following variables to connect to MySQL, falling back to the defaults shown:
|
|
45
127
|
|
|
46
|
-
|
|
128
|
+
- `MYSQL_HOST` (default: `localhost`)
|
|
129
|
+
- `MYSQL_PORT` (default: `3306`)
|
|
130
|
+
- `MYSQL_USER` (default: `mastra`)
|
|
131
|
+
- `MYSQL_PASSWORD` (default: `mastra`)
|
|
132
|
+
- `MYSQL_DB` (default: `mastra`)
|
|
47
133
|
|
|
48
|
-
|
|
134
|
+
### Running Tests
|
|
49
135
|
|
|
50
|
-
|
|
51
|
-
- `MYSQL_PORT` (default: 3306)
|
|
52
|
-
- `MYSQL_USER` (default: mastra)
|
|
53
|
-
- `MYSQL_PASSWORD` (default: mastra)
|
|
54
|
-
- `MYSQL_DB` (default: mastra)
|
|
136
|
+
Unit tests run without a database:
|
|
55
137
|
|
|
56
|
-
|
|
138
|
+
```bash
|
|
139
|
+
pnpm test src/storage/index.unit.test.ts
|
|
140
|
+
```
|
|
57
141
|
|
|
58
|
-
|
|
142
|
+
Integration tests use Docker to start a MySQL instance, run the suite, and clean up afterward:
|
|
59
143
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
2. Reinstall dependencies:
|
|
65
|
-
```bash
|
|
66
|
-
pnpm install
|
|
67
|
-
```
|
|
68
|
-
3. Verify the previous state:
|
|
69
|
-
```bash
|
|
70
|
-
pnpm exec tsc --noEmit
|
|
71
|
-
pnpm test src/storage/index.unit.test.ts
|
|
72
|
-
```
|
|
144
|
+
```bash
|
|
145
|
+
# Ensure Docker is running
|
|
146
|
+
pnpm test
|
|
147
|
+
```
|
|
73
148
|
|
|
74
|
-
##
|
|
149
|
+
## Related Links
|
|
75
150
|
|
|
76
|
-
-
|
|
77
|
-
-
|
|
78
|
-
- Unit tests use mocked MySQL connections and run quickly without external dependencies
|
|
79
|
-
- Integration tests verify real database interactions and require a running MySQL instance
|
|
80
|
-
- AI tracing features (span creation, updates, pagination, and batch operations) are supported by the MySQL observability domain.
|
|
81
|
-
- Observability tables are initialized during store setup.
|
|
151
|
+
- [Mastra Storage Documentation](https://mastra.ai/docs)
|
|
152
|
+
- [MySQL Documentation](https://dev.mysql.com/doc/)
|
package/dist/index.cjs
CHANGED
|
@@ -406,12 +406,35 @@ var StoreOperationsMySQL = class extends storage.StoreOperations {
|
|
|
406
406
|
if (existing.length > 0) {
|
|
407
407
|
return;
|
|
408
408
|
}
|
|
409
|
+
const [columnMeta] = await this.pool.execute(
|
|
410
|
+
`SELECT COLUMN_NAME, DATA_TYPE FROM information_schema.COLUMNS
|
|
411
|
+
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?`,
|
|
412
|
+
[db, table]
|
|
413
|
+
);
|
|
414
|
+
const dataTypeByColumn = new Map(
|
|
415
|
+
columnMeta.map((row) => [String(row.COLUMN_NAME).toLowerCase(), String(row.DATA_TYPE).toLowerCase()])
|
|
416
|
+
);
|
|
417
|
+
const PREFIX_TYPES = /* @__PURE__ */ new Set([
|
|
418
|
+
"tinytext",
|
|
419
|
+
"text",
|
|
420
|
+
"mediumtext",
|
|
421
|
+
"longtext",
|
|
422
|
+
"blob",
|
|
423
|
+
"tinyblob",
|
|
424
|
+
"mediumblob",
|
|
425
|
+
"longblob"
|
|
426
|
+
]);
|
|
427
|
+
const indexColumn = (colName) => {
|
|
428
|
+
const quoted = quoteIdentifier(colName, "column name");
|
|
429
|
+
const dataType = dataTypeByColumn.get(colName.toLowerCase());
|
|
430
|
+
return dataType && PREFIX_TYPES.has(dataType) ? `${quoted}(191)` : quoted;
|
|
431
|
+
};
|
|
409
432
|
const columnsStr = columns.map((col) => {
|
|
410
433
|
if (col.includes(" DESC") || col.includes(" ASC")) {
|
|
411
434
|
const [colName, ...modifiers] = col.split(" ");
|
|
412
|
-
return `${
|
|
435
|
+
return `${indexColumn(colName)} ${modifiers.join(" ")}`;
|
|
413
436
|
}
|
|
414
|
-
return
|
|
437
|
+
return indexColumn(col);
|
|
415
438
|
}).join(", ");
|
|
416
439
|
const uniqueStr = unique ? "UNIQUE " : "";
|
|
417
440
|
const sql = `CREATE ${uniqueStr}INDEX ${indexName} ON ${tableName} (${columnsStr})`;
|
|
@@ -3490,19 +3513,6 @@ var FavoritesMySQL = class _FavoritesMySQL extends storage.FavoritesStorage {
|
|
|
3490
3513
|
tableName: storage.TABLE_FAVORITES,
|
|
3491
3514
|
schema: storage.FAVORITES_SCHEMA
|
|
3492
3515
|
});
|
|
3493
|
-
const tableName = formatTableName(storage.TABLE_FAVORITES);
|
|
3494
|
-
const [idxCheck] = await this.pool.execute(
|
|
3495
|
-
`SELECT INDEX_NAME FROM information_schema.STATISTICS
|
|
3496
|
-
WHERE TABLE_SCHEMA = DATABASE()
|
|
3497
|
-
AND TABLE_NAME = ?
|
|
3498
|
-
AND INDEX_NAME = 'unique_favorite'`,
|
|
3499
|
-
[storage.TABLE_FAVORITES]
|
|
3500
|
-
);
|
|
3501
|
-
if (idxCheck.length === 0) {
|
|
3502
|
-
await this.pool.execute(
|
|
3503
|
-
`CREATE UNIQUE INDEX unique_favorite ON ${tableName} (${quoteIdentifier("userId", "index column")}(255), ${quoteIdentifier("entityType", "index column")}(255), ${quoteIdentifier("entityId", "index column")}(255))`
|
|
3504
|
-
);
|
|
3505
|
-
}
|
|
3506
3516
|
await this.createDefaultIndexes();
|
|
3507
3517
|
await this.createCustomIndexes();
|
|
3508
3518
|
}
|
|
@@ -3889,7 +3899,15 @@ var MCPClientsMySQL = class _MCPClientsMySQL extends storage.MCPClientsStorage {
|
|
|
3889
3899
|
const { id, ...updates } = input;
|
|
3890
3900
|
try {
|
|
3891
3901
|
const existing = await this.getById(id);
|
|
3892
|
-
if (!existing)
|
|
3902
|
+
if (!existing) {
|
|
3903
|
+
throw new error.MastraError({
|
|
3904
|
+
id: storage.createStorageErrorId("MYSQL", "UPDATE_MCP_CLIENT", "NOT_FOUND"),
|
|
3905
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3906
|
+
category: error.ErrorCategory.USER,
|
|
3907
|
+
text: `MCP client with id ${id} not found`,
|
|
3908
|
+
details: { id }
|
|
3909
|
+
});
|
|
3910
|
+
}
|
|
3893
3911
|
const { authorId, activeVersionId, metadata, status } = updates;
|
|
3894
3912
|
const updateData = { updatedAt: /* @__PURE__ */ new Date() };
|
|
3895
3913
|
if (authorId !== void 0) updateData.authorId = authorId;
|