@mastra/cloudflare-d1 1.5.0 → 1.5.1-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.
@@ -3,7 +3,7 @@ name: mastra-cloudflare-d1
3
3
  description: Documentation for @mastra/cloudflare-d1. Use when working with @mastra/cloudflare-d1 APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/cloudflare-d1"
6
- version: "1.5.0"
6
+ version: "1.5.1-alpha.0"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,6 +1,32 @@
1
1
  {
2
- "version": "1.5.0",
2
+ "version": "1.5.1-alpha.0",
3
3
  "package": "@mastra/cloudflare-d1",
4
- "exports": {},
4
+ "exports": {
5
+ "BackgroundTasksStorageD1": {
6
+ "types": "dist/storage/domains/background-tasks/index.d.ts",
7
+ "implementation": "dist/index.js",
8
+ "line": 749
9
+ },
10
+ "D1Store": {
11
+ "types": "dist/storage/index.d.ts",
12
+ "implementation": "dist/index.js",
13
+ "line": 2885
14
+ },
15
+ "MemoryStorageD1": {
16
+ "types": "dist/storage/domains/memory/index.d.ts",
17
+ "implementation": "dist/index.js",
18
+ "line": 1087
19
+ },
20
+ "ScoresStorageD1": {
21
+ "types": "dist/storage/domains/scores/index.d.ts",
22
+ "implementation": "dist/index.js",
23
+ "line": 2061
24
+ },
25
+ "WorkflowsStorageD1": {
26
+ "types": "dist/storage/domains/workflows/index.d.ts",
27
+ "implementation": "dist/index.js",
28
+ "line": 2392
29
+ }
30
+ },
5
31
  "modules": {}
6
32
  }
package/dist/index.cjs CHANGED
@@ -2380,6 +2380,28 @@ const RUN_OWNER_COLUMNS = `generation, ownerId, leaseExpiresAt, ${DB_NOW_MS} AS
2380
2380
  * its ownership record was pruned still gets a higher generation than its fence.
2381
2381
  */
2382
2382
  const FIRST_GENERATION = `MAX(1, ${DB_NOW_MS})`;
2383
+ /**
2384
+ * The snapshot fields a `listWorkflowRuns({ summary: true })` row carries, read in SQL so the
2385
+ * snapshot itself is never sent. One multi-path `json_extract` parses each snapshot once and
2386
+ * returns `[status, timestamp, parentWorkflowId, parentRunId]` as a JSON array.
2387
+ */
2388
+ const SUMMARY_FIELDS_COLUMN = `CASE WHEN json_valid(snapshot) THEN json_extract(snapshot, '$.status', '$.timestamp', '$.parentWorkflow.workflowId', '$.parentWorkflow.runId') END AS summaryFields`;
2389
+ /**
2390
+ * The reduced snapshot of a summary row from `SUMMARY_FIELDS_COLUMN`: `status` and `timestamp`
2391
+ * when set, and `parentWorkflow` only when both of its IDs are non-empty strings. A row whose
2392
+ * snapshot isn't valid JSON reduces to `{}`.
2393
+ */
2394
+ function summarySnapshot(summaryFields) {
2395
+ const [status, timestamp, workflowId, runId] = typeof summaryFields === "string" ? JSON.parse(summaryFields) : [];
2396
+ const snapshot = {};
2397
+ if (status !== null && status !== void 0) snapshot.status = status;
2398
+ if (timestamp !== null && timestamp !== void 0) snapshot.timestamp = Number(timestamp);
2399
+ if (typeof workflowId === "string" && workflowId !== "" && typeof runId === "string" && runId !== "") snapshot.parentWorkflow = {
2400
+ workflowId,
2401
+ runId
2402
+ };
2403
+ return snapshot;
2404
+ }
2383
2405
  function toRunOwnershipRecord(runId, row) {
2384
2406
  const leaseExpiresAt = row.leaseExpiresAt === null ? null : Number(row.leaseExpiresAt);
2385
2407
  return {
@@ -2403,6 +2425,8 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends _mastra_core_storage.W
2403
2425
  anchorType: "epoch-ms"
2404
2426
  } };
2405
2427
  #db;
2428
+ /** Set once the snapshot table is seen with a `resourceId` column, which is never dropped. */
2429
+ #hasResourceIdColumn = false;
2406
2430
  constructor(config) {
2407
2431
  super();
2408
2432
  this.#db = new D1DB(resolveD1Config(config));
@@ -2699,8 +2723,12 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends _mastra_core_storage.W
2699
2723
  }, error);
2700
2724
  }
2701
2725
  }
2702
- parseWorkflowRun(row) {
2703
- let parsedSnapshot = row.snapshot;
2726
+ /**
2727
+ * Maps a row to a workflow run. With `summary`, the row has `summaryFields` instead of the
2728
+ * snapshot.
2729
+ */
2730
+ parseWorkflowRun(row, { summary } = {}) {
2731
+ let parsedSnapshot = summary ? summarySnapshot(row.summaryFields) : row.snapshot;
2704
2732
  if (typeof parsedSnapshot === "string") try {
2705
2733
  parsedSnapshot = JSON.parse(row.snapshot);
2706
2734
  } catch (e) {
@@ -2715,7 +2743,29 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends _mastra_core_storage.W
2715
2743
  resourceId: row.resourceId
2716
2744
  };
2717
2745
  }
2718
- async listWorkflowRuns({ workflowName, fromDate, toDate, page, perPage, resourceId, status } = {}) {
2746
+ /**
2747
+ * Rewrites the listing's `SELECT *` to the summary columns. The SQL builder only selects plain
2748
+ * column names, so the JSON expressions go in here. A table created before `resourceId` existed
2749
+ * is listed without that column, as `SELECT *` lists it.
2750
+ */
2751
+ async #summarySql(tableName, selectAllSql) {
2752
+ if (!selectAllSql.startsWith("SELECT *")) throw new Error(`Expected the workflow run listing to start with "SELECT *": ${selectAllSql}`);
2753
+ if (!this.#hasResourceIdColumn) this.#hasResourceIdColumn = await this.#db.hasColumn(tableName, "resourceId");
2754
+ const columns = [
2755
+ "workflow_name",
2756
+ "run_id",
2757
+ "createdAt",
2758
+ "updatedAt",
2759
+ SUMMARY_FIELDS_COLUMN
2760
+ ];
2761
+ if (this.#hasResourceIdColumn) columns.push("resourceId");
2762
+ return `SELECT ${columns.join(", ")}${selectAllSql.slice(8)}`;
2763
+ }
2764
+ /**
2765
+ * Lists runs newest first, with optional filters and pagination. With `summary`, each run's
2766
+ * snapshot holds only `status`, `timestamp` and `parentWorkflow`.
2767
+ */
2768
+ async listWorkflowRuns({ workflowName, fromDate, toDate, page, perPage, resourceId, status, summary } = {}) {
2719
2769
  const fullTableName = this.#db.getTableName(_mastra_core_storage.TABLE_WORKFLOW_SNAPSHOT);
2720
2770
  try {
2721
2771
  const builder = createSqlBuilder().select().from(fullTableName);
@@ -2745,7 +2795,8 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends _mastra_core_storage.W
2745
2795
  builder.limit(perPage);
2746
2796
  builder.offset(offset);
2747
2797
  }
2748
- const { sql, params } = builder.build();
2798
+ const { sql: selectAllSql, params } = builder.build();
2799
+ const sql = summary ? await this.#summarySql(fullTableName, selectAllSql) : selectAllSql;
2749
2800
  let total = 0;
2750
2801
  if (perPage !== void 0 && page !== void 0) {
2751
2802
  const { sql: countSql, params: countParams } = countBuilder.build();
@@ -2760,7 +2811,7 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends _mastra_core_storage.W
2760
2811
  sql,
2761
2812
  params
2762
2813
  });
2763
- const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row));
2814
+ const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row, { summary }));
2764
2815
  return {
2765
2816
  runs,
2766
2817
  total: total || runs.length