@mastra/cloudflare-d1 1.4.5-alpha.1 → 1.4.5-alpha.2

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.4.5-alpha.1"
6
+ version: "1.4.5-alpha.2"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.4.5-alpha.1",
2
+ "version": "1.4.5-alpha.2",
3
3
  "package": "@mastra/cloudflare-d1",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -2379,6 +2379,9 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends _mastra_core_storage.W
2379
2379
  super();
2380
2380
  this.#db = new D1DB(resolveD1Config(config));
2381
2381
  }
2382
+ supportsGuardedSnapshotWrites() {
2383
+ return true;
2384
+ }
2382
2385
  supportsConcurrentUpdates() {
2383
2386
  return false;
2384
2387
  }
@@ -2555,7 +2558,7 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends _mastra_core_storage.W
2555
2558
  async updateWorkflowState(_args) {
2556
2559
  throw new Error("updateWorkflowState is not implemented for Cloudflare D1 storage. D1 does not support atomic read-modify-write operations needed for concurrent workflow updates.");
2557
2560
  }
2558
- async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt, fence }) {
2561
+ async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt, fence, expectedStatus }) {
2559
2562
  const fullTableName = this.#db.getTableName(_mastra_core_storage.TABLE_WORKFLOW_SNAPSHOT);
2560
2563
  const now = (/* @__PURE__ */ new Date()).toISOString();
2561
2564
  const check = this.#runFenceCheck(fence, runId, "persistWorkflowSnapshot");
@@ -2592,7 +2595,20 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends _mastra_core_storage.W
2592
2595
  });
2593
2596
  const query = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap, runFenceGuard(check));
2594
2597
  try {
2595
- await executeFenced(this.#db, check, query.build());
2598
+ if (expectedStatus === void 0) {
2599
+ await executeFenced(this.#db, check, query.build());
2600
+ return true;
2601
+ }
2602
+ const expectedStatuses = Array.isArray(expectedStatus) ? expectedStatus : [expectedStatus];
2603
+ const { sql, params } = query.build();
2604
+ const statusGuard = expectedStatuses.length === 0 ? " WHERE 0" : ` WHERE (CASE WHEN json_valid(${fullTableName}.snapshot) THEN json_extract(${fullTableName}.snapshot, '$.status') END) IN (${expectedStatuses.map(() => "?").join(", ")})`;
2605
+ const rows = await this.#db.executeQuery({
2606
+ sql: `${sql}${statusGuard} RETURNING 1 AS written`,
2607
+ params: [...params, ...expectedStatuses]
2608
+ });
2609
+ const written = Array.isArray(rows) && rows.length > 0;
2610
+ if (!written && check) await assertRunFence(this.#db, check);
2611
+ return written;
2596
2612
  } catch (error) {
2597
2613
  if ((0, _mastra_core_storage.isRunFenceConflictError)(error)) throw error;
2598
2614
  throw new _mastra_core_error.MastraError({
@@ -2607,6 +2623,26 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends _mastra_core_storage.W
2607
2623
  }, error);
2608
2624
  }
2609
2625
  }
2626
+ async loadWorkflowRunStatus({ workflowName, runId }) {
2627
+ try {
2628
+ const row = await this.#db.executeQuery({
2629
+ sql: `SELECT CASE WHEN json_valid(snapshot) THEN json_extract(snapshot, '$.status') END AS status FROM ${this.#db.getTableName(_mastra_core_storage.TABLE_WORKFLOW_SNAPSHOT)} WHERE workflow_name = ? AND run_id = ?`,
2630
+ params: [workflowName, runId],
2631
+ first: true
2632
+ });
2633
+ return (row && !Array.isArray(row) ? row.status : void 0) ?? null;
2634
+ } catch (error) {
2635
+ throw new _mastra_core_error.MastraError({
2636
+ id: (0, _mastra_core_storage.createStorageErrorId)("CLOUDFLARE_D1", "LOAD_WORKFLOW_RUN_STATUS", "FAILED"),
2637
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
2638
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
2639
+ details: {
2640
+ workflowName,
2641
+ runId
2642
+ }
2643
+ }, error);
2644
+ }
2645
+ }
2610
2646
  async loadWorkflowSnapshot(params) {
2611
2647
  const { workflowName, runId } = params;
2612
2648
  this.logger.debug("Loading workflow snapshot", {