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