@mastra/cloudflare-d1 1.1.1 → 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,5 +1,5 @@
1
1
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
- import { BackgroundTasksStorage, TABLE_SCHEMAS, TABLE_BACKGROUND_TASKS, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ensureDate, createStorageErrorId, normalizePerPage, calculatePagination, serializeDate, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraCompositeStore, getSqlType, getDefaultValue, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
2
+ import { BackgroundTasksStorage, TABLE_SCHEMAS, TABLE_BACKGROUND_TASKS, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ensureDate, createStorageErrorId, normalizePerPage, calculatePagination, validateStorageMetadataFilter, serializeDate, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraCompositeStore, getSqlType, getDefaultValue, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
3
3
  import Cloudflare from 'cloudflare';
4
4
  import { MastraBase } from '@mastra/core/base';
5
5
  import { parseSqlIdentifier } from '@mastra/core/utils';
@@ -991,6 +991,40 @@ var BackgroundTasksStorageD1 = class extends BackgroundTasksStorage {
991
991
  return Number(row?.count ?? 0);
992
992
  }
993
993
  };
994
+ function addSqliteMetadataFilter(conditions, params, metadataFilter) {
995
+ if (!metadataFilter) return;
996
+ for (const [key, value] of Object.entries(metadataFilter)) {
997
+ const path = `$.metadata.${key}`;
998
+ conditions.push(`CASE WHEN json_valid(content) THEN json_type(content, ?) IS NOT NULL ELSE 0 END`);
999
+ params.push(path);
1000
+ addSqliteMetadataValuePredicate(conditions, params, path, value);
1001
+ }
1002
+ }
1003
+ function addSqliteMetadataValuePredicate(conditions, params, path, value) {
1004
+ if (value === null) {
1005
+ conditions.push(`CASE WHEN json_valid(content) THEN json_type(content, ?) = 'null' ELSE 0 END`);
1006
+ params.push(path);
1007
+ return;
1008
+ }
1009
+ if (typeof value === "string") {
1010
+ conditions.push(
1011
+ `CASE WHEN json_valid(content) THEN json_type(content, ?) = 'text' AND json_extract(content, ?) = ? ELSE 0 END`
1012
+ );
1013
+ params.push(path, path, value);
1014
+ return;
1015
+ }
1016
+ if (typeof value === "number") {
1017
+ conditions.push(
1018
+ `CASE WHEN json_valid(content) THEN json_type(content, ?) IN ('integer', 'real') AND json_extract(content, ?) = ? ELSE 0 END`
1019
+ );
1020
+ params.push(path, path, value);
1021
+ return;
1022
+ }
1023
+ conditions.push(
1024
+ `CASE WHEN json_valid(content) THEN json_type(content, ?) = ? AND json_extract(content, ?) = ? ELSE 0 END`
1025
+ );
1026
+ params.push(path, value ? "true" : "false", path, value ? 1 : 0);
1027
+ }
994
1028
  var MemoryStorageD1 = class extends MemoryStorage {
995
1029
  #db;
996
1030
  constructor(config) {
@@ -1596,6 +1630,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
1596
1630
  }
1597
1631
  const perPage = normalizePerPage(perPageInput, 40);
1598
1632
  const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1633
+ const metadataFilter = validateStorageMetadataFilter(filter?.metadata);
1599
1634
  try {
1600
1635
  const fullTableName = this.#db.getTableName(TABLE_MESSAGES);
1601
1636
  let query = `
@@ -1621,6 +1656,11 @@ var MemoryStorageD1 = class extends MemoryStorage {
1621
1656
  query += ` AND createdAt ${endOp} ?`;
1622
1657
  queryParams.push(endDate);
1623
1658
  }
1659
+ const metadataConditions = [];
1660
+ addSqliteMetadataFilter(metadataConditions, queryParams, metadataFilter);
1661
+ if (metadataConditions.length > 0) {
1662
+ query += ` AND ${metadataConditions.join(" AND ")}`;
1663
+ }
1624
1664
  const { field, direction } = this.parseOrderBy(orderBy, "ASC");
1625
1665
  if (perPage === 0 && (!include || include.length === 0)) {
1626
1666
  return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
@@ -1672,6 +1712,11 @@ var MemoryStorageD1 = class extends MemoryStorage {
1672
1712
  countQuery += ` AND createdAt ${endOp} ?`;
1673
1713
  countParams.push(endDate);
1674
1714
  }
1715
+ const countMetadataConditions = [];
1716
+ addSqliteMetadataFilter(countMetadataConditions, countParams, metadataFilter);
1717
+ if (countMetadataConditions.length > 0) {
1718
+ countQuery += ` AND ${countMetadataConditions.join(" AND ")}`;
1719
+ }
1675
1720
  const countResult = await this.#db.executeQuery({ sql: countQuery, params: countParams });
1676
1721
  const total = Number(countResult[0]?.count ?? 0);
1677
1722
  if (total === 0 && paginatedCount === 0 && (!include || include.length === 0)) {
@@ -1700,8 +1745,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
1700
1745
  const list = new MessageList().add(paginatedMessages, "memory");
1701
1746
  const finalMessages = this._sortMessages(list.get.all.db(), field, direction);
1702
1747
  const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
1703
- const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
1704
- const hasMore = perPageInput === false ? false : allThreadMessagesReturned ? false : offset + paginatedCount < total;
1748
+ const hasMore = perPageInput !== false && (metadataFilter || returnedThreadMessageIds.size < total) && offset + paginatedCount < total;
1705
1749
  return {
1706
1750
  messages: finalMessages,
1707
1751
  total,