@mastra/mysql 0.14.0-alpha.1 → 0.14.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/dist/index.js CHANGED
@@ -85,13 +85,14 @@ function prepareWhereClause(filters) {
85
85
  if (value === true) conditions.push(`${quoteIdentifier(fieldName, "column name")} IS NULL`);
86
86
  else if (value === false) conditions.push(`${quoteIdentifier(fieldName, "column name")} IS NOT NULL`);
87
87
  } else if (value === null) conditions.push(`${quoteIdentifier(key, "column name")} IS NULL`);
88
- else if (Array.isArray(value)) if (value.length === 0) conditions.push("FALSE");
89
- else {
90
- const placeholders = value.map(() => "?").join(", ");
91
- conditions.push(`${quoteIdentifier(key, "column name")} IN (${placeholders})`);
92
- args.push(...value.map(transformToSqlValue));
93
- }
94
- else {
88
+ else if (Array.isArray(value)) {
89
+ if (value.length === 0) conditions.push("FALSE");
90
+ else {
91
+ const placeholders = value.map(() => "?").join(", ");
92
+ conditions.push(`${quoteIdentifier(key, "column name")} IN (${placeholders})`);
93
+ args.push(...value.map(transformToSqlValue));
94
+ }
95
+ } else {
95
96
  conditions.push(`${quoteIdentifier(key, "column name")} = ?`);
96
97
  args.push(transformToSqlValue(value));
97
98
  }
@@ -156,13 +157,14 @@ function transformFromSqlRow({ tableName, sqlRow }) {
156
157
  const result = {};
157
158
  Object.entries(sqlRow).forEach(([key, value]) => {
158
159
  const columnSchema = schema?.[key];
159
- if (columnSchema?.type === "jsonb") if (typeof value === "string") try {
160
- result[key] = JSON.parse(value);
161
- } catch {
162
- result[key] = value;
163
- }
164
- else result[key] = value;
165
- else if (columnSchema?.type === "timestamp") result[key] = value === null ? null : parseDateTime(value);
160
+ if (columnSchema?.type === "jsonb") {
161
+ if (typeof value === "string") try {
162
+ result[key] = JSON.parse(value);
163
+ } catch {
164
+ result[key] = value;
165
+ }
166
+ else result[key] = value;
167
+ } else if (columnSchema?.type === "timestamp") result[key] = value === null ? null : parseDateTime(value);
166
168
  else if (columnSchema?.type === "boolean") result[key] = value === true || value === 1 || value === "1";
167
169
  else result[key] = value;
168
170
  });
@@ -1564,15 +1566,19 @@ var BackgroundTasksMySQL = class BackgroundTasksMySQL extends BackgroundTasksSto
1564
1566
  predicates.push(`${quoteIdentifier("status", "column name")} = ?`);
1565
1567
  params.push(options.expectedStatus);
1566
1568
  }
1567
- if (options?.expectedOwnerId !== void 0) if (options.expectedOwnerId === null) predicates.push(`${quoteIdentifier("ownerId", "column name")} IS NULL`);
1568
- else {
1569
- predicates.push(`${quoteIdentifier("ownerId", "column name")} = ?`);
1570
- params.push(options.expectedOwnerId);
1569
+ if (options?.expectedOwnerId !== void 0) {
1570
+ if (options.expectedOwnerId === null) predicates.push(`${quoteIdentifier("ownerId", "column name")} IS NULL`);
1571
+ else {
1572
+ predicates.push(`${quoteIdentifier("ownerId", "column name")} = ?`);
1573
+ params.push(options.expectedOwnerId);
1574
+ }
1571
1575
  }
1572
- if (options?.expectedLeaseExpiresAt !== void 0) if (options.expectedLeaseExpiresAt === null) predicates.push(`${quoteIdentifier("leaseExpiresAt", "column name")} IS NULL`);
1573
- else {
1574
- predicates.push(`${quoteIdentifier("leaseExpiresAt", "column name")} = ?`);
1575
- params.push(transformToSqlValue(options.expectedLeaseExpiresAt));
1576
+ if (options?.expectedLeaseExpiresAt !== void 0) {
1577
+ if (options.expectedLeaseExpiresAt === null) predicates.push(`${quoteIdentifier("leaseExpiresAt", "column name")} IS NULL`);
1578
+ else {
1579
+ predicates.push(`${quoteIdentifier("leaseExpiresAt", "column name")} = ?`);
1580
+ params.push(transformToSqlValue(options.expectedLeaseExpiresAt));
1581
+ }
1576
1582
  }
1577
1583
  const predicateSql = predicates.length > 0 ? ` AND ${predicates.join(" AND ")}` : "";
1578
1584
  const [result] = await this.pool.execute(`UPDATE ${formatTableName(TABLE_BACKGROUND_TASKS)} SET ${setClauses.join(", ")} WHERE ${quoteIdentifier("id", "column name")} = ?${predicateSql}`, params);
@@ -6133,10 +6139,11 @@ async function assertRunFence(q, claimsTable, fence, operation) {
6133
6139
  const [rows] = await q.execute(`SELECT generation, ${quoteIdentifier("ownerId", "column name")} AS ownerId FROM ${claimsTable}
6134
6140
  WHERE ${quoteIdentifier("runId", "column name")} = ? LOCK IN SHARE MODE`, [fence.runId]);
6135
6141
  const row = rows[0];
6136
- if (!matchesRunFence(row ? {
6142
+ const current = row ? {
6137
6143
  generation: Number(row.generation),
6138
6144
  ownerId: String(row.ownerId)
6139
- } : null, fence)) throw new RunFenceConflictError(fence, operation);
6145
+ } : null;
6146
+ if (!matchesRunFence(current, fence)) throw new RunFenceConflictError(fence, operation);
6140
6147
  }
6141
6148
  /** Runs `fn` in a transaction on its own pooled connection. */
6142
6149
  async function inTransaction(pool, fn) {
@@ -6470,13 +6477,14 @@ var MemoryMySQL = class MemoryMySQL extends MemoryStorage {
6470
6477
  for (const indexDef of this.#indexes) await this.operations.createIndex(indexDef);
6471
6478
  }
6472
6479
  async prune(policies, options) {
6480
+ const targets = resolveRetentionTargets({
6481
+ policies,
6482
+ descriptor: MemoryMySQL.retentionTables,
6483
+ order: ["runFences"]
6484
+ });
6473
6485
  return executeRetentionPrune({
6474
6486
  domain: "memory",
6475
- targets: resolveRetentionTargets({
6476
- policies,
6477
- descriptor: MemoryMySQL.retentionTables,
6478
- order: ["runFences"]
6479
- }),
6487
+ targets,
6480
6488
  options,
6481
6489
  cutoffFor: (target, now) => retentionCutoffMs(target.policy, now),
6482
6490
  deleteBatch: (target, cutoff, limit) => this.operations.pruneBatch({
@@ -8404,13 +8412,14 @@ var ObservabilityMySQL = class ObservabilityMySQL extends ObservabilityStorage {
8404
8412
  this.#indexes = indexes?.filter((idx) => ObservabilityMySQL.MANAGED_TABLES.includes(idx.table));
8405
8413
  }
8406
8414
  async prune(policies, options) {
8415
+ const targets = resolveRetentionTargets({
8416
+ policies,
8417
+ descriptor: ObservabilityMySQL.retentionTables,
8418
+ order: ["spans"]
8419
+ });
8407
8420
  return executeRetentionPrune({
8408
8421
  domain: "observability",
8409
- targets: resolveRetentionTargets({
8410
- policies,
8411
- descriptor: ObservabilityMySQL.retentionTables,
8412
- order: ["spans"]
8413
- }),
8422
+ targets,
8414
8423
  options,
8415
8424
  cutoffFor: (target, now) => new Date(retentionCutoffMs(target.policy, now)),
8416
8425
  deleteBatch: (target, cutoff, limit) => this.operations.pruneBatch({
@@ -8777,22 +8786,22 @@ var ObservabilityMySQL = class ObservabilityMySQL extends ObservabilityStorage {
8777
8786
  case TraceStatus.RUNNING:
8778
8787
  conditions.push(`${quoteIdentifier("endedAt", "column name")} IS NULL AND ${quoteIdentifier("error", "column name")} IS NULL`);
8779
8788
  break;
8780
- case TraceStatus.SUCCESS:
8781
- conditions.push(`${quoteIdentifier("endedAt", "column name")} IS NOT NULL AND ${quoteIdentifier("error", "column name")} IS NULL`);
8782
- break;
8789
+ case TraceStatus.SUCCESS: conditions.push(`${quoteIdentifier("endedAt", "column name")} IS NOT NULL AND ${quoteIdentifier("error", "column name")} IS NULL`);
8783
8790
  }
8784
- if (filters.hasChildError !== void 0) if (filters.hasChildError) conditions.push(`EXISTS (
8791
+ if (filters.hasChildError !== void 0) {
8792
+ if (filters.hasChildError) conditions.push(`EXISTS (
8785
8793
  SELECT 1 FROM ${tbl} c
8786
8794
  WHERE c.${quoteIdentifier("traceId", "column name")} = ${tbl}.${quoteIdentifier("traceId", "column name")}
8787
8795
  AND c.${quoteIdentifier("parentSpanId", "column name")} IS NOT NULL
8788
8796
  AND c.${quoteIdentifier("error", "column name")} IS NOT NULL
8789
8797
  )`);
8790
- else conditions.push(`NOT EXISTS (
8798
+ else conditions.push(`NOT EXISTS (
8791
8799
  SELECT 1 FROM ${tbl} c
8792
8800
  WHERE c.${quoteIdentifier("traceId", "column name")} = ${tbl}.${quoteIdentifier("traceId", "column name")}
8793
8801
  AND c.${quoteIdentifier("parentSpanId", "column name")} IS NOT NULL
8794
8802
  AND c.${quoteIdentifier("error", "column name")} IS NOT NULL
8795
8803
  )`);
8804
+ }
8796
8805
  }
8797
8806
  const whereClause = conditions.length > 0 ? ` WHERE ${conditions.join(" AND ")}` : "";
8798
8807
  const sortField = quoteIdentifier(orderBy.field, "column name");
@@ -9528,15 +9537,19 @@ var SchedulesMySQL = class SchedulesMySQL extends SchedulesStorage {
9528
9537
  conditions.push(`JSON_EXTRACT(${quoteIdentifier("target", "column name")}, '$.workflowId') = ?`);
9529
9538
  params.push(filter.workflowId);
9530
9539
  }
9531
- if (filter?.ownerType !== void 0) if (filter.ownerType === null) conditions.push(`${quoteIdentifier("owner_type", "column name")} IS NULL`);
9532
- else {
9533
- conditions.push(`${quoteIdentifier("owner_type", "column name")} = ?`);
9534
- params.push(filter.ownerType);
9540
+ if (filter?.ownerType !== void 0) {
9541
+ if (filter.ownerType === null) conditions.push(`${quoteIdentifier("owner_type", "column name")} IS NULL`);
9542
+ else {
9543
+ conditions.push(`${quoteIdentifier("owner_type", "column name")} = ?`);
9544
+ params.push(filter.ownerType);
9545
+ }
9535
9546
  }
9536
- if (filter?.ownerId !== void 0) if (filter.ownerId === null) conditions.push(`${quoteIdentifier("owner_id", "column name")} IS NULL`);
9537
- else {
9538
- conditions.push(`${quoteIdentifier("owner_id", "column name")} = ?`);
9539
- params.push(filter.ownerId);
9547
+ if (filter?.ownerId !== void 0) {
9548
+ if (filter.ownerId === null) conditions.push(`${quoteIdentifier("owner_id", "column name")} IS NULL`);
9549
+ else {
9550
+ conditions.push(`${quoteIdentifier("owner_id", "column name")} = ?`);
9551
+ params.push(filter.ownerId);
9552
+ }
9540
9553
  }
9541
9554
  const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
9542
9555
  const [rows] = await this.pool.execute(`SELECT * FROM ${formatTableName(TABLE_SCHEDULES)} ${where} ORDER BY ${quoteIdentifier("created_at", "column name")} ASC`, params);
@@ -11471,13 +11484,14 @@ var WorkflowsMySQL = class WorkflowsMySQL extends WorkflowsStorage {
11471
11484
  await this.createCustomIndexes();
11472
11485
  }
11473
11486
  async prune(policies, options) {
11487
+ const targets = resolveRetentionTargets({
11488
+ policies,
11489
+ descriptor: WorkflowsMySQL.retentionTables,
11490
+ order: ["runOwnership"]
11491
+ });
11474
11492
  return executeRetentionPrune({
11475
11493
  domain: "workflows",
11476
- targets: resolveRetentionTargets({
11477
- policies,
11478
- descriptor: WorkflowsMySQL.retentionTables,
11479
- order: ["runOwnership"]
11480
- }),
11494
+ targets,
11481
11495
  options,
11482
11496
  cutoffFor: (target, now) => retentionCutoffMs(target.policy, now),
11483
11497
  deleteBatch: (target, cutoff, limit) => this.operations.pruneBatch({