@mastra/spanner 1.10.0-alpha.1 → 1.10.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
@@ -838,24 +838,27 @@ var SpannerDB = class extends MastraBase {
838
838
  result[key] = value;
839
839
  continue;
840
840
  }
841
- if (colDef?.type === "jsonb") if (typeof value === "string") try {
842
- result[key] = JSON.parse(value);
843
- } catch {
844
- result[key] = value;
845
- }
846
- else result[key] = value;
847
- else if (colDef?.type === "timestamp") if (value instanceof Date) result[key] = new Date(value.getTime());
848
- else if (typeof value === "string") result[key] = new Date(value);
849
- else if (typeof value === "object" && typeof value.value === "string") result[key] = new Date(value.value);
850
- else result[key] = value;
851
- else if (colDef?.type === "integer" || colDef?.type === "bigint") if (typeof value === "string") {
852
- const n = Number(value);
853
- result[key] = Number.isSafeInteger(n) ? n : value;
854
- } else if (typeof value === "bigint") {
855
- const n = Number(value);
856
- result[key] = Number.isSafeInteger(n) ? n : value.toString();
857
- } else result[key] = value;
858
- else if (colDef?.type === "boolean") result[key] = Boolean(value);
841
+ if (colDef?.type === "jsonb") {
842
+ if (typeof value === "string") try {
843
+ result[key] = JSON.parse(value);
844
+ } catch {
845
+ result[key] = value;
846
+ }
847
+ else result[key] = value;
848
+ } else if (colDef?.type === "timestamp") {
849
+ if (value instanceof Date) result[key] = new Date(value.getTime());
850
+ else if (typeof value === "string") result[key] = new Date(value);
851
+ else if (typeof value === "object" && typeof value.value === "string") result[key] = new Date(value.value);
852
+ else result[key] = value;
853
+ } else if (colDef?.type === "integer" || colDef?.type === "bigint") {
854
+ if (typeof value === "string") {
855
+ const n = Number(value);
856
+ result[key] = Number.isSafeInteger(n) ? n : value;
857
+ } else if (typeof value === "bigint") {
858
+ const n = Number(value);
859
+ result[key] = Number.isSafeInteger(n) ? n : value.toString();
860
+ } else result[key] = value;
861
+ } else if (colDef?.type === "boolean") result[key] = Boolean(value);
859
862
  else result[key] = value;
860
863
  }
861
864
  return result;
@@ -904,16 +907,17 @@ var SpannerDB = class extends MastraBase {
904
907
  for (const item of inValues) paramNames.push(`@${bind(key, item)}`);
905
908
  conditions.push(`${quoteIdent(key, "field name")} IN (${paramNames.join(", ")})`);
906
909
  }
907
- } else if (Array.isArray(value)) if (value.length === 0) conditions.push("1 = 0");
908
- else if (value.length === 1) {
909
- const param = bind(key, value[0]);
910
- conditions.push(`${quoteIdent(key, "field name")} = @${param}`);
910
+ } else if (Array.isArray(value)) {
911
+ if (value.length === 0) conditions.push("1 = 0");
912
+ else if (value.length === 1) {
913
+ const param = bind(key, value[0]);
914
+ conditions.push(`${quoteIdent(key, "field name")} = @${param}`);
915
+ } else {
916
+ const paramNames = [];
917
+ for (const item of value) paramNames.push(`@${bind(key, item)}`);
918
+ conditions.push(`${quoteIdent(key, "field name")} IN (${paramNames.join(", ")})`);
919
+ }
911
920
  } else {
912
- const paramNames = [];
913
- for (const item of value) paramNames.push(`@${bind(key, item)}`);
914
- conditions.push(`${quoteIdent(key, "field name")} IN (${paramNames.join(", ")})`);
915
- }
916
- else {
917
921
  const param = bind(key, value);
918
922
  conditions.push(`${quoteIdent(key, "field name")} = @${param}`);
919
923
  }
@@ -1173,18 +1177,20 @@ function transformFromSpannerRow({ tableName, row }) {
1173
1177
  } catch {
1174
1178
  result[key] = value;
1175
1179
  }
1176
- else if (colDef?.type === "timestamp") if (value instanceof Date) result[key] = new Date(value.getTime());
1177
- else if (typeof value === "string") result[key] = new Date(value);
1178
- else if (typeof value === "object" && typeof value.value === "string") result[key] = new Date(value.value);
1179
- else result[key] = value;
1180
- else if (colDef?.type === "integer" || colDef?.type === "bigint") if (typeof value === "string") {
1181
- const n = Number(value);
1182
- result[key] = Number.isSafeInteger(n) ? n : value;
1183
- } else if (typeof value === "bigint") {
1184
- const n = Number(value);
1185
- result[key] = Number.isSafeInteger(n) ? n : value.toString();
1186
- } else result[key] = value;
1187
- else if (colDef?.type === "boolean") result[key] = Boolean(value);
1180
+ else if (colDef?.type === "timestamp") {
1181
+ if (value instanceof Date) result[key] = new Date(value.getTime());
1182
+ else if (typeof value === "string") result[key] = new Date(value);
1183
+ else if (typeof value === "object" && typeof value.value === "string") result[key] = new Date(value.value);
1184
+ else result[key] = value;
1185
+ } else if (colDef?.type === "integer" || colDef?.type === "bigint") {
1186
+ if (typeof value === "string") {
1187
+ const n = Number(value);
1188
+ result[key] = Number.isSafeInteger(n) ? n : value;
1189
+ } else if (typeof value === "bigint") {
1190
+ const n = Number(value);
1191
+ result[key] = Number.isSafeInteger(n) ? n : value.toString();
1192
+ } else result[key] = value;
1193
+ } else if (colDef?.type === "boolean") result[key] = Boolean(value);
1188
1194
  else result[key] = value;
1189
1195
  }
1190
1196
  return result;
@@ -2048,15 +2054,19 @@ var BackgroundTasksSpanner = class BackgroundTasksSpanner extends BackgroundTask
2048
2054
  predicates.push("status = @expectedStatus");
2049
2055
  params.expectedStatus = options.expectedStatus;
2050
2056
  }
2051
- if (options?.expectedOwnerId !== void 0) if (options.expectedOwnerId === null) predicates.push("ownerId IS NULL");
2052
- else {
2053
- predicates.push("ownerId = @expectedOwnerId");
2054
- params.expectedOwnerId = options.expectedOwnerId;
2057
+ if (options?.expectedOwnerId !== void 0) {
2058
+ if (options.expectedOwnerId === null) predicates.push("ownerId IS NULL");
2059
+ else {
2060
+ predicates.push("ownerId = @expectedOwnerId");
2061
+ params.expectedOwnerId = options.expectedOwnerId;
2062
+ }
2055
2063
  }
2056
- if (options?.expectedLeaseExpiresAt !== void 0) if (options.expectedLeaseExpiresAt === null) predicates.push("leaseExpiresAt IS NULL");
2057
- else {
2058
- predicates.push("leaseExpiresAt = @expectedLeaseExpiresAt");
2059
- params.expectedLeaseExpiresAt = options.expectedLeaseExpiresAt;
2064
+ if (options?.expectedLeaseExpiresAt !== void 0) {
2065
+ if (options.expectedLeaseExpiresAt === null) predicates.push("leaseExpiresAt IS NULL");
2066
+ else {
2067
+ predicates.push("leaseExpiresAt = @expectedLeaseExpiresAt");
2068
+ params.expectedLeaseExpiresAt = options.expectedLeaseExpiresAt;
2069
+ }
2060
2070
  }
2061
2071
  const predicateSql = predicates.length > 0 ? ` AND ${predicates.join(" AND ")}` : "";
2062
2072
  const [rowCount] = await this.database.runUpdate({
@@ -6728,10 +6738,11 @@ async function assertRunFence(tx, claimsTable, fence, operation) {
6728
6738
  json: true
6729
6739
  });
6730
6740
  const row = rows[0];
6731
- if (!matchesRunFence(row ? {
6741
+ const current = row ? {
6732
6742
  generation: Number(row.generation),
6733
6743
  ownerId: String(row.ownerId)
6734
- } : null, fence)) throw new RunFenceConflictError(fence, operation);
6744
+ } : null;
6745
+ if (!matchesRunFence(current, fence)) throw new RunFenceConflictError(fence, operation);
6735
6746
  }
6736
6747
  /**
6737
6748
  * Runs `fn` in a read-write transaction and commits it, retrying the whole
@@ -6882,13 +6893,14 @@ var MemorySpanner = class MemorySpanner extends MemoryStorage {
6882
6893
  }
6883
6894
  /** Removes every row from threads/messages/resources and run fence tables. Intended for tests. */
6884
6895
  async prune(policies, options) {
6896
+ const targets = resolveRetentionTargets({
6897
+ policies,
6898
+ descriptor: MemorySpanner.retentionTables,
6899
+ order: ["runFences"]
6900
+ });
6885
6901
  return executeRetentionPrune({
6886
6902
  domain: "memory",
6887
- targets: resolveRetentionTargets({
6888
- policies,
6889
- descriptor: MemorySpanner.retentionTables,
6890
- order: ["runFences"]
6891
- }),
6903
+ targets,
6892
6904
  options,
6893
6905
  cutoffFor: (target, now) => retentionCutoffMs(target.policy, now),
6894
6906
  deleteBatch: (target, cutoff, limit) => this.db.pruneRunRows({
@@ -8560,18 +8572,19 @@ function combineWhere(nameValue, filterClause, filterParams, filterTypes, nextIn
8560
8572
  const params = { ...filterParams };
8561
8573
  const types = { ...filterTypes };
8562
8574
  const conditions = [];
8563
- if (Array.isArray(nameValue)) if (nameValue.length === 0) conditions.push("1 = 0");
8564
- else {
8565
- const placeholders = [];
8566
- let i = nextIndex;
8567
- for (const n of nameValue) {
8568
- const param = `n${i++}`;
8569
- placeholders.push(`@${param}`);
8570
- params[param] = n;
8575
+ if (Array.isArray(nameValue)) {
8576
+ if (nameValue.length === 0) conditions.push("1 = 0");
8577
+ else {
8578
+ const placeholders = [];
8579
+ let i = nextIndex;
8580
+ for (const n of nameValue) {
8581
+ const param = `n${i++}`;
8582
+ placeholders.push(`@${param}`);
8583
+ params[param] = n;
8584
+ }
8585
+ conditions.push(`${quoteIdent("name", "column name")} IN (${placeholders.join(", ")})`);
8571
8586
  }
8572
- conditions.push(`${quoteIdent("name", "column name")} IN (${placeholders.join(", ")})`);
8573
- }
8574
- else {
8587
+ } else {
8575
8588
  const param = `n${nextIndex}`;
8576
8589
  params[param] = nameValue;
8577
8590
  conditions.push(`${quoteIdent("name", "column name")} = @${param}`);
@@ -8790,7 +8803,6 @@ async function getMetricAggregate(database, args, options) {
8790
8803
  default:
8791
8804
  prevStart = new Date(ts.start.getTime() - duration);
8792
8805
  prevEnd = new Date(ts.end.getTime() - duration);
8793
- break;
8794
8806
  }
8795
8807
  const prevFilterResult = buildWhereClause({
8796
8808
  ...args.filters,
@@ -9208,17 +9220,19 @@ var ObservabilitySpanner = class ObservabilitySpanner extends ObservabilityStora
9208
9220
  return this.disableMetrics ? [] : ["metrics", "metric-discovery"];
9209
9221
  }
9210
9222
  async prune(policies, options) {
9223
+ const descriptor = this.disableMetrics ? { spans: {
9224
+ table: TABLE_SPANS,
9225
+ column: "startedAt",
9226
+ indexed: false
9227
+ } } : ObservabilitySpanner.retentionTables;
9228
+ const targets = resolveRetentionTargets({
9229
+ policies,
9230
+ descriptor,
9231
+ order: ["spans", "metrics"]
9232
+ });
9211
9233
  return executeRetentionPrune({
9212
9234
  domain: "observability",
9213
- targets: resolveRetentionTargets({
9214
- policies,
9215
- descriptor: this.disableMetrics ? { spans: {
9216
- table: TABLE_SPANS,
9217
- column: "startedAt",
9218
- indexed: false
9219
- } } : ObservabilitySpanner.retentionTables,
9220
- order: ["spans", "metrics"]
9221
- }),
9235
+ targets,
9222
9236
  options,
9223
9237
  cutoffFor: (target, now) => new Date(retentionCutoffMs(target.policy, now)).toISOString(),
9224
9238
  deleteBatch: async (target, cutoff, limit) => {
@@ -9618,9 +9632,7 @@ var ObservabilitySpanner = class ObservabilitySpanner extends ObservabilityStora
9618
9632
  case TraceStatus.RUNNING:
9619
9633
  conditions.push(`${rAlias}.${quoteIdent("endedAt", "column name")} IS NULL AND ${rAlias}.${quoteIdent("error", "column name")} IS NULL`);
9620
9634
  break;
9621
- case TraceStatus.SUCCESS:
9622
- conditions.push(`${rAlias}.${quoteIdent("endedAt", "column name")} IS NOT NULL AND ${rAlias}.${quoteIdent("error", "column name")} IS NULL`);
9623
- break;
9635
+ case TraceStatus.SUCCESS: conditions.push(`${rAlias}.${quoteIdent("endedAt", "column name")} IS NOT NULL AND ${rAlias}.${quoteIdent("error", "column name")} IS NULL`);
9624
9636
  }
9625
9637
  if (filters.hasChildError !== void 0) {
9626
9638
  const existsClause = `EXISTS (SELECT 1 FROM ${tableName} c WHERE c.${quoteIdent("traceId", "column name")} = ${rAlias}.${quoteIdent("traceId", "column name")} AND c.${quoteIdent("error", "column name")} IS NOT NULL)`;
@@ -10591,15 +10603,19 @@ var SchedulesSpanner = class SchedulesSpanner extends SchedulesStorage {
10591
10603
  else conditions.push(`JSON_VALUE(${quoteIdent("target", "column name")}, '$.workflowId') = @workflowId`);
10592
10604
  params.workflowId = filter.workflowId;
10593
10605
  }
10594
- if (filter?.ownerType !== void 0) if (filter.ownerType === null) conditions.push(`${quoteIdent("owner_type", "column name")} IS NULL`);
10595
- else {
10596
- conditions.push(`${quoteIdent("owner_type", "column name")} = @ownerType`);
10597
- params.ownerType = filter.ownerType;
10606
+ if (filter?.ownerType !== void 0) {
10607
+ if (filter.ownerType === null) conditions.push(`${quoteIdent("owner_type", "column name")} IS NULL`);
10608
+ else {
10609
+ conditions.push(`${quoteIdent("owner_type", "column name")} = @ownerType`);
10610
+ params.ownerType = filter.ownerType;
10611
+ }
10598
10612
  }
10599
- if (filter?.ownerId !== void 0) if (filter.ownerId === null) conditions.push(`${quoteIdent("owner_id", "column name")} IS NULL`);
10600
- else {
10601
- conditions.push(`${quoteIdent("owner_id", "column name")} = @ownerId`);
10602
- params.ownerId = filter.ownerId;
10613
+ if (filter?.ownerId !== void 0) {
10614
+ if (filter.ownerId === null) conditions.push(`${quoteIdent("owner_id", "column name")} IS NULL`);
10615
+ else {
10616
+ conditions.push(`${quoteIdent("owner_id", "column name")} = @ownerId`);
10617
+ params.ownerId = filter.ownerId;
10618
+ }
10603
10619
  }
10604
10620
  const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
10605
10621
  const sql = `SELECT * FROM ${quoteIdent(TABLE_SCHEDULES, "table name")}
@@ -11447,10 +11463,11 @@ var ScorerDefinitionsSpanner = class ScorerDefinitionsSpanner extends ScorerDefi
11447
11463
  //#endregion
11448
11464
  //#region src/storage/domains/scores/index.ts
11449
11465
  function transformScoreRow$1(row) {
11450
- return transformScoreRow(transformFromSpannerRow({
11466
+ const normalized = transformFromSpannerRow({
11451
11467
  tableName: TABLE_SCORERS,
11452
11468
  row
11453
- }), { convertTimestamps: true });
11469
+ });
11470
+ return transformScoreRow(normalized, { convertTimestamps: true });
11454
11471
  }
11455
11472
  /** Appends multi-tenant scope conditions and their named params. */
11456
11473
  function applyTenancyFilters(conditions, params, filters) {
@@ -12702,13 +12719,14 @@ var WorkflowsSpanner = class WorkflowsSpanner extends WorkflowsStorage {
12702
12719
  }
12703
12720
  /** Removes every workflow snapshot and run ownership row. Intended for tests. */
12704
12721
  async prune(policies, options) {
12722
+ const targets = resolveRetentionTargets({
12723
+ policies,
12724
+ descriptor: WorkflowsSpanner.retentionTables,
12725
+ order: ["runOwnership"]
12726
+ });
12705
12727
  return executeRetentionPrune({
12706
12728
  domain: "workflows",
12707
- targets: resolveRetentionTargets({
12708
- policies,
12709
- descriptor: WorkflowsSpanner.retentionTables,
12710
- order: ["runOwnership"]
12711
- }),
12729
+ targets,
12712
12730
  options,
12713
12731
  cutoffFor: (target, now) => retentionCutoffMs(target.policy, now),
12714
12732
  deleteBatch: (target, cutoff, limit) => this.db.pruneRunRows({