@mastra/mssql 1.10.4 → 1.10.5

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
@@ -1110,21 +1110,22 @@ function prepareWhereClause(filters, _schema) {
1110
1110
  }
1111
1111
  conditions.push(`[${parseSqlIdentifier(key, "field name")}] IN (${inParamNames.join(", ")})`);
1112
1112
  }
1113
- } else if (Array.isArray(value)) if (value.length === 0) conditions.push("1 = 0");
1114
- else if (value.length === 1) {
1115
- const paramName = `p${paramIndex++}`;
1116
- conditions.push(`[${parseSqlIdentifier(key, "field name")}] = @${paramName}`);
1117
- params[paramName] = value[0] instanceof Date ? value[0].toISOString() : value[0];
1118
- } else {
1119
- const inParamNames = [];
1120
- for (const item of value) {
1113
+ } else if (Array.isArray(value)) {
1114
+ if (value.length === 0) conditions.push("1 = 0");
1115
+ else if (value.length === 1) {
1121
1116
  const paramName = `p${paramIndex++}`;
1122
- inParamNames.push(`@${paramName}`);
1123
- params[paramName] = item instanceof Date ? item.toISOString() : item;
1117
+ conditions.push(`[${parseSqlIdentifier(key, "field name")}] = @${paramName}`);
1118
+ params[paramName] = value[0] instanceof Date ? value[0].toISOString() : value[0];
1119
+ } else {
1120
+ const inParamNames = [];
1121
+ for (const item of value) {
1122
+ const paramName = `p${paramIndex++}`;
1123
+ inParamNames.push(`@${paramName}`);
1124
+ params[paramName] = item instanceof Date ? item.toISOString() : item;
1125
+ }
1126
+ conditions.push(`[${parseSqlIdentifier(key, "field name")}] IN (${inParamNames.join(", ")})`);
1124
1127
  }
1125
- conditions.push(`[${parseSqlIdentifier(key, "field name")}] IN (${inParamNames.join(", ")})`);
1126
- }
1127
- else {
1128
+ } else {
1128
1129
  const paramName = `p${paramIndex++}`;
1129
1130
  conditions.push(`[${parseSqlIdentifier(key, "field name")}] = @${paramName}`);
1130
1131
  params[paramName] = value instanceof Date ? value.toISOString() : value;
@@ -1959,15 +1960,19 @@ var BackgroundTasksMSSQL = class BackgroundTasksMSSQL extends BackgroundTasksSto
1959
1960
  params[`p${idx}`] = options.expectedStatus;
1960
1961
  predicates.push(`[status] = @p${idx++}`);
1961
1962
  }
1962
- if (options?.expectedOwnerId !== void 0) if (options.expectedOwnerId === null) predicates.push(`[ownerId] IS NULL`);
1963
- else {
1964
- params[`p${idx}`] = options.expectedOwnerId;
1965
- predicates.push(`[ownerId] = @p${idx++}`);
1963
+ if (options?.expectedOwnerId !== void 0) {
1964
+ if (options.expectedOwnerId === null) predicates.push(`[ownerId] IS NULL`);
1965
+ else {
1966
+ params[`p${idx}`] = options.expectedOwnerId;
1967
+ predicates.push(`[ownerId] = @p${idx++}`);
1968
+ }
1966
1969
  }
1967
- if (options?.expectedLeaseExpiresAt !== void 0) if (options.expectedLeaseExpiresAt === null) predicates.push(`[leaseExpiresAt] IS NULL`);
1968
- else {
1969
- params[`p${idx}`] = options.expectedLeaseExpiresAt.toISOString();
1970
- predicates.push(`[leaseExpiresAt] = @p${idx++}`);
1970
+ if (options?.expectedLeaseExpiresAt !== void 0) {
1971
+ if (options.expectedLeaseExpiresAt === null) predicates.push(`[leaseExpiresAt] IS NULL`);
1972
+ else {
1973
+ params[`p${idx}`] = options.expectedLeaseExpiresAt.toISOString();
1974
+ predicates.push(`[leaseExpiresAt] = @p${idx++}`);
1975
+ }
1971
1976
  }
1972
1977
  const predicateSql = predicates.length > 0 ? ` AND ${predicates.join(" AND ")}` : "";
1973
1978
  const request = this.pool.request();
@@ -2124,10 +2129,11 @@ const MAX_CLAIM_ATTEMPTS = 5;
2124
2129
  */
2125
2130
  async function assertRunFence(q, claimsTable, fence, operation) {
2126
2131
  const row = (await q.request().input("runId", fence.runId).query(`SELECT [generation], [ownerId] FROM ${claimsTable} WITH (HOLDLOCK) WHERE [runId] = @runId`)).recordset[0];
2127
- if (!matchesRunFence(row ? {
2132
+ const current = row ? {
2128
2133
  generation: Number(row.generation),
2129
2134
  ownerId: String(row.ownerId)
2130
- } : null, fence)) throw new RunFenceConflictError(fence, operation);
2135
+ } : null;
2136
+ if (!matchesRunFence(current, fence)) throw new RunFenceConflictError(fence, operation);
2131
2137
  }
2132
2138
  /** Runs `fn` in a transaction. */
2133
2139
  async function inTransaction(pool, fn) {
@@ -2372,13 +2378,14 @@ var MemoryMSSQL = class MemoryMSSQL extends MemoryStorage {
2372
2378
  }
2373
2379
  }
2374
2380
  async prune(policies, options) {
2381
+ const targets = resolveRetentionTargets({
2382
+ policies,
2383
+ descriptor: MemoryMSSQL.retentionTables,
2384
+ order: ["runFences"]
2385
+ });
2375
2386
  return executeRetentionPrune({
2376
2387
  domain: "memory",
2377
- targets: resolveRetentionTargets({
2378
- policies,
2379
- descriptor: MemoryMSSQL.retentionTables,
2380
- order: ["runFences"]
2381
- }),
2388
+ targets,
2382
2389
  options,
2383
2390
  cutoffFor: (target, now) => retentionCutoffMs(target.policy, now),
2384
2391
  deleteBatch: (target, cutoff, limit) => this.db.pruneBatch({
@@ -3360,13 +3367,14 @@ var ObservabilityMSSQL = class ObservabilityMSSQL extends ObservabilityStorage {
3360
3367
  this.indexes = indexes?.filter((idx) => ObservabilityMSSQL.MANAGED_TABLES.includes(idx.table));
3361
3368
  }
3362
3369
  async prune(policies, options) {
3370
+ const targets = resolveRetentionTargets({
3371
+ policies,
3372
+ descriptor: ObservabilityMSSQL.retentionTables,
3373
+ order: ["spans"]
3374
+ });
3363
3375
  return executeRetentionPrune({
3364
3376
  domain: "observability",
3365
- targets: resolveRetentionTargets({
3366
- policies,
3367
- descriptor: ObservabilityMSSQL.retentionTables,
3368
- order: ["spans"]
3369
- }),
3377
+ targets,
3370
3378
  options,
3371
3379
  cutoffFor: (target, now) => new Date(retentionCutoffMs(target.policy, now)),
3372
3380
  deleteBatch: (target, cutoff, limit) => this.db.pruneBatch({
@@ -3829,18 +3837,18 @@ var ObservabilityMSSQL = class ObservabilityMSSQL extends ObservabilityStorage {
3829
3837
  case TraceStatus.RUNNING:
3830
3838
  conditions.push(`r.[endedAt] IS NULL AND r.[error] IS NULL`);
3831
3839
  break;
3832
- case TraceStatus.SUCCESS:
3833
- conditions.push(`r.[endedAt] IS NOT NULL AND r.[error] IS NULL`);
3834
- break;
3840
+ case TraceStatus.SUCCESS: conditions.push(`r.[endedAt] IS NOT NULL AND r.[error] IS NULL`);
3835
3841
  }
3836
- if (filters.hasChildError !== void 0) if (filters.hasChildError) conditions.push(`EXISTS (
3842
+ if (filters.hasChildError !== void 0) {
3843
+ if (filters.hasChildError) conditions.push(`EXISTS (
3837
3844
  SELECT 1 FROM ${tableName} c
3838
3845
  WHERE c.[traceId] = r.[traceId] AND c.[error] IS NOT NULL
3839
3846
  )`);
3840
- else conditions.push(`NOT EXISTS (
3847
+ else conditions.push(`NOT EXISTS (
3841
3848
  SELECT 1 FROM ${tableName} c
3842
3849
  WHERE c.[traceId] = r.[traceId] AND c.[error] IS NOT NULL
3843
3850
  )`);
3851
+ }
3844
3852
  }
3845
3853
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
3846
3854
  const sortField = orderBy?.field ?? "startedAt";
@@ -4683,13 +4691,14 @@ var WorkflowsMSSQL = class WorkflowsMSSQL extends WorkflowsStorage {
4683
4691
  }
4684
4692
  }
4685
4693
  async prune(policies, options) {
4694
+ const targets = resolveRetentionTargets({
4695
+ policies,
4696
+ descriptor: WorkflowsMSSQL.retentionTables,
4697
+ order: ["runOwnership"]
4698
+ });
4686
4699
  return executeRetentionPrune({
4687
4700
  domain: "workflows",
4688
- targets: resolveRetentionTargets({
4689
- policies,
4690
- descriptor: WorkflowsMSSQL.retentionTables,
4691
- order: ["runOwnership"]
4692
- }),
4701
+ targets,
4693
4702
  options,
4694
4703
  cutoffFor: (target, now) => retentionCutoffMs(target.policy, now),
4695
4704
  deleteBatch: (target, cutoff, limit) => this.db.pruneBatch({
@@ -5074,10 +5083,12 @@ var WorkflowsMSSQL = class WorkflowsMSSQL extends WorkflowsStorage {
5074
5083
  conditions.push(`JSON_VALUE([snapshot], '$.status') = @status`);
5075
5084
  paramMap["status"] = status;
5076
5085
  }
5077
- if (resourceId) if (await this.db.hasColumn(TABLE_WORKFLOW_SNAPSHOT, "resourceId")) {
5078
- conditions.push(`[resourceId] = @resourceId`);
5079
- paramMap["resourceId"] = resourceId;
5080
- } else this.logger?.warn?.(`[${TABLE_WORKFLOW_SNAPSHOT}] resourceId column not found. Skipping resourceId filter.`);
5086
+ if (resourceId) {
5087
+ if (await this.db.hasColumn(TABLE_WORKFLOW_SNAPSHOT, "resourceId")) {
5088
+ conditions.push(`[resourceId] = @resourceId`);
5089
+ paramMap["resourceId"] = resourceId;
5090
+ } else this.logger?.warn?.(`[${TABLE_WORKFLOW_SNAPSHOT}] resourceId column not found. Skipping resourceId filter.`);
5091
+ }
5081
5092
  if (fromDate instanceof Date && !isNaN(fromDate.getTime())) {
5082
5093
  conditions.push(`[createdAt] >= @fromDate`);
5083
5094
  paramMap[`fromDate`] = fromDate.toISOString();