@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.
package/dist/index.js CHANGED
@@ -64,7 +64,7 @@ var SqlBuilder = class {
64
64
  */
65
65
  whereAnd(condition, ...params) {
66
66
  if (this.whereAdded) return this.andWhere(condition, ...params);
67
- else return this.where(condition, ...params);
67
+ return this.where(condition, ...params);
68
68
  }
69
69
  andWhere(condition, ...params) {
70
70
  this.sql += ` AND ${condition}`;
@@ -382,18 +382,16 @@ var D1DB = class extends MastraBase {
382
382
  result = await statement.bind(...formattedParams).first();
383
383
  if (!result) return null;
384
384
  return result;
385
- } else {
386
- result = await statement.bind(...formattedParams).all();
387
- return result.results || [];
388
385
  }
386
+ result = await statement.bind(...formattedParams).all();
387
+ return result.results || [];
389
388
  } else if (first) {
390
389
  result = await statement.first();
391
390
  if (!result) return null;
392
391
  return result;
393
- } else {
394
- result = await statement.all();
395
- return result.results || [];
396
392
  }
393
+ result = await statement.all();
394
+ return result.results || [];
397
395
  } catch (error) {
398
396
  throw new MastraError({
399
397
  id: createStorageErrorId("CLOUDFLARE_D1", "WORKERS_BINDING_QUERY", "FAILED"),
@@ -440,7 +438,7 @@ var D1DB = class extends MastraBase {
440
438
  async executeQuery(options) {
441
439
  if (this.binding) return this.executeWorkersBindingQuery(options);
442
440
  else if (this.client) return this.executeRestQuery(options);
443
- else throw new Error("Neither binding nor client is configured");
441
+ throw new Error("Neither binding nor client is configured");
444
442
  }
445
443
  /**
446
444
  * Gets the set of column names that actually exist in the database table.
@@ -2356,6 +2354,9 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2356
2354
  super();
2357
2355
  this.#db = new D1DB(resolveD1Config(config));
2358
2356
  }
2357
+ supportsGuardedSnapshotWrites() {
2358
+ return true;
2359
+ }
2359
2360
  supportsConcurrentUpdates() {
2360
2361
  return false;
2361
2362
  }
@@ -2532,7 +2533,7 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2532
2533
  async updateWorkflowState(_args) {
2533
2534
  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.");
2534
2535
  }
2535
- async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt, fence }) {
2536
+ async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt, fence, expectedStatus }) {
2536
2537
  const fullTableName = this.#db.getTableName(TABLE_WORKFLOW_SNAPSHOT);
2537
2538
  const now = (/* @__PURE__ */ new Date()).toISOString();
2538
2539
  const check = this.#runFenceCheck(fence, runId, "persistWorkflowSnapshot");
@@ -2569,7 +2570,20 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2569
2570
  });
2570
2571
  const query = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap, runFenceGuard(check));
2571
2572
  try {
2572
- await executeFenced(this.#db, check, query.build());
2573
+ if (expectedStatus === void 0) {
2574
+ await executeFenced(this.#db, check, query.build());
2575
+ return true;
2576
+ }
2577
+ const expectedStatuses = Array.isArray(expectedStatus) ? expectedStatus : [expectedStatus];
2578
+ const { sql, params } = query.build();
2579
+ 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(", ")})`;
2580
+ const rows = await this.#db.executeQuery({
2581
+ sql: `${sql}${statusGuard} RETURNING 1 AS written`,
2582
+ params: [...params, ...expectedStatuses]
2583
+ });
2584
+ const written = Array.isArray(rows) && rows.length > 0;
2585
+ if (!written && check) await assertRunFence(this.#db, check);
2586
+ return written;
2573
2587
  } catch (error) {
2574
2588
  if (isRunFenceConflictError(error)) throw error;
2575
2589
  throw new MastraError({
@@ -2584,6 +2598,26 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2584
2598
  }, error);
2585
2599
  }
2586
2600
  }
2601
+ async loadWorkflowRunStatus({ workflowName, runId }) {
2602
+ try {
2603
+ const row = await this.#db.executeQuery({
2604
+ sql: `SELECT CASE WHEN json_valid(snapshot) THEN json_extract(snapshot, '$.status') END AS status FROM ${this.#db.getTableName(TABLE_WORKFLOW_SNAPSHOT)} WHERE workflow_name = ? AND run_id = ?`,
2605
+ params: [workflowName, runId],
2606
+ first: true
2607
+ });
2608
+ return (row && !Array.isArray(row) ? row.status : void 0) ?? null;
2609
+ } catch (error) {
2610
+ throw new MastraError({
2611
+ id: createStorageErrorId("CLOUDFLARE_D1", "LOAD_WORKFLOW_RUN_STATUS", "FAILED"),
2612
+ domain: ErrorDomain.STORAGE,
2613
+ category: ErrorCategory.THIRD_PARTY,
2614
+ details: {
2615
+ workflowName,
2616
+ runId
2617
+ }
2618
+ }, error);
2619
+ }
2620
+ }
2587
2621
  async loadWorkflowSnapshot(params) {
2588
2622
  const { workflowName, runId } = params;
2589
2623
  this.logger.debug("Loading workflow snapshot", {