@mastra/mysql 0.0.0 → 0.1.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 ADDED
@@ -0,0 +1,47 @@
1
+ # @mastra/mysql
2
+
3
+ ## 0.1.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 [[`fa63872`](https://github.com/mastra-ai/mastra/commit/fa6387280954e6b667bec5714b55ba082bc627ff), [`d779de3`](https://github.com/mastra-ai/mastra/commit/d779de3cd9d2e7ed8110547190e2f15e786a0e41), [`1750c97`](https://github.com/mastra-ai/mastra/commit/1750c975d6179fbf6db2813b15229d4f8f23fc55), [`9283971`](https://github.com/mastra-ai/mastra/commit/928397157009b4aef4d5fdf3a0a273cb371beb55), [`f07b646`](https://github.com/mastra-ai/mastra/commit/f07b64604ab7d25391179790b7fd4823df9e2dff), [`d8838ae`](https://github.com/mastra-ai/mastra/commit/d8838ae80b69780361693d27098f7f6684af12fe), [`40f9297`](https://github.com/mastra-ai/mastra/commit/40f9297003b921c62373d3e8d3a4bda76c9f6de3), [`19a8658`](https://github.com/mastra-ai/mastra/commit/19a86589c788ef48bb6c1b0612cc82a201857379), [`850af77`](https://github.com/mastra-ai/mastra/commit/850af7779cb87c350804488734544a5b1843de25), [`0f0d1ba`](https://github.com/mastra-ai/mastra/commit/0f0d1ba67bfcb2204e571401662f1eceefc03357), [`a18775a`](https://github.com/mastra-ai/mastra/commit/a18775a693172546ee2378d39b67d4e32895b251), [`1baf2d1`](https://github.com/mastra-ai/mastra/commit/1baf2d152c6881338ff8f114633d5316fe13dd15), [`8c31bcd`](https://github.com/mastra-ai/mastra/commit/8c31bcdb00e597880d5939b1b7d7566fbe5dacae), [`0e32507`](https://github.com/mastra-ai/mastra/commit/0e32507962cdfa5569b7bda5bc6fb3dd34e40b03), [`95b14cd`](https://github.com/mastra-ai/mastra/commit/95b14cdd820e86d97ac05fe568424c513a252e31), [`07c3de7`](https://github.com/mastra-ai/mastra/commit/07c3de7f7bc418beccaea3b5e6b7f7cdda79d492), [`0bf2d93`](https://github.com/mastra-ai/mastra/commit/0bf2d932d20e2936f2d9abb8c0a86e24fbc97ec6), [`7b0d34c`](https://github.com/mastra-ai/mastra/commit/7b0d34cfe4a2fce22ac86ae17404685ff67a2ddb), [`a659a77`](https://github.com/mastra-ai/mastra/commit/a659a779bdebe3a52a518c56d2260592d0240fe0), [`aa36be2`](https://github.com/mastra-ai/mastra/commit/aa36be23aa513b7dc53cb8ca16b7fab8f20e43ad), [`3332be9`](https://github.com/mastra-ai/mastra/commit/3332be9701ecd77aba840959d9a1d1ce7aef02d3), [`212c635`](https://github.com/mastra-ai/mastra/commit/212c635203e61d036ab41db8ff86c3893dc795b3), [`d8838ae`](https://github.com/mastra-ai/mastra/commit/d8838ae80b69780361693d27098f7f6684af12fe), [`9aa5a73`](https://github.com/mastra-ai/mastra/commit/9aa5a73e7e110f6e9365eec69364a33d5f03bb56), [`f73c789`](https://github.com/mastra-ai/mastra/commit/f73c789e8ef21561580395d2c410119cab5848c8), [`8bd16da`](https://github.com/mastra-ai/mastra/commit/8bd16da73a4cb874d739373643dbd6a6e7f88684), [`c8630f8`](https://github.com/mastra-ai/mastra/commit/c8630f80d4f40cb5d22e60ab162b618b1907167a), [`94dfef6`](https://github.com/mastra-ai/mastra/commit/94dfef6e2bf19a88467ea3940afcbce88a433f0f), [`47f71dc`](https://github.com/mastra-ai/mastra/commit/47f71dc6fbcbd12d71e21a979e676e20a02bd77d), [`50ceae2`](https://github.com/mastra-ai/mastra/commit/50ceae270878e2f8fb2b2c6c2faab09df0007c8a), [`a122f79`](https://github.com/mastra-ai/mastra/commit/a122f79427ae225ec79c7b2ed46278da48d04b17), [`8cdde58`](https://github.com/mastra-ai/mastra/commit/8cdde5875bbba6702d9df226f2b20232b8d75d6c), [`3a081c1`](https://github.com/mastra-ai/mastra/commit/3a081c1255c5ae8c99f6dad91cc612934ef6f2bd), [`49f8abc`](https://github.com/mastra-ai/mastra/commit/49f8abce8258e4f2f87bd326acfbdb641264a47c), [`847ff1e`](https://github.com/mastra-ai/mastra/commit/847ff1e0d94368d94b2e173e4e0908e115568ef3), [`0c1ed1d`](https://github.com/mastra-ai/mastra/commit/0c1ed1d00c7d87b5ac99ca95896211a2fa9189fa), [`259d409`](https://github.com/mastra-ai/mastra/commit/259d409a514174299dbde1ff5e1121209b3ba850), [`9e16c68`](https://github.com/mastra-ai/mastra/commit/9e16c6818b6485ccb43df28aba6f3a2219d28662), [`cefca33`](https://github.com/mastra-ai/mastra/commit/cefca33ae666e69810c935fedf95a929c173d1d7), [`d00e8c5`](https://github.com/mastra-ai/mastra/commit/d00e8c50daebe5bce5bf2f48bde39c86fc3d2fe4), [`36fa7e2`](https://github.com/mastra-ai/mastra/commit/36fa7e24d14e58a1eb46147097b32f583e5b8775), [`87e9774`](https://github.com/mastra-ai/mastra/commit/87e97741c1e493cd6d62f478eb810b49bda4d57c), [`65a72e7`](https://github.com/mastra-ai/mastra/commit/65a72e70c25eedea8ff985a6624b96be2850236b), [`fe9eacd`](https://github.com/mastra-ai/mastra/commit/fe9eacd9545a0a9d64aad31c9fa90294a425289e), [`4c02027`](https://github.com/mastra-ai/mastra/commit/4c020277235eaa6b1dc957c90ad0639eef213992), [`0f77241`](https://github.com/mastra-ai/mastra/commit/0f7724108806703799a8ba80ad0f09414afd5066), [`849efb9`](https://github.com/mastra-ai/mastra/commit/849efb9fca6dc976589c1f90a303fea618769109), [`92ff509`](https://github.com/mastra-ai/mastra/commit/92ff5098ef8a990438ca038077021a5f7541ec1d), [`3fce5e7`](https://github.com/mastra-ai/mastra/commit/3fce5e70d011d289043e75003ef3336ed4aa43c3), [`a763592`](https://github.com/mastra-ai/mastra/commit/a763592c3db46963ef1011cfe16fe372816e775e), [`db79c86`](https://github.com/mastra-ai/mastra/commit/db79c86c60723d57e02f9636ca2611bd4515f194), [`6855012`](https://github.com/mastra-ai/mastra/commit/685501247cc4717506f3e89beed03509d63a5370), [`80c7737`](https://github.com/mastra-ai/mastra/commit/80c7737e32d7917b5f356957d67c169d01744fd3), [`7fef31c`](https://github.com/mastra-ai/mastra/commit/7fef31c0d2a6d362a43a647a8a4f6ab893758a23), [`7fef31c`](https://github.com/mastra-ai/mastra/commit/7fef31c0d2a6d362a43a647a8a4f6ab893758a23), [`3f1cf47`](https://github.com/mastra-ai/mastra/commit/3f1cf476f74c1e4cc2df908837e05853a5347e31)]:
24
+ - @mastra/core@1.38.0
25
+
26
+ ## 0.1.0-alpha.0
27
+
28
+ ### Minor Changes
29
+
30
+ - 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))
31
+
32
+ ```ts
33
+ import { MySQLStore } from '@mastra/mysql';
34
+
35
+ const store = new MySQLStore({
36
+ connectionString: 'mysql://user:password@localhost:3306/mastra',
37
+ });
38
+ ```
39
+
40
+ This release also makes table and index setup reliable on a brand-new database:
41
+ - 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.
42
+ - 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.
43
+
44
+ ### Patch Changes
45
+
46
+ - 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)]:
47
+ - @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 provider for Mastra.
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
- ## Stable v1.5 Compatibility
5
+ ## Installation
6
6
 
7
- This version has been adapted to meet the stable v1.5 contract for Mastra storage adapters. It supports:
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
- - Full observability/tracing (spans, traces)
10
- - Workflow run management
11
- - Thread and message persistence
12
- - Scoring and evaluation storage
55
+ ### With Mastra
13
56
 
14
- To verify compatibility:
57
+ ```typescript
58
+ import { Mastra } from '@mastra/core';
59
+ import { MySQLStore } from '@mastra/mysql';
15
60
 
16
- ```bash
17
- pnpm exec tsc --noEmit
18
- pnpm test src/storage/index.unit.test.ts
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
- ## Running the test suite
68
+ ## Configuration
22
69
 
23
- ### Unit tests (no database required)
70
+ `MySQLStore` supports two connection methods:
24
71
 
25
- Run unit tests that don't require a database connection:
72
+ **1. Connection String**
26
73
 
27
- ```bash
28
- # Run all unit tests
29
- pnpm test src/storage/domains/utils.test.ts src/storage/index.unit.test.ts
74
+ ```typescript
75
+ const store = new MySQLStore({
76
+ connectionString: 'mysql://user:password@localhost:3306/mastra',
77
+ });
78
+ ```
30
79
 
31
- # Or run just the unit test file
32
- pnpm test src/storage/index.unit.test.ts
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
- ### Integration tests (requires Docker)
92
+ ### Optional Configuration
36
93
 
37
- The integration test suite uses Docker to run a MySQL instance:
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
- ```bash
40
- # Ensure Docker is running, then run all tests including integration tests
41
- pnpm test
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
- This will automatically start a MySQL container, run the tests, and clean up afterward.
126
+ The test suite reads the following variables to connect to MySQL, falling back to the defaults shown:
45
127
 
46
- ## Environment Variables
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
- You can override the default database connection settings:
134
+ ### Running Tests
49
135
 
50
- - `MYSQL_HOST` (default: localhost)
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
- ## Rollback Procedure
138
+ ```bash
139
+ pnpm test src/storage/index.unit.test.ts
140
+ ```
57
141
 
58
- If issues arise with this version, follow these steps to rollback:
142
+ Integration tests use Docker to start a MySQL instance, run the suite, and clean up afterward:
59
143
 
60
- 1. Checkout the last known good commit:
61
- ```bash
62
- git checkout <last-known-good-commit>
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
- ## Notes
149
+ ## Related Links
75
150
 
76
- - Integration tests require Docker to be running
77
- - The test suite includes both unit tests and integration tests
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 `${quoteIdentifier(colName, "column name")} ${modifiers.join(" ")}`;
435
+ return `${indexColumn(colName)} ${modifiers.join(" ")}`;
413
436
  }
414
- return quoteIdentifier(col, "column name");
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) throw new Error(`MCP client with id ${id} not found`);
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;