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

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"
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",
3
3
  "package": "@mastra/cloudflare-d1",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -87,7 +87,7 @@ var SqlBuilder = class {
87
87
  */
88
88
  whereAnd(condition, ...params) {
89
89
  if (this.whereAdded) return this.andWhere(condition, ...params);
90
- else return this.where(condition, ...params);
90
+ return this.where(condition, ...params);
91
91
  }
92
92
  andWhere(condition, ...params) {
93
93
  this.sql += ` AND ${condition}`;
@@ -405,18 +405,16 @@ var D1DB = class extends _mastra_core_base.MastraBase {
405
405
  result = await statement.bind(...formattedParams).first();
406
406
  if (!result) return null;
407
407
  return result;
408
- } else {
409
- result = await statement.bind(...formattedParams).all();
410
- return result.results || [];
411
408
  }
409
+ result = await statement.bind(...formattedParams).all();
410
+ return result.results || [];
412
411
  } else if (first) {
413
412
  result = await statement.first();
414
413
  if (!result) return null;
415
414
  return result;
416
- } else {
417
- result = await statement.all();
418
- return result.results || [];
419
415
  }
416
+ result = await statement.all();
417
+ return result.results || [];
420
418
  } catch (error) {
421
419
  throw new _mastra_core_error.MastraError({
422
420
  id: (0, _mastra_core_storage.createStorageErrorId)("CLOUDFLARE_D1", "WORKERS_BINDING_QUERY", "FAILED"),
@@ -463,7 +461,7 @@ var D1DB = class extends _mastra_core_base.MastraBase {
463
461
  async executeQuery(options) {
464
462
  if (this.binding) return this.executeWorkersBindingQuery(options);
465
463
  else if (this.client) return this.executeRestQuery(options);
466
- else throw new Error("Neither binding nor client is configured");
464
+ throw new Error("Neither binding nor client is configured");
467
465
  }
468
466
  /**
469
467
  * Gets the set of column names that actually exist in the database table.
@@ -2379,6 +2377,9 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends _mastra_core_storage.W
2379
2377
  super();
2380
2378
  this.#db = new D1DB(resolveD1Config(config));
2381
2379
  }
2380
+ supportsGuardedSnapshotWrites() {
2381
+ return true;
2382
+ }
2382
2383
  supportsConcurrentUpdates() {
2383
2384
  return false;
2384
2385
  }
@@ -2555,7 +2556,7 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends _mastra_core_storage.W
2555
2556
  async updateWorkflowState(_args) {
2556
2557
  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
2558
  }
2558
- async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt, fence }) {
2559
+ async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt, fence, expectedStatus }) {
2559
2560
  const fullTableName = this.#db.getTableName(_mastra_core_storage.TABLE_WORKFLOW_SNAPSHOT);
2560
2561
  const now = (/* @__PURE__ */ new Date()).toISOString();
2561
2562
  const check = this.#runFenceCheck(fence, runId, "persistWorkflowSnapshot");
@@ -2592,7 +2593,20 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends _mastra_core_storage.W
2592
2593
  });
2593
2594
  const query = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap, runFenceGuard(check));
2594
2595
  try {
2595
- await executeFenced(this.#db, check, query.build());
2596
+ if (expectedStatus === void 0) {
2597
+ await executeFenced(this.#db, check, query.build());
2598
+ return true;
2599
+ }
2600
+ const expectedStatuses = Array.isArray(expectedStatus) ? expectedStatus : [expectedStatus];
2601
+ const { sql, params } = query.build();
2602
+ 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(", ")})`;
2603
+ const rows = await this.#db.executeQuery({
2604
+ sql: `${sql}${statusGuard} RETURNING 1 AS written`,
2605
+ params: [...params, ...expectedStatuses]
2606
+ });
2607
+ const written = Array.isArray(rows) && rows.length > 0;
2608
+ if (!written && check) await assertRunFence(this.#db, check);
2609
+ return written;
2596
2610
  } catch (error) {
2597
2611
  if ((0, _mastra_core_storage.isRunFenceConflictError)(error)) throw error;
2598
2612
  throw new _mastra_core_error.MastraError({
@@ -2607,6 +2621,26 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends _mastra_core_storage.W
2607
2621
  }, error);
2608
2622
  }
2609
2623
  }
2624
+ async loadWorkflowRunStatus({ workflowName, runId }) {
2625
+ try {
2626
+ const row = await this.#db.executeQuery({
2627
+ 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 = ?`,
2628
+ params: [workflowName, runId],
2629
+ first: true
2630
+ });
2631
+ return (row && !Array.isArray(row) ? row.status : void 0) ?? null;
2632
+ } catch (error) {
2633
+ throw new _mastra_core_error.MastraError({
2634
+ id: (0, _mastra_core_storage.createStorageErrorId)("CLOUDFLARE_D1", "LOAD_WORKFLOW_RUN_STATUS", "FAILED"),
2635
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
2636
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
2637
+ details: {
2638
+ workflowName,
2639
+ runId
2640
+ }
2641
+ }, error);
2642
+ }
2643
+ }
2610
2644
  async loadWorkflowSnapshot(params) {
2611
2645
  const { workflowName, runId } = params;
2612
2646
  this.logger.debug("Loading workflow snapshot", {