@mastra/dsql 1.4.0 → 1.5.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
@@ -1,6 +1,6 @@
1
1
  import { AuroraDSQLClient } from "@aws/aurora-dsql-node-postgres-connector";
2
2
  import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
3
- import { AgentsStorage, MastraStorage, MemoryStorage, ObservabilityStorage, ScoresStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_MESSAGES, TABLE_RESOURCES, TABLE_SCHEMAS, TABLE_SCORERS, TABLE_SPANS, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT, TraceStatus, WorkflowsStorage, calculatePagination, createStorageErrorId, getDefaultValue, getSqlType, listTracesArgsSchema, matchesExpectedWorkflowStatus, normalizePerPage, storageMessageMatchesMetadataFilter, toTraceSpans, transformScoreRow, validateStorageMetadataFilter } from "@mastra/core/storage";
3
+ import { AgentsStorage, MastraStorage, MemoryStorage, ObservabilityStorage, ScoresStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_MESSAGES, TABLE_RESOURCES, TABLE_SCHEMAS, TABLE_SCORERS, TABLE_SPANS, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT, TraceStatus, WorkflowsStorage, calculatePagination, createStorageErrorId, executeRetentionPrune, getDefaultValue, getSqlType, listTracesArgsSchema, matchesExpectedWorkflowStatus, normalizePerPage, resolveRetentionTargets, retentionCutoffMs, storageMessageMatchesMetadataFilter, toTraceSpans, transformScoreRow, validateStorageMetadataFilter } from "@mastra/core/storage";
4
4
  import { Pool } from "pg";
5
5
  import { MastraBase } from "@mastra/core/base";
6
6
  import { parseSqlIdentifier } from "@mastra/core/utils";
@@ -662,6 +662,16 @@ var DsqlDB = class extends MastraBase {
662
662
  }, error);
663
663
  });
664
664
  }
665
+ async pruneBatch({ tableName, column, cutoff, limit }) {
666
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(column)) throw new Error(`Invalid retention identifier: ${tableName}.${column}`);
667
+ const table = getTableName$1({
668
+ indexName: tableName,
669
+ schemaName: getSchemaName$1(this.schemaName)
670
+ });
671
+ return (await this.client.query(`DELETE FROM ${table} WHERE ("traceId", "spanId") IN (
672
+ SELECT "traceId", "spanId" FROM ${table} WHERE "${column}" < $1 ORDER BY "${column}" LIMIT $2
673
+ )`, [cutoff, limit])).rowCount ?? 0;
674
+ }
665
675
  async clearTable({ tableName }) {
666
676
  try {
667
677
  await withRetry(async () => {
@@ -2773,6 +2783,11 @@ var MemoryDSQL = class MemoryDSQL extends MemoryStorage {
2773
2783
  //#endregion
2774
2784
  //#region src/storage/domains/observability/index.ts
2775
2785
  var ObservabilityDSQL = class ObservabilityDSQL extends ObservabilityStorage {
2786
+ static retentionTables = { spans: {
2787
+ table: TABLE_SPANS,
2788
+ column: "startedAtZ",
2789
+ indexed: true
2790
+ } };
2776
2791
  #db;
2777
2792
  #schema;
2778
2793
  #skipDefaultIndexes;
@@ -2791,6 +2806,24 @@ var ObservabilityDSQL = class ObservabilityDSQL extends ObservabilityStorage {
2791
2806
  this.#skipDefaultIndexes = skipDefaultIndexes;
2792
2807
  this.#indexes = indexes?.filter((idx) => ObservabilityDSQL.MANAGED_TABLES.includes(idx.table));
2793
2808
  }
2809
+ async prune(policies, options) {
2810
+ return executeRetentionPrune({
2811
+ domain: "observability",
2812
+ targets: resolveRetentionTargets({
2813
+ policies,
2814
+ descriptor: ObservabilityDSQL.retentionTables,
2815
+ order: ["spans"]
2816
+ }),
2817
+ options,
2818
+ cutoffFor: (target, now) => new Date(retentionCutoffMs(target.policy, now)),
2819
+ deleteBatch: (target, cutoff, limit) => this.#db.pruneBatch({
2820
+ tableName: target.table,
2821
+ column: target.column,
2822
+ cutoff,
2823
+ limit
2824
+ })
2825
+ });
2826
+ }
2794
2827
  async init() {
2795
2828
  await this.#db.createTable({
2796
2829
  tableName: TABLE_SPANS,
@@ -2805,6 +2838,11 @@ var ObservabilityDSQL = class ObservabilityDSQL extends ObservabilityStorage {
2805
2838
  getDefaultIndexDefinitions() {
2806
2839
  const schemaPrefix = this.#schema !== "public" ? `${this.#schema}_` : "";
2807
2840
  return [
2841
+ {
2842
+ name: `${schemaPrefix}mastra_ai_spans_startedatz_idx`,
2843
+ table: TABLE_SPANS,
2844
+ columns: ["startedAtZ"]
2845
+ },
2808
2846
  {
2809
2847
  name: `${schemaPrefix}mastra_ai_spans_traceid_startedat_idx`,
2810
2848
  table: TABLE_SPANS,
@@ -4053,7 +4091,8 @@ var DSQLStore = class extends MastraStorage {
4053
4091
  super({
4054
4092
  id: config.id,
4055
4093
  name: "DSQLStore",
4056
- disableInit: config.disableInit
4094
+ disableInit: config.disableInit,
4095
+ retention: config.retention
4057
4096
  });
4058
4097
  this.schema = config.schemaName || "public";
4059
4098
  if (isPoolConfig(config)) {