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