@mastra/mysql 0.8.7 → 0.9.0-alpha.1

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.
@@ -3,7 +3,7 @@ name: mastra-mysql
3
3
  description: Documentation for @mastra/mysql. Use when working with @mastra/mysql APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/mysql"
6
- version: "0.8.7"
6
+ version: "0.9.0-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.8.7",
2
+ "version": "0.9.0-alpha.1",
3
3
  "package": "@mastra/mysql",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -2415,23 +2415,6 @@ var DatasetsMySQL = class DatasetsMySQL extends _mastra_core_storage.DatasetsSto
2415
2415
  }
2416
2416
  async _doUpdateItem(args) {
2417
2417
  this.#rejectToolMocks(args.toolMocks);
2418
- const existing = await this.getItemById({ id: args.id });
2419
- if (!existing) throw new _mastra_core_error.MastraError({
2420
- id: "MYSQL_UPDATE_ITEM_NOT_FOUND",
2421
- domain: _mastra_core_error.ErrorDomain.STORAGE,
2422
- category: _mastra_core_error.ErrorCategory.USER,
2423
- details: { itemId: args.id }
2424
- });
2425
- if (existing.datasetId !== args.datasetId) throw new _mastra_core_error.MastraError({
2426
- id: "MYSQL_UPDATE_ITEM_DATASET_MISMATCH",
2427
- domain: _mastra_core_error.ErrorDomain.STORAGE,
2428
- category: _mastra_core_error.ErrorCategory.USER,
2429
- details: {
2430
- itemId: args.id,
2431
- expectedDatasetId: args.datasetId,
2432
- actualDatasetId: existing.datasetId
2433
- }
2434
- });
2435
2418
  const connection = await this.pool.getConnection();
2436
2419
  try {
2437
2420
  await connection.beginTransaction();
@@ -2440,6 +2423,36 @@ var DatasetsMySQL = class DatasetsMySQL extends _mastra_core_storage.DatasetsSto
2440
2423
  const tableDatasetsName = formatTableName(_mastra_core_storage.TABLE_DATASETS);
2441
2424
  const tableItemsName = formatTableName(_mastra_core_storage.TABLE_DATASET_ITEMS);
2442
2425
  const tableVersionsName = formatTableName(_mastra_core_storage.TABLE_DATASET_VERSIONS);
2426
+ await connection.execute(`SELECT id FROM ${tableDatasetsName} WHERE id = ? FOR UPDATE`, [args.datasetId]);
2427
+ const [itemRows] = await connection.execute(`SELECT * FROM ${tableItemsName} WHERE id = ? AND validTo IS NULL AND isDeleted = 0`, [args.id]);
2428
+ const itemRow = itemRows[0];
2429
+ const existing = itemRow ? this.mapItem(itemRow) : null;
2430
+ if (!existing) throw new _mastra_core_error.MastraError({
2431
+ id: "MYSQL_UPDATE_ITEM_NOT_FOUND",
2432
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
2433
+ category: _mastra_core_error.ErrorCategory.USER,
2434
+ details: { itemId: args.id }
2435
+ });
2436
+ if (existing.datasetId !== args.datasetId) throw new _mastra_core_error.MastraError({
2437
+ id: "MYSQL_UPDATE_ITEM_DATASET_MISMATCH",
2438
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
2439
+ category: _mastra_core_error.ErrorCategory.USER,
2440
+ details: {
2441
+ itemId: args.id,
2442
+ expectedDatasetId: args.datasetId,
2443
+ actualDatasetId: existing.datasetId
2444
+ }
2445
+ });
2446
+ if (existing.metadata?.__purged === true) throw new _mastra_core_error.MastraError({
2447
+ id: "DATASET_ITEM_PURGED",
2448
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
2449
+ category: _mastra_core_error.ErrorCategory.USER,
2450
+ details: {
2451
+ datasetId: args.datasetId,
2452
+ itemId: args.id
2453
+ },
2454
+ text: `Purged dataset item cannot be updated: ${args.id}`
2455
+ });
2443
2456
  const mergedInput = args.input ?? existing.input;
2444
2457
  const mergedGroundTruth = args.groundTruth ?? existing.groundTruth;
2445
2458
  const mergedExpectedTrajectory = args.expectedTrajectory ?? existing.expectedTrajectory;
@@ -2511,18 +2524,6 @@ var DatasetsMySQL = class DatasetsMySQL extends _mastra_core_storage.DatasetsSto
2511
2524
  }
2512
2525
  }
2513
2526
  async _doDeleteItem({ id, datasetId }) {
2514
- const existing = await this.getItemById({ id });
2515
- if (!existing) return;
2516
- if (existing.datasetId !== datasetId) throw new _mastra_core_error.MastraError({
2517
- id: "MYSQL_DELETE_ITEM_DATASET_MISMATCH",
2518
- domain: _mastra_core_error.ErrorDomain.STORAGE,
2519
- category: _mastra_core_error.ErrorCategory.USER,
2520
- details: {
2521
- itemId: id,
2522
- expectedDatasetId: datasetId,
2523
- actualDatasetId: existing.datasetId
2524
- }
2525
- });
2526
2527
  const connection = await this.pool.getConnection();
2527
2528
  try {
2528
2529
  await connection.beginTransaction();
@@ -2531,6 +2532,24 @@ var DatasetsMySQL = class DatasetsMySQL extends _mastra_core_storage.DatasetsSto
2531
2532
  const tableDatasetsName = formatTableName(_mastra_core_storage.TABLE_DATASETS);
2532
2533
  const tableItemsName = formatTableName(_mastra_core_storage.TABLE_DATASET_ITEMS);
2533
2534
  const tableVersionsName = formatTableName(_mastra_core_storage.TABLE_DATASET_VERSIONS);
2535
+ await connection.execute(`SELECT id FROM ${tableDatasetsName} WHERE id = ? FOR UPDATE`, [datasetId]);
2536
+ const [itemRows] = await connection.execute(`SELECT * FROM ${tableItemsName} WHERE id = ? AND validTo IS NULL AND isDeleted = 0`, [id]);
2537
+ const itemRow = itemRows[0];
2538
+ const existing = itemRow ? this.mapItem(itemRow) : null;
2539
+ if (!existing) {
2540
+ await connection.commit();
2541
+ return;
2542
+ }
2543
+ if (existing.datasetId !== datasetId) throw new _mastra_core_error.MastraError({
2544
+ id: "MYSQL_DELETE_ITEM_DATASET_MISMATCH",
2545
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
2546
+ category: _mastra_core_error.ErrorCategory.USER,
2547
+ details: {
2548
+ itemId: id,
2549
+ expectedDatasetId: datasetId,
2550
+ actualDatasetId: existing.datasetId
2551
+ }
2552
+ });
2534
2553
  await connection.execute(`UPDATE ${tableDatasetsName} SET \`version\` = \`version\` + 1 WHERE id = ?`, [datasetId]);
2535
2554
  const [datasetRows] = await connection.execute(`SELECT \`version\`, \`organizationId\`, \`projectId\` FROM ${tableDatasetsName} WHERE id = ?`, [datasetId]);
2536
2555
  const parentRow = datasetRows[0];
@@ -2899,12 +2918,6 @@ var DatasetsMySQL = class DatasetsMySQL extends _mastra_core_storage.DatasetsSto
2899
2918
  category: _mastra_core_error.ErrorCategory.USER,
2900
2919
  details: { datasetId: input.datasetId }
2901
2920
  });
2902
- const currentItems = [];
2903
- for (const itemId of input.itemIds) {
2904
- const item = await this.getItemById({ id: itemId });
2905
- if (item && item.datasetId === input.datasetId) currentItems.push(item);
2906
- }
2907
- if (currentItems.length === 0) return;
2908
2921
  const connection = await this.pool.getConnection();
2909
2922
  try {
2910
2923
  await connection.beginTransaction();
@@ -2913,6 +2926,18 @@ var DatasetsMySQL = class DatasetsMySQL extends _mastra_core_storage.DatasetsSto
2913
2926
  const tableDatasetsName = formatTableName(_mastra_core_storage.TABLE_DATASETS);
2914
2927
  const tableItemsName = formatTableName(_mastra_core_storage.TABLE_DATASET_ITEMS);
2915
2928
  const tableVersionsName = formatTableName(_mastra_core_storage.TABLE_DATASET_VERSIONS);
2929
+ await connection.execute(`SELECT id FROM ${tableDatasetsName} WHERE id = ? FOR UPDATE`, [input.datasetId]);
2930
+ const currentItems = [];
2931
+ for (const itemId of input.itemIds) {
2932
+ const [itemRows] = await connection.execute(`SELECT * FROM ${tableItemsName} WHERE id = ? AND validTo IS NULL AND isDeleted = 0`, [itemId]);
2933
+ const row = itemRows[0];
2934
+ const item = row ? this.mapItem(row) : null;
2935
+ if (item && item.datasetId === input.datasetId) currentItems.push(item);
2936
+ }
2937
+ if (currentItems.length === 0) {
2938
+ await connection.commit();
2939
+ return;
2940
+ }
2916
2941
  await connection.execute(`UPDATE ${tableDatasetsName} SET \`version\` = \`version\` + 1 WHERE id = ?`, [input.datasetId]);
2917
2942
  const [versionRows] = await connection.execute(`SELECT \`version\` FROM ${tableDatasetsName} WHERE id = ?`, [input.datasetId]);
2918
2943
  const newVersion = versionRows[0]?.version;
@@ -6350,6 +6375,62 @@ var MemoryMySQL = class MemoryMySQL extends _mastra_core_storage.MemoryStorage {
6350
6375
  }, error);
6351
6376
  }
6352
6377
  }
6378
+ /**
6379
+ * Atomically reassign a thread and all of its messages to a different resource.
6380
+ *
6381
+ * Runs inside a single transaction and takes a `SELECT ... FOR UPDATE` row lock on the
6382
+ * thread, so overlapping transfers of the same thread serialize and can never interleave
6383
+ * the thread update with the message update. Either both the thread and every message move
6384
+ * to the new resource, or neither does — there is no split-ownership window. The thread's
6385
+ * `createdAt` is preserved. Callers are responsible for authorizing the reassignment.
6386
+ */
6387
+ async updateThreadResourceId({ threadId, resourceId }) {
6388
+ const connection = await this.pool.getConnection();
6389
+ try {
6390
+ await connection.beginTransaction();
6391
+ const [rows] = await connection.execute(`SELECT * FROM ${formatTableName(_mastra_core_storage.TABLE_THREADS)} WHERE ${quoteIdentifier("id", "column name")} = ? FOR UPDATE`, [threadId]);
6392
+ const row = rows[0];
6393
+ if (!row) throw new _mastra_core_error.MastraError({
6394
+ id: (0, _mastra_core_storage.createStorageErrorId)("MYSQL", "UPDATE_THREAD_RESOURCE_ID", "NOT_FOUND"),
6395
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
6396
+ category: _mastra_core_error.ErrorCategory.USER,
6397
+ text: `Thread "${threadId}" not found`,
6398
+ details: { threadId }
6399
+ });
6400
+ const thread = this.mapThread(row);
6401
+ if (thread.resourceId === resourceId) {
6402
+ await connection.commit();
6403
+ return thread;
6404
+ }
6405
+ const updatedAt = /* @__PURE__ */ new Date();
6406
+ await connection.execute(`UPDATE ${formatTableName(_mastra_core_storage.TABLE_THREADS)} SET ${quoteIdentifier("resourceId", "column name")} = ?, ${quoteIdentifier("updatedAt", "column name")} = ? WHERE ${quoteIdentifier("id", "column name")} = ?`, [
6407
+ resourceId,
6408
+ transformToSqlValue(updatedAt),
6409
+ threadId
6410
+ ]);
6411
+ await connection.execute(`UPDATE ${formatTableName(_mastra_core_storage.TABLE_MESSAGES)} SET ${quoteIdentifier("resourceId", "column name")} = ? WHERE ${quoteIdentifier("thread_id", "column name")} = ?`, [resourceId, threadId]);
6412
+ await connection.commit();
6413
+ return {
6414
+ ...thread,
6415
+ resourceId,
6416
+ updatedAt
6417
+ };
6418
+ } catch (error) {
6419
+ await connection.rollback();
6420
+ if (error instanceof _mastra_core_error.MastraError) throw error;
6421
+ throw new _mastra_core_error.MastraError({
6422
+ id: (0, _mastra_core_storage.createStorageErrorId)("MYSQL", "UPDATE_THREAD_RESOURCE_ID", "FAILED"),
6423
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
6424
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
6425
+ details: {
6426
+ threadId,
6427
+ resourceId
6428
+ }
6429
+ }, error);
6430
+ } finally {
6431
+ connection.release();
6432
+ }
6433
+ }
6353
6434
  async listThreads(args) {
6354
6435
  const { page = 0, perPage: perPageInput, orderBy, filter } = args;
6355
6436
  const { field, direction } = this.parseOrderBy(orderBy, "DESC");