@mastra/pg 1.17.1-alpha.0 → 1.17.1

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,5 +1,5 @@
1
1
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
- import { BRANCH_SPAN_TYPES, createVectorErrorId, AgentsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_SCHEMAS, createStorageErrorId, normalizePerPage, calculatePagination, BackgroundTasksStorage, TABLE_BACKGROUND_TASKS, BlobStore, TABLE_SKILL_BLOBS, ChannelsStorage, TABLE_CHANNEL_INSTALLATIONS, TABLE_CHANNEL_CONFIG, DatasetsStorage, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, DATASETS_SCHEMA, TABLE_CONFIGS, DATASET_ITEMS_SCHEMA, DATASET_VERSIONS_SCHEMA, ensureDate, safelyParseJSON, hasErrorCode, ExperimentsStorage, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, EXPERIMENTS_SCHEMA, EXPERIMENT_RESULTS_SCHEMA, FavoritesStorage, TABLE_FAVORITES, MCPClientsStorage, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, MCPServersStorage, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, MemoryStorage, TABLE_THREADS, TABLE_RESOURCES, TABLE_MESSAGES, OBSERVATIONAL_MEMORY_TABLE_SCHEMA, NotificationsStorage, TABLE_NOTIFICATIONS, ObservabilityStorage, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, PromptBlocksStorage, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, SchedulesStorage, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, ScorerDefinitionsStorage, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, ScoresStorage, TABLE_SCORERS, SkillsStorage, TABLE_SKILLS, TABLE_SKILL_VERSIONS, ToolProviderConnectionsStorage, TABLE_TOOL_PROVIDER_CONNECTIONS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, mergeWorkflowStepResult, WorkspacesStorage, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, FactoryStorage, MastraCompositeStore, TraceStatus, getDefaultValue, parseDuration, listBranchesArgsSchema, listLogsArgsSchema, listMetricsArgsSchema, listScoresArgsSchema, listFeedbackArgsSchema, normalizeScheduleTarget, transformScoreRow as transformScoreRow$1, UniqueViolationError, getSqlType, EntityType, METRIC_DISTINCT_COLUMNS } from '@mastra/core/storage';
2
+ import { BRANCH_SPAN_TYPES, createVectorErrorId, AgentsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_SCHEMAS, createStorageErrorId, normalizePerPage, calculatePagination, BackgroundTasksStorage, TABLE_BACKGROUND_TASKS, BlobStore, TABLE_SKILL_BLOBS, ChannelsStorage, TABLE_CHANNEL_INSTALLATIONS, TABLE_CHANNEL_CONFIG, DatasetsStorage, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, DATASETS_SCHEMA, TABLE_CONFIGS, DATASET_ITEMS_SCHEMA, DATASET_VERSIONS_SCHEMA, ensureDate, safelyParseJSON, hasErrorCode, ExperimentsStorage, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, EXPERIMENTS_SCHEMA, EXPERIMENT_RESULTS_SCHEMA, FavoritesStorage, TABLE_FAVORITES, MCPClientsStorage, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, MCPServersStorage, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, MemoryStorage, TABLE_THREADS, TABLE_RESOURCES, TABLE_MESSAGES, OBSERVATIONAL_MEMORY_TABLE_SCHEMA, validateStorageMetadataFilter, storageMessageMatchesMetadataFilter, NotificationsStorage, TABLE_NOTIFICATIONS, ObservabilityStorage, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, PromptBlocksStorage, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, SchedulesStorage, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, ScorerDefinitionsStorage, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, ScoresStorage, TABLE_SCORERS, SkillsStorage, TABLE_SKILLS, TABLE_SKILL_VERSIONS, ToolProviderConnectionsStorage, TABLE_TOOL_PROVIDER_CONNECTIONS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, mergeWorkflowStepResult, WorkspacesStorage, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, FactoryStorage, MastraCompositeStore, TraceStatus, getDefaultValue, parseDuration, listBranchesArgsSchema, listLogsArgsSchema, listMetricsArgsSchema, listScoresArgsSchema, listFeedbackArgsSchema, normalizeScheduleTarget, transformScoreRow as transformScoreRow$1, UniqueViolationError, getSqlType, EntityType, METRIC_DISTINCT_COLUMNS } from '@mastra/core/storage';
3
3
  import { parseSqlIdentifier, parseFieldKey } from '@mastra/core/utils';
4
4
  import { MastraVector, validateTopK, validateUpsertInput } from '@mastra/core/vector';
5
5
  import { Mutex } from 'async-mutex';
@@ -9574,6 +9574,7 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
9574
9574
  }
9575
9575
  const perPage = normalizePerPage(perPageInput, 40);
9576
9576
  const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
9577
+ const metadataFilter = validateStorageMetadataFilter(filter?.metadata);
9577
9578
  try {
9578
9579
  const { field, direction } = this.parseOrderBy(orderBy, "ASC");
9579
9580
  const orderByStatement = `ORDER BY COALESCE("${field}Z", "${field}") ${direction}`;
@@ -9615,13 +9616,27 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
9615
9616
  hasMore: false
9616
9617
  };
9617
9618
  }
9618
- const countQuery = `SELECT COUNT(*) FROM ${tableName} ${whereClause}`;
9619
- const countResult = await this.#db.client.one(countQuery, queryParams);
9620
- const total = parseInt(countResult.count, 10);
9621
- const limitValue = perPageInput === false ? total : perPage;
9622
- const dataQuery = `${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement} LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
9623
- const rows = await this.#db.client.manyOrNone(dataQuery, [...queryParams, limitValue, offset]);
9624
- const messages = [...rows || []];
9619
+ let total;
9620
+ let messages;
9621
+ if (metadataFilter) {
9622
+ const rows = await this.#db.client.manyOrNone(
9623
+ `${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement}`,
9624
+ queryParams
9625
+ );
9626
+ const filteredRows = (rows || []).filter(
9627
+ (row) => storageMessageMatchesMetadataFilter(row.content, metadataFilter)
9628
+ );
9629
+ total = filteredRows.length;
9630
+ messages = perPageInput === false ? filteredRows : filteredRows.slice(offset, offset + perPage);
9631
+ } else {
9632
+ const countResult = await this.#db.client.one(`SELECT COUNT(*) FROM ${tableName} ${whereClause}`, queryParams);
9633
+ total = parseInt(countResult.count, 10);
9634
+ const limitValue = perPageInput === false ? total : perPage;
9635
+ const dataQuery = `${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement} LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
9636
+ const rows = await this.#db.client.manyOrNone(dataQuery, [...queryParams, limitValue, offset]);
9637
+ messages = [...rows || []];
9638
+ }
9639
+ const primaryPageCount = messages.length;
9625
9640
  if (total === 0 && messages.length === 0 && (!include || include.length === 0)) {
9626
9641
  return {
9627
9642
  messages: [],
@@ -9651,7 +9666,7 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
9651
9666
  finalMessages.filter((m) => m.threadId && threadIdSet.has(m.threadId)).map((m) => m.id)
9652
9667
  );
9653
9668
  const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
9654
- const hasMore = perPageInput !== false && !allThreadMessagesReturned && offset + perPage < total;
9669
+ const hasMore = metadataFilter ? perPageInput !== false && offset + primaryPageCount < total : perPageInput !== false && !allThreadMessagesReturned && offset + perPage < total;
9655
9670
  return {
9656
9671
  messages: finalMessages,
9657
9672
  total,
@@ -9713,6 +9728,7 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
9713
9728
  }
9714
9729
  const perPage = normalizePerPage(perPageInput, 40);
9715
9730
  const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
9731
+ const metadataFilter = validateStorageMetadataFilter(filter?.metadata);
9716
9732
  try {
9717
9733
  const { field, direction } = this.parseOrderBy(orderBy, "ASC");
9718
9734
  const orderByStatement = `ORDER BY COALESCE("${field}Z", "${field}") ${direction}`;
@@ -9758,13 +9774,26 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
9758
9774
  hasMore: false
9759
9775
  };
9760
9776
  }
9761
- const countQuery = `SELECT COUNT(*) FROM ${tableName} ${whereClause}`;
9762
- const countResult = await this.#db.client.one(countQuery, queryParams);
9763
- const total = parseInt(countResult.count, 10);
9764
- const limitValue = perPageInput === false ? total : perPage;
9765
- const dataQuery = `${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement} LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
9766
- const rows = await this.#db.client.manyOrNone(dataQuery, [...queryParams, limitValue, offset]);
9767
- const messages = [...rows || []];
9777
+ let total;
9778
+ let messages;
9779
+ if (metadataFilter) {
9780
+ const rows = await this.#db.client.manyOrNone(
9781
+ `${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement}`,
9782
+ queryParams
9783
+ );
9784
+ const filteredRows = (rows || []).filter(
9785
+ (row) => storageMessageMatchesMetadataFilter(row.content, metadataFilter)
9786
+ );
9787
+ total = filteredRows.length;
9788
+ messages = perPageInput === false ? filteredRows : filteredRows.slice(offset, offset + perPage);
9789
+ } else {
9790
+ const countResult = await this.#db.client.one(`SELECT COUNT(*) FROM ${tableName} ${whereClause}`, queryParams);
9791
+ total = parseInt(countResult.count, 10);
9792
+ const limitValue = perPageInput === false ? total : perPage;
9793
+ const dataQuery = `${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement} LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
9794
+ const rows = await this.#db.client.manyOrNone(dataQuery, [...queryParams, limitValue, offset]);
9795
+ messages = [...rows || []];
9796
+ }
9768
9797
  if (total === 0 && messages.length === 0 && (!include || include.length === 0)) {
9769
9798
  return {
9770
9799
  messages: [],