@mastra/cloudflare-d1 1.0.0-beta.9 → 1.0.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.
@@ -28,4 +28,4 @@ docs/
28
28
  ## Version
29
29
 
30
30
  Package: @mastra/cloudflare-d1
31
- Version: 1.0.0-beta.9
31
+ Version: 1.0.0
@@ -5,7 +5,7 @@ description: Documentation for @mastra/cloudflare-d1. Includes links to type def
5
5
 
6
6
  # @mastra/cloudflare-d1 Documentation
7
7
 
8
- > **Version**: 1.0.0-beta.9
8
+ > **Version**: 1.0.0
9
9
  > **Package**: @mastra/cloudflare-d1
10
10
 
11
11
  ## Quick Navigation
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.0-beta.9",
2
+ "version": "1.0.0",
3
3
  "package": "@mastra/cloudflare-d1",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -846,18 +846,33 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
846
846
  return null;
847
847
  }
848
848
  }
849
- async listThreadsByResourceId(args) {
850
- const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
849
+ async listThreads(args) {
850
+ const { page = 0, perPage: perPageInput, orderBy, filter } = args;
851
+ try {
852
+ this.validatePaginationInput(page, perPageInput ?? 100);
853
+ } catch (error$1) {
854
+ throw new error.MastraError(
855
+ {
856
+ id: storage.createStorageErrorId("CLOUDFLARE_D1", "LIST_THREADS", "INVALID_PAGE"),
857
+ domain: error.ErrorDomain.STORAGE,
858
+ category: error.ErrorCategory.USER,
859
+ details: { page, ...perPageInput !== void 0 && { perPage: perPageInput } }
860
+ },
861
+ error$1 instanceof Error ? error$1 : new Error("Invalid pagination parameters")
862
+ );
863
+ }
851
864
  const perPage = storage.normalizePerPage(perPageInput, 100);
852
- if (page < 0) {
865
+ try {
866
+ this.validateMetadataKeys(filter?.metadata);
867
+ } catch (error$1) {
853
868
  throw new error.MastraError(
854
869
  {
855
- id: storage.createStorageErrorId("CLOUDFLARE_D1", "LIST_THREADS_BY_RESOURCE_ID", "INVALID_PAGE"),
870
+ id: storage.createStorageErrorId("CLOUDFLARE_D1", "LIST_THREADS", "INVALID_METADATA_KEY"),
856
871
  domain: error.ErrorDomain.STORAGE,
857
872
  category: error.ErrorCategory.USER,
858
- details: { page }
873
+ details: { metadataKeys: filter?.metadata ? Object.keys(filter.metadata).join(", ") : "" }
859
874
  },
860
- new Error("page must be >= 0")
875
+ error$1 instanceof Error ? error$1 : new Error("Invalid metadata key")
861
876
  );
862
877
  }
863
878
  const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
@@ -870,11 +885,51 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
870
885
  metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata || "{}") : row.metadata || {}
871
886
  });
872
887
  try {
873
- const countQuery = createSqlBuilder().count().from(fullTableName).where("resourceId = ?", resourceId);
888
+ let countQuery = createSqlBuilder().count().from(fullTableName);
889
+ let selectQuery = createSqlBuilder().select("*").from(fullTableName);
890
+ if (filter?.resourceId) {
891
+ countQuery = countQuery.whereAnd("resourceId = ?", filter.resourceId);
892
+ selectQuery = selectQuery.whereAnd("resourceId = ?", filter.resourceId);
893
+ }
894
+ if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
895
+ for (const [key, value] of Object.entries(filter.metadata)) {
896
+ if (value !== null && typeof value === "object") {
897
+ throw new error.MastraError(
898
+ {
899
+ id: storage.createStorageErrorId("CLOUDFLARE_D1", "LIST_THREADS", "INVALID_METADATA_VALUE"),
900
+ domain: error.ErrorDomain.STORAGE,
901
+ category: error.ErrorCategory.USER,
902
+ text: `Metadata filter value for key "${key}" must be a scalar type (string, number, boolean, or null), got ${Array.isArray(value) ? "array" : "object"}`,
903
+ details: { key, valueType: Array.isArray(value) ? "array" : "object" }
904
+ },
905
+ new Error("Invalid metadata filter value type")
906
+ );
907
+ }
908
+ if (value === null) {
909
+ const condition = `json_extract(metadata, '$.${key}') IS NULL`;
910
+ countQuery = countQuery.whereAnd(condition);
911
+ selectQuery = selectQuery.whereAnd(condition);
912
+ } else {
913
+ const condition = `json_extract(metadata, '$.${key}') = ?`;
914
+ const filterValue = value;
915
+ countQuery = countQuery.whereAnd(condition, filterValue);
916
+ selectQuery = selectQuery.whereAnd(condition, filterValue);
917
+ }
918
+ }
919
+ }
874
920
  const countResult = await this.#db.executeQuery(countQuery.build());
875
921
  const total = Number(countResult?.[0]?.count ?? 0);
922
+ if (total === 0) {
923
+ return {
924
+ threads: [],
925
+ total: 0,
926
+ page,
927
+ perPage: perPageForResponse,
928
+ hasMore: false
929
+ };
930
+ }
876
931
  const limitValue = perPageInput === false ? total : perPage;
877
- const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId).orderBy(field, direction).limit(limitValue).offset(offset);
932
+ selectQuery = selectQuery.orderBy(field, direction).limit(limitValue).offset(offset);
878
933
  const results = await this.#db.executeQuery(selectQuery.build());
879
934
  const threads = results.map(mapRowToStorageThreadType);
880
935
  return {
@@ -885,13 +940,19 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
885
940
  hasMore: perPageInput === false ? false : offset + perPage < total
886
941
  };
887
942
  } catch (error$1) {
943
+ if (error$1 instanceof error.MastraError && error$1.category === error.ErrorCategory.USER) {
944
+ throw error$1;
945
+ }
888
946
  const mastraError = new error.MastraError(
889
947
  {
890
- id: storage.createStorageErrorId("CLOUDFLARE_D1", "LIST_THREADS_BY_RESOURCE_ID", "FAILED"),
948
+ id: storage.createStorageErrorId("CLOUDFLARE_D1", "LIST_THREADS", "FAILED"),
891
949
  domain: error.ErrorDomain.STORAGE,
892
950
  category: error.ErrorCategory.THIRD_PARTY,
893
- text: `Error getting threads by resourceId ${resourceId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
894
- details: { resourceId }
951
+ text: `Error listing threads: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
952
+ details: {
953
+ ...filter?.resourceId && { resourceId: filter.resourceId },
954
+ hasMetadataFilter: !!filter?.metadata
955
+ }
895
956
  },
896
957
  error$1
897
958
  );
@@ -1891,7 +1952,7 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
1891
1952
  try {
1892
1953
  parsedSnapshot = JSON.parse(row.snapshot);
1893
1954
  } catch (e) {
1894
- console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
1955
+ this.logger.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
1895
1956
  }
1896
1957
  }
1897
1958
  return {
@@ -1927,7 +1988,7 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
1927
1988
  builder.whereAnd("resourceId = ?", resourceId);
1928
1989
  countBuilder.whereAnd("resourceId = ?", resourceId);
1929
1990
  } else {
1930
- console.warn(`[${fullTableName}] resourceId column not found. Skipping resourceId filter.`);
1991
+ this.logger.warn(`[${fullTableName}] resourceId column not found. Skipping resourceId filter.`);
1931
1992
  }
1932
1993
  }
1933
1994
  if (fromDate) {
@@ -2030,7 +2091,7 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
2030
2091
  };
2031
2092
 
2032
2093
  // src/storage/index.ts
2033
- var D1Store = class extends storage.MastraStorage {
2094
+ var D1Store = class extends storage.MastraCompositeStore {
2034
2095
  client;
2035
2096
  binding;
2036
2097
  tablePrefix;