@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.
package/dist/index.js CHANGED
@@ -2357,6 +2357,28 @@ const RUN_OWNER_COLUMNS = `generation, ownerId, leaseExpiresAt, ${DB_NOW_MS} AS
2357
2357
  * its ownership record was pruned still gets a higher generation than its fence.
2358
2358
  */
2359
2359
  const FIRST_GENERATION = `MAX(1, ${DB_NOW_MS})`;
2360
+ /**
2361
+ * The snapshot fields a `listWorkflowRuns({ summary: true })` row carries, read in SQL so the
2362
+ * snapshot itself is never sent. One multi-path `json_extract` parses each snapshot once and
2363
+ * returns `[status, timestamp, parentWorkflowId, parentRunId]` as a JSON array.
2364
+ */
2365
+ const SUMMARY_FIELDS_COLUMN = `CASE WHEN json_valid(snapshot) THEN json_extract(snapshot, '$.status', '$.timestamp', '$.parentWorkflow.workflowId', '$.parentWorkflow.runId') END AS summaryFields`;
2366
+ /**
2367
+ * The reduced snapshot of a summary row from `SUMMARY_FIELDS_COLUMN`: `status` and `timestamp`
2368
+ * when set, and `parentWorkflow` only when both of its IDs are non-empty strings. A row whose
2369
+ * snapshot isn't valid JSON reduces to `{}`.
2370
+ */
2371
+ function summarySnapshot(summaryFields) {
2372
+ const [status, timestamp, workflowId, runId] = typeof summaryFields === "string" ? JSON.parse(summaryFields) : [];
2373
+ const snapshot = {};
2374
+ if (status !== null && status !== void 0) snapshot.status = status;
2375
+ if (timestamp !== null && timestamp !== void 0) snapshot.timestamp = Number(timestamp);
2376
+ if (typeof workflowId === "string" && workflowId !== "" && typeof runId === "string" && runId !== "") snapshot.parentWorkflow = {
2377
+ workflowId,
2378
+ runId
2379
+ };
2380
+ return snapshot;
2381
+ }
2360
2382
  function toRunOwnershipRecord(runId, row) {
2361
2383
  const leaseExpiresAt = row.leaseExpiresAt === null ? null : Number(row.leaseExpiresAt);
2362
2384
  return {
@@ -2380,6 +2402,8 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2380
2402
  anchorType: "epoch-ms"
2381
2403
  } };
2382
2404
  #db;
2405
+ /** Set once the snapshot table is seen with a `resourceId` column, which is never dropped. */
2406
+ #hasResourceIdColumn = false;
2383
2407
  constructor(config) {
2384
2408
  super();
2385
2409
  this.#db = new D1DB(resolveD1Config(config));
@@ -2676,8 +2700,12 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2676
2700
  }, error);
2677
2701
  }
2678
2702
  }
2679
- parseWorkflowRun(row) {
2680
- let parsedSnapshot = row.snapshot;
2703
+ /**
2704
+ * Maps a row to a workflow run. With `summary`, the row has `summaryFields` instead of the
2705
+ * snapshot.
2706
+ */
2707
+ parseWorkflowRun(row, { summary } = {}) {
2708
+ let parsedSnapshot = summary ? summarySnapshot(row.summaryFields) : row.snapshot;
2681
2709
  if (typeof parsedSnapshot === "string") try {
2682
2710
  parsedSnapshot = JSON.parse(row.snapshot);
2683
2711
  } catch (e) {
@@ -2692,7 +2720,29 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2692
2720
  resourceId: row.resourceId
2693
2721
  };
2694
2722
  }
2695
- async listWorkflowRuns({ workflowName, fromDate, toDate, page, perPage, resourceId, status } = {}) {
2723
+ /**
2724
+ * Rewrites the listing's `SELECT *` to the summary columns. The SQL builder only selects plain
2725
+ * column names, so the JSON expressions go in here. A table created before `resourceId` existed
2726
+ * is listed without that column, as `SELECT *` lists it.
2727
+ */
2728
+ async #summarySql(tableName, selectAllSql) {
2729
+ if (!selectAllSql.startsWith("SELECT *")) throw new Error(`Expected the workflow run listing to start with "SELECT *": ${selectAllSql}`);
2730
+ if (!this.#hasResourceIdColumn) this.#hasResourceIdColumn = await this.#db.hasColumn(tableName, "resourceId");
2731
+ const columns = [
2732
+ "workflow_name",
2733
+ "run_id",
2734
+ "createdAt",
2735
+ "updatedAt",
2736
+ SUMMARY_FIELDS_COLUMN
2737
+ ];
2738
+ if (this.#hasResourceIdColumn) columns.push("resourceId");
2739
+ return `SELECT ${columns.join(", ")}${selectAllSql.slice(8)}`;
2740
+ }
2741
+ /**
2742
+ * Lists runs newest first, with optional filters and pagination. With `summary`, each run's
2743
+ * snapshot holds only `status`, `timestamp` and `parentWorkflow`.
2744
+ */
2745
+ async listWorkflowRuns({ workflowName, fromDate, toDate, page, perPage, resourceId, status, summary } = {}) {
2696
2746
  const fullTableName = this.#db.getTableName(TABLE_WORKFLOW_SNAPSHOT);
2697
2747
  try {
2698
2748
  const builder = createSqlBuilder().select().from(fullTableName);
@@ -2722,7 +2772,8 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2722
2772
  builder.limit(perPage);
2723
2773
  builder.offset(offset);
2724
2774
  }
2725
- const { sql, params } = builder.build();
2775
+ const { sql: selectAllSql, params } = builder.build();
2776
+ const sql = summary ? await this.#summarySql(fullTableName, selectAllSql) : selectAllSql;
2726
2777
  let total = 0;
2727
2778
  if (perPage !== void 0 && page !== void 0) {
2728
2779
  const { sql: countSql, params: countParams } = countBuilder.build();
@@ -2737,7 +2788,7 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2737
2788
  sql,
2738
2789
  params
2739
2790
  });
2740
- const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row));
2791
+ const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row, { summary }));
2741
2792
  return {
2742
2793
  runs,
2743
2794
  total: total || runs.length