@mastra/dsql 1.5.4-alpha.0 → 1.5.5-alpha.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
@@ -6,7 +6,7 @@ import { MastraBase } from "@mastra/core/base";
6
6
  import { parseSqlIdentifier } from "@mastra/core/utils";
7
7
  import { MessageList } from "@mastra/core/agent";
8
8
  import { saveScorePayloadSchema } from "@mastra/core/evals";
9
- //#region src/shared/config.ts
9
+ // src/shared/config.ts
10
10
  /**
11
11
  * Default connection pool settings optimized for Aurora DSQL.
12
12
  *
@@ -70,8 +70,7 @@ const getEffectiveRegion = (config) => {
70
70
  if (extractedRegion) return extractedRegion;
71
71
  throw new Error("DSQLStore: region could not be determined. Provide region in config or use a standard DSQL endpoint.");
72
72
  };
73
- //#endregion
74
- //#region src/storage/client.ts
73
+ // src/storage/client.ts
75
74
  /**
76
75
  * Truncate a query string for error messages.
77
76
  */
@@ -439,8 +438,7 @@ async function withRetry(fn, options = {}) {
439
438
  }
440
439
  throw lastError;
441
440
  }
442
- //#endregion
443
- //#region src/shared/batch.ts
441
+ // src/shared/batch.ts
444
442
  /**
445
443
  * Batch utilities for Aurora DSQL.
446
444
  *
@@ -489,8 +487,7 @@ function splitIntoBatches(records, options = {}) {
489
487
  batchCount: batches.length
490
488
  };
491
489
  }
492
- //#endregion
493
- //#region src/storage/db/index.ts
490
+ // src/storage/db/index.ts
494
491
  /**
495
492
  * Resolves DsqlDomainConfi to a database client and schema.
496
493
  * Handles creating a new pool if config is provided.
@@ -934,7 +931,8 @@ var DsqlDB = class extends MastraBase {
934
931
  const uniqueStr = unique ? "UNIQUE " : "";
935
932
  const methodStr = method !== "btree" ? `USING ${method} ` : "";
936
933
  const columnsStr = columns.map((col) => {
937
- const quotedCol = `"${parseSqlIdentifier(col.replace(/\s+(DESC|ASC)$/i, "").trim(), "column name")}"`;
934
+ const colName = col.replace(/\s+(DESC|ASC)$/i, "").trim();
935
+ const quotedCol = `"${parseSqlIdentifier(colName, "column name")}"`;
938
936
  return opclass ? `${quotedCol} ${opclass}` : quotedCol;
939
937
  }).join(", ");
940
938
  const whereStr = where ? ` WHERE ${where}` : "";
@@ -1229,8 +1227,7 @@ var DsqlDB = class extends MastraBase {
1229
1227
  return this.clearTable({ tableName });
1230
1228
  }
1231
1229
  };
1232
- //#endregion
1233
- //#region src/storage/domains/utils.ts
1230
+ // src/storage/domains/utils.ts
1234
1231
  function getSchemaName(schema) {
1235
1232
  return schema ? `"${parseSqlIdentifier(schema, "schema name")}"` : void 0;
1236
1233
  }
@@ -1259,8 +1256,7 @@ function transformFromSqlRow({ tableName, sqlRow }) {
1259
1256
  });
1260
1257
  return result;
1261
1258
  }
1262
- //#endregion
1263
- //#region src/storage/domains/agents/index.ts
1259
+ // src/storage/domains/agents/index.ts
1264
1260
  var AgentsDSQL = class AgentsDSQL extends AgentsStorage {
1265
1261
  #db;
1266
1262
  #schema;
@@ -1824,8 +1820,7 @@ var AgentsDSQL = class AgentsDSQL extends AgentsStorage {
1824
1820
  };
1825
1821
  }
1826
1822
  };
1827
- //#endregion
1828
- //#region src/storage/db/run-fencing.ts
1823
+ // src/storage/db/run-fencing.ts
1829
1824
  /**
1830
1825
  * Epoch milliseconds on the database clock. Aurora DSQL never waits on a row
1831
1826
  * lock, so the transaction's start time is the time its statements run, and
@@ -1850,10 +1845,11 @@ function isRetriableRunFenceWrite(error) {
1850
1845
  */
1851
1846
  async function assertRunFence(t, claimsTable, fence, operation) {
1852
1847
  const row = await t.oneOrNone(`SELECT generation, "ownerId" FROM ${claimsTable} WHERE "runId" = $1 FOR UPDATE`, [fence.runId]);
1853
- if (!matchesRunFence(row && {
1848
+ const current = row && {
1854
1849
  generation: Number(row.generation),
1855
1850
  ownerId: row.ownerId
1856
- }, fence)) throw new RunFenceConflictError(fence, operation);
1851
+ };
1852
+ if (!matchesRunFence(current, fence)) throw new RunFenceConflictError(fence, operation);
1857
1853
  }
1858
1854
  /**
1859
1855
  * Runs `write` directly when it carries no fence, or in a transaction behind
@@ -1867,8 +1863,7 @@ function withRunFence(client, claimsTable, fence, operation, write) {
1867
1863
  return write(t);
1868
1864
  });
1869
1865
  }
1870
- //#endregion
1871
- //#region src/storage/domains/memory/index.ts
1866
+ // src/storage/domains/memory/index.ts
1872
1867
  /**
1873
1868
  * Generate SQL placeholder string for IN clauses.
1874
1869
  * @param count - Number of placeholders to generate
@@ -1984,13 +1979,14 @@ var MemoryDSQL = class MemoryDSQL extends MemoryStorage {
1984
1979
  }
1985
1980
  }
1986
1981
  async prune(policies, options) {
1982
+ const targets = resolveRetentionTargets({
1983
+ policies,
1984
+ descriptor: MemoryDSQL.retentionTables,
1985
+ order: ["runFences"]
1986
+ });
1987
1987
  return executeRetentionPrune({
1988
1988
  domain: "memory",
1989
- targets: resolveRetentionTargets({
1990
- policies,
1991
- descriptor: MemoryDSQL.retentionTables,
1992
- order: ["runFences"]
1993
- }),
1989
+ targets,
1994
1990
  options,
1995
1991
  cutoffFor: (target, now) => retentionCutoffMs(target.policy, now),
1996
1992
  deleteBatch: (target, cutoff, limit) => this.#db.pruneBatch({
@@ -2943,8 +2939,7 @@ var MemoryDSQL = class MemoryDSQL extends MemoryStorage {
2943
2939
  return result;
2944
2940
  }
2945
2941
  };
2946
- //#endregion
2947
- //#region src/storage/domains/observability/index.ts
2942
+ // src/storage/domains/observability/index.ts
2948
2943
  var ObservabilityDSQL = class ObservabilityDSQL extends ObservabilityStorage {
2949
2944
  static retentionTables = { spans: {
2950
2945
  table: TABLE_SPANS,
@@ -2970,13 +2965,14 @@ var ObservabilityDSQL = class ObservabilityDSQL extends ObservabilityStorage {
2970
2965
  this.#indexes = indexes?.filter((idx) => ObservabilityDSQL.MANAGED_TABLES.includes(idx.table));
2971
2966
  }
2972
2967
  async prune(policies, options) {
2968
+ const targets = resolveRetentionTargets({
2969
+ policies,
2970
+ descriptor: ObservabilityDSQL.retentionTables,
2971
+ order: ["spans"]
2972
+ });
2973
2973
  return executeRetentionPrune({
2974
2974
  domain: "observability",
2975
- targets: resolveRetentionTargets({
2976
- policies,
2977
- descriptor: ObservabilityDSQL.retentionTables,
2978
- order: ["spans"]
2979
- }),
2975
+ targets,
2980
2976
  options,
2981
2977
  cutoffFor: (target, now) => new Date(retentionCutoffMs(target.policy, now)),
2982
2978
  deleteBatch: (target, cutoff, limit) => this.#db.pruneBatch({
@@ -3351,18 +3347,18 @@ var ObservabilityDSQL = class ObservabilityDSQL extends ObservabilityStorage {
3351
3347
  case TraceStatus.RUNNING:
3352
3348
  conditions.push(`r."endedAtZ" IS NULL AND r."error" IS NULL`);
3353
3349
  break;
3354
- case TraceStatus.SUCCESS:
3355
- conditions.push(`r."endedAtZ" IS NOT NULL AND r."error" IS NULL`);
3356
- break;
3350
+ case TraceStatus.SUCCESS: conditions.push(`r."endedAtZ" IS NOT NULL AND r."error" IS NULL`);
3357
3351
  }
3358
- if (filters.hasChildError !== void 0) if (filters.hasChildError) conditions.push(`EXISTS (
3352
+ if (filters.hasChildError !== void 0) {
3353
+ if (filters.hasChildError) conditions.push(`EXISTS (
3359
3354
  SELECT 1 FROM ${tableName} c
3360
3355
  WHERE c."traceId" = r."traceId" AND c."error" IS NOT NULL
3361
3356
  )`);
3362
- else conditions.push(`NOT EXISTS (
3357
+ else conditions.push(`NOT EXISTS (
3363
3358
  SELECT 1 FROM ${tableName} c
3364
3359
  WHERE c."traceId" = r."traceId" AND c."error" IS NOT NULL
3365
3360
  )`);
3361
+ }
3366
3362
  }
3367
3363
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
3368
3364
  const orderClause = `ORDER BY r."${`${orderBy?.field ?? "startedAt"}Z`}" ${orderBy?.direction ?? "DESC"}`;
@@ -3499,8 +3495,7 @@ var ObservabilityDSQL = class ObservabilityDSQL extends ObservabilityStorage {
3499
3495
  }
3500
3496
  }
3501
3497
  };
3502
- //#endregion
3503
- //#region src/storage/domains/scores/index.ts
3498
+ // src/storage/domains/scores/index.ts
3504
3499
  /**
3505
3500
  * DSQL-specific score row transformation.
3506
3501
  * Uses Z-suffix timestamps (createdAtZ, updatedAtZ) when available.
@@ -3877,8 +3872,7 @@ var ScoresDSQL = class ScoresDSQL extends ScoresStorage {
3877
3872
  }
3878
3873
  }
3879
3874
  };
3880
- //#endregion
3881
- //#region src/storage/domains/workflows/index.ts
3875
+ // src/storage/domains/workflows/index.ts
3882
3876
  const RUN_OWNER_COLUMNS = `generation, "ownerId", "leaseExpiresAt", ${DB_NOW_MS} AS "nowMs"`;
3883
3877
  function toRunOwnershipRecord(runId, row) {
3884
3878
  const leaseExpiresAt = row.leaseExpiresAt === null ? null : Number(row.leaseExpiresAt);
@@ -3981,13 +3975,14 @@ var WorkflowsDSQL = class WorkflowsDSQL extends WorkflowsStorage {
3981
3975
  }
3982
3976
  }
3983
3977
  async prune(policies, options) {
3978
+ const targets = resolveRetentionTargets({
3979
+ policies,
3980
+ descriptor: WorkflowsDSQL.retentionTables,
3981
+ order: ["runOwnership"]
3982
+ });
3984
3983
  return executeRetentionPrune({
3985
3984
  domain: "workflows",
3986
- targets: resolveRetentionTargets({
3987
- policies,
3988
- descriptor: WorkflowsDSQL.retentionTables,
3989
- order: ["runOwnership"]
3990
- }),
3985
+ targets,
3991
3986
  options,
3992
3987
  cutoffFor: (target, now) => retentionCutoffMs(target.policy, now),
3993
3988
  deleteBatch: (target, cutoff, limit) => this.#db.pruneBatch({
@@ -4353,11 +4348,13 @@ var WorkflowsDSQL = class WorkflowsDSQL extends WorkflowsStorage {
4353
4348
  values.push(status);
4354
4349
  paramIndex++;
4355
4350
  }
4356
- if (resourceId) if (await this.#db.hasColumn(TABLE_WORKFLOW_SNAPSHOT, "resourceId")) {
4357
- conditions.push(`"resourceId" = $${paramIndex}`);
4358
- values.push(resourceId);
4359
- paramIndex++;
4360
- } else console.warn(`[${TABLE_WORKFLOW_SNAPSHOT}] resourceId column not found. Skipping resourceId filter.`);
4351
+ if (resourceId) {
4352
+ if (await this.#db.hasColumn(TABLE_WORKFLOW_SNAPSHOT, "resourceId")) {
4353
+ conditions.push(`"resourceId" = $${paramIndex}`);
4354
+ values.push(resourceId);
4355
+ paramIndex++;
4356
+ } else console.warn(`[${TABLE_WORKFLOW_SNAPSHOT}] resourceId column not found. Skipping resourceId filter.`);
4357
+ }
4361
4358
  if (fromDate) {
4362
4359
  conditions.push(`"createdAt" >= $${paramIndex}`);
4363
4360
  values.push(fromDate instanceof Date ? fromDate.toISOString() : fromDate);
@@ -4411,8 +4408,7 @@ var WorkflowsDSQL = class WorkflowsDSQL extends WorkflowsStorage {
4411
4408
  }
4412
4409
  }
4413
4410
  };
4414
- //#endregion
4415
- //#region src/storage/index.ts
4411
+ // src/storage/index.ts
4416
4412
  var DSQLStore = class extends MastraStorage {
4417
4413
  #pool;
4418
4414
  #db;
@@ -4465,7 +4461,7 @@ var DSQLStore = class extends MastraStorage {
4465
4461
  createPool(config) {
4466
4462
  if (!isHostConfig(config)) throw new Error("DSQLStore: Invalid configuration for creating pool.");
4467
4463
  const region = getEffectiveRegion(config);
4468
- return new Pool({
4464
+ const poolConfig = {
4469
4465
  host: config.host,
4470
4466
  user: config.user ?? "admin",
4471
4467
  database: config.database ?? "postgres",
@@ -4478,7 +4474,8 @@ var DSQLStore = class extends MastraStorage {
4478
4474
  maxLifetimeSeconds: config.maxLifetimeSeconds ?? DSQL_POOL_DEFAULTS.maxLifetimeSeconds,
4479
4475
  connectionTimeoutMillis: config.connectionTimeoutMillis ?? DSQL_POOL_DEFAULTS.connectionTimeoutMillis,
4480
4476
  allowExitOnIdle: config.allowExitOnIdle ?? DSQL_POOL_DEFAULTS.allowExitOnIdle
4481
- });
4477
+ };
4478
+ return new Pool(poolConfig);
4482
4479
  }
4483
4480
  async init() {
4484
4481
  if (this.isInitialized) return;
@@ -4521,7 +4518,6 @@ var DSQLStore = class extends MastraStorage {
4521
4518
  this.isInitialized = false;
4522
4519
  }
4523
4520
  };
4524
- //#endregion
4525
4521
  export { AgentsDSQL, DSQLStore, DSQL_POOL_DEFAULTS, MemoryDSQL, ObservabilityDSQL, PoolAdapter, ScoresDSQL, WorkflowsDSQL };
4526
4522
 
4527
4523
  //# sourceMappingURL=index.js.map