@mastra/dsql 1.1.2 → 1.2.0-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
@@ -1,6 +1,6 @@
1
1
  import { AuroraDSQLClient } from '@aws/aurora-dsql-node-postgres-connector';
2
2
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
3
- import { AgentsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_SCHEMAS, createStorageErrorId, normalizePerPage, calculatePagination, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ObservabilityStorage, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraStorage, TraceStatus, getSqlType, getDefaultValue, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
3
+ import { AgentsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_SCHEMAS, createStorageErrorId, normalizePerPage, calculatePagination, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, validateStorageMetadataFilter, storageMessageMatchesMetadataFilter, ObservabilityStorage, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraStorage, TraceStatus, getSqlType, getDefaultValue, transformScoreRow as transformScoreRow$1 } 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';
@@ -2479,6 +2479,7 @@ var MemoryDSQL = class _MemoryDSQL extends MemoryStorage {
2479
2479
  }
2480
2480
  const perPage = normalizePerPage(perPageInput, 40);
2481
2481
  const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
2482
+ const metadataFilter = validateStorageMetadataFilter(filter?.metadata);
2482
2483
  try {
2483
2484
  const { field, direction } = this.parseOrderBy(orderBy, "ASC");
2484
2485
  const orderByStatement = `ORDER BY "${field}" ${direction}`;
@@ -2500,13 +2501,31 @@ var MemoryDSQL = class _MemoryDSQL extends MemoryStorage {
2500
2501
  queryParams.push(filter.dateRange.end);
2501
2502
  }
2502
2503
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
2503
- const countQuery = `SELECT COUNT(*) FROM ${tableName} ${whereClause}`;
2504
- const countResult = await this.#db.client.one(countQuery, queryParams);
2505
- const total = parseInt(countResult.count, 10);
2506
- const limitValue = perPageInput === false ? total : perPage;
2507
- const dataQuery = `${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement} LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
2508
- const rows = await this.#db.client.manyOrNone(dataQuery, [...queryParams, limitValue, offset]);
2509
- const messages = [...rows || []];
2504
+ let total;
2505
+ let messages;
2506
+ if (metadataFilter) {
2507
+ const rows = await this.#db.client.manyOrNone(
2508
+ `${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement}`,
2509
+ queryParams
2510
+ );
2511
+ const filteredRows = (rows || []).filter(
2512
+ (row) => storageMessageMatchesMetadataFilter(row.content, metadataFilter)
2513
+ );
2514
+ total = filteredRows.length;
2515
+ messages = perPageInput === false ? filteredRows : filteredRows.slice(offset, offset + perPage);
2516
+ } else {
2517
+ const countResult = await this.#db.client.one(`SELECT COUNT(*) FROM ${tableName} ${whereClause}`, queryParams);
2518
+ total = parseInt(countResult.count, 10);
2519
+ const limitValue = perPageInput === false ? total : perPage;
2520
+ const dataQuery = `${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement} LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
2521
+ const rows = await this.#db.client.manyOrNone(dataQuery, [
2522
+ ...queryParams,
2523
+ limitValue,
2524
+ offset
2525
+ ]);
2526
+ messages = [...rows || []];
2527
+ }
2528
+ const primaryPageCount = messages.length;
2510
2529
  if (total === 0 && messages.length === 0 && (!include || include.length === 0)) {
2511
2530
  return {
2512
2531
  messages: [],
@@ -2550,7 +2569,7 @@ var MemoryDSQL = class _MemoryDSQL extends MemoryStorage {
2550
2569
  finalMessages.filter((m) => m.threadId && threadIdSet.has(m.threadId)).map((m) => m.id)
2551
2570
  );
2552
2571
  const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
2553
- const hasMore = perPageInput !== false && !allThreadMessagesReturned && offset + perPage < total;
2572
+ const hasMore = metadataFilter ? perPageInput !== false && offset + primaryPageCount < total : perPageInput !== false && !allThreadMessagesReturned && offset + perPage < total;
2554
2573
  return {
2555
2574
  messages: finalMessages,
2556
2575
  total,