@mastra/cloudflare-d1 1.0.0-beta.10 → 1.0.0-beta.11
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/CHANGELOG.md +46 -0
- package/dist/docs/README.md +1 -1
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/SOURCE_MAP.json +1 -1
- package/dist/index.cjs +72 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +72 -11
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +2 -2
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -840,18 +840,33 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
840
840
|
return null;
|
|
841
841
|
}
|
|
842
842
|
}
|
|
843
|
-
async
|
|
844
|
-
const {
|
|
843
|
+
async listThreads(args) {
|
|
844
|
+
const { page = 0, perPage: perPageInput, orderBy, filter } = args;
|
|
845
|
+
try {
|
|
846
|
+
this.validatePaginationInput(page, perPageInput ?? 100);
|
|
847
|
+
} catch (error) {
|
|
848
|
+
throw new MastraError(
|
|
849
|
+
{
|
|
850
|
+
id: createStorageErrorId("CLOUDFLARE_D1", "LIST_THREADS", "INVALID_PAGE"),
|
|
851
|
+
domain: ErrorDomain.STORAGE,
|
|
852
|
+
category: ErrorCategory.USER,
|
|
853
|
+
details: { page, ...perPageInput !== void 0 && { perPage: perPageInput } }
|
|
854
|
+
},
|
|
855
|
+
error instanceof Error ? error : new Error("Invalid pagination parameters")
|
|
856
|
+
);
|
|
857
|
+
}
|
|
845
858
|
const perPage = normalizePerPage(perPageInput, 100);
|
|
846
|
-
|
|
859
|
+
try {
|
|
860
|
+
this.validateMetadataKeys(filter?.metadata);
|
|
861
|
+
} catch (error) {
|
|
847
862
|
throw new MastraError(
|
|
848
863
|
{
|
|
849
|
-
id: createStorageErrorId("CLOUDFLARE_D1", "
|
|
864
|
+
id: createStorageErrorId("CLOUDFLARE_D1", "LIST_THREADS", "INVALID_METADATA_KEY"),
|
|
850
865
|
domain: ErrorDomain.STORAGE,
|
|
851
866
|
category: ErrorCategory.USER,
|
|
852
|
-
details: {
|
|
867
|
+
details: { metadataKeys: filter?.metadata ? Object.keys(filter.metadata).join(", ") : "" }
|
|
853
868
|
},
|
|
854
|
-
new Error("
|
|
869
|
+
error instanceof Error ? error : new Error("Invalid metadata key")
|
|
855
870
|
);
|
|
856
871
|
}
|
|
857
872
|
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
@@ -864,11 +879,51 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
864
879
|
metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata || "{}") : row.metadata || {}
|
|
865
880
|
});
|
|
866
881
|
try {
|
|
867
|
-
|
|
882
|
+
let countQuery = createSqlBuilder().count().from(fullTableName);
|
|
883
|
+
let selectQuery = createSqlBuilder().select("*").from(fullTableName);
|
|
884
|
+
if (filter?.resourceId) {
|
|
885
|
+
countQuery = countQuery.whereAnd("resourceId = ?", filter.resourceId);
|
|
886
|
+
selectQuery = selectQuery.whereAnd("resourceId = ?", filter.resourceId);
|
|
887
|
+
}
|
|
888
|
+
if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
|
|
889
|
+
for (const [key, value] of Object.entries(filter.metadata)) {
|
|
890
|
+
if (value !== null && typeof value === "object") {
|
|
891
|
+
throw new MastraError(
|
|
892
|
+
{
|
|
893
|
+
id: createStorageErrorId("CLOUDFLARE_D1", "LIST_THREADS", "INVALID_METADATA_VALUE"),
|
|
894
|
+
domain: ErrorDomain.STORAGE,
|
|
895
|
+
category: ErrorCategory.USER,
|
|
896
|
+
text: `Metadata filter value for key "${key}" must be a scalar type (string, number, boolean, or null), got ${Array.isArray(value) ? "array" : "object"}`,
|
|
897
|
+
details: { key, valueType: Array.isArray(value) ? "array" : "object" }
|
|
898
|
+
},
|
|
899
|
+
new Error("Invalid metadata filter value type")
|
|
900
|
+
);
|
|
901
|
+
}
|
|
902
|
+
if (value === null) {
|
|
903
|
+
const condition = `json_extract(metadata, '$.${key}') IS NULL`;
|
|
904
|
+
countQuery = countQuery.whereAnd(condition);
|
|
905
|
+
selectQuery = selectQuery.whereAnd(condition);
|
|
906
|
+
} else {
|
|
907
|
+
const condition = `json_extract(metadata, '$.${key}') = ?`;
|
|
908
|
+
const filterValue = value;
|
|
909
|
+
countQuery = countQuery.whereAnd(condition, filterValue);
|
|
910
|
+
selectQuery = selectQuery.whereAnd(condition, filterValue);
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
}
|
|
868
914
|
const countResult = await this.#db.executeQuery(countQuery.build());
|
|
869
915
|
const total = Number(countResult?.[0]?.count ?? 0);
|
|
916
|
+
if (total === 0) {
|
|
917
|
+
return {
|
|
918
|
+
threads: [],
|
|
919
|
+
total: 0,
|
|
920
|
+
page,
|
|
921
|
+
perPage: perPageForResponse,
|
|
922
|
+
hasMore: false
|
|
923
|
+
};
|
|
924
|
+
}
|
|
870
925
|
const limitValue = perPageInput === false ? total : perPage;
|
|
871
|
-
|
|
926
|
+
selectQuery = selectQuery.orderBy(field, direction).limit(limitValue).offset(offset);
|
|
872
927
|
const results = await this.#db.executeQuery(selectQuery.build());
|
|
873
928
|
const threads = results.map(mapRowToStorageThreadType);
|
|
874
929
|
return {
|
|
@@ -879,13 +934,19 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
879
934
|
hasMore: perPageInput === false ? false : offset + perPage < total
|
|
880
935
|
};
|
|
881
936
|
} catch (error) {
|
|
937
|
+
if (error instanceof MastraError && error.category === ErrorCategory.USER) {
|
|
938
|
+
throw error;
|
|
939
|
+
}
|
|
882
940
|
const mastraError = new MastraError(
|
|
883
941
|
{
|
|
884
|
-
id: createStorageErrorId("CLOUDFLARE_D1", "
|
|
942
|
+
id: createStorageErrorId("CLOUDFLARE_D1", "LIST_THREADS", "FAILED"),
|
|
885
943
|
domain: ErrorDomain.STORAGE,
|
|
886
944
|
category: ErrorCategory.THIRD_PARTY,
|
|
887
|
-
text: `Error
|
|
888
|
-
details: {
|
|
945
|
+
text: `Error listing threads: ${error instanceof Error ? error.message : String(error)}`,
|
|
946
|
+
details: {
|
|
947
|
+
...filter?.resourceId && { resourceId: filter.resourceId },
|
|
948
|
+
hasMetadataFilter: !!filter?.metadata
|
|
949
|
+
}
|
|
889
950
|
},
|
|
890
951
|
error
|
|
891
952
|
);
|