@mastra/cloudflare-d1 1.4.4-alpha.0 → 1.4.4

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
@@ -287,10 +287,11 @@ async function assertRunFence(db, { claimsTable, fence, operation }) {
287
287
  params: [fence.runId],
288
288
  first: true
289
289
  });
290
- if (!matchesRunFence(row ? {
290
+ const current = row ? {
291
291
  generation: Number(row.generation),
292
292
  ownerId: String(row.ownerId)
293
- } : null, fence)) throw new RunFenceConflictError(fence, operation);
293
+ } : null;
294
+ if (!matchesRunFence(current, fence)) throw new RunFenceConflictError(fence, operation);
294
295
  }
295
296
  /**
296
297
  * Runs one statement of a fenced write. With a check, `statement` must carry
@@ -379,15 +380,16 @@ var D1DB = class extends MastraBase {
379
380
  const statement = this.binding.prepare(sql);
380
381
  const formattedParams = this.formatSqlParams(params);
381
382
  let result;
382
- if (formattedParams.length > 0) if (first) {
383
- result = await statement.bind(...formattedParams).first();
384
- if (!result) return null;
385
- return result;
386
- } else {
387
- result = await statement.bind(...formattedParams).all();
388
- return result.results || [];
389
- }
390
- else if (first) {
383
+ if (formattedParams.length > 0) {
384
+ if (first) {
385
+ result = await statement.bind(...formattedParams).first();
386
+ if (!result) return null;
387
+ return result;
388
+ } else {
389
+ result = await statement.bind(...formattedParams).all();
390
+ return result.results || [];
391
+ }
392
+ } else if (first) {
391
393
  result = await statement.first();
392
394
  if (!result) return null;
393
395
  return result;
@@ -876,15 +878,19 @@ var BackgroundTasksStorageD1 = class extends BackgroundTasksStorage {
876
878
  predicates += " AND status = ?";
877
879
  params.push(options.expectedStatus);
878
880
  }
879
- if (options?.expectedOwnerId !== void 0) if (options.expectedOwnerId === null) predicates += " AND ownerId IS NULL";
880
- else {
881
- predicates += " AND ownerId = ?";
882
- params.push(options.expectedOwnerId);
881
+ if (options?.expectedOwnerId !== void 0) {
882
+ if (options.expectedOwnerId === null) predicates += " AND ownerId IS NULL";
883
+ else {
884
+ predicates += " AND ownerId = ?";
885
+ params.push(options.expectedOwnerId);
886
+ }
883
887
  }
884
- if (options?.expectedLeaseExpiresAt !== void 0) if (options.expectedLeaseExpiresAt === null) predicates += " AND leaseExpiresAt IS NULL";
885
- else {
886
- predicates += " AND leaseExpiresAt = ?";
887
- params.push(options.expectedLeaseExpiresAt.toISOString());
888
+ if (options?.expectedLeaseExpiresAt !== void 0) {
889
+ if (options.expectedLeaseExpiresAt === null) predicates += " AND leaseExpiresAt IS NULL";
890
+ else {
891
+ predicates += " AND leaseExpiresAt = ?";
892
+ params.push(options.expectedLeaseExpiresAt.toISOString());
893
+ }
888
894
  }
889
895
  const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
890
896
  const result = await this.#db.executeQuery({
@@ -1097,13 +1103,14 @@ var MemoryStorageD1 = class MemoryStorageD1 extends MemoryStorage {
1097
1103
  });
1098
1104
  }
1099
1105
  async prune(policies, options) {
1106
+ const targets = resolveRetentionTargets({
1107
+ policies,
1108
+ descriptor: MemoryStorageD1.retentionTables,
1109
+ order: ["runFences"]
1110
+ });
1100
1111
  return executeRetentionPrune({
1101
1112
  domain: "memory",
1102
- targets: resolveRetentionTargets({
1103
- policies,
1104
- descriptor: MemoryStorageD1.retentionTables,
1105
- order: ["runFences"]
1106
- }),
1113
+ targets,
1107
1114
  options,
1108
1115
  cutoffFor: (target, now) => retentionCutoffMs(target.policy, now),
1109
1116
  deleteBatch: (_target, cutoff, limit) => this.#db.pruneRunRows({
@@ -2096,9 +2103,10 @@ var ScoresStorageD1 = class extends ScoresStorage {
2096
2103
  try {
2097
2104
  const fullTableName = this.#db.getTableName(TABLE_SCORERS);
2098
2105
  const serializedRecord = {};
2099
- for (const [key, value] of Object.entries(parsedScore)) if (value !== null && value !== void 0) if (typeof value === "object") serializedRecord[key] = JSON.stringify(value);
2100
- else serializedRecord[key] = value;
2101
- else serializedRecord[key] = null;
2106
+ for (const [key, value] of Object.entries(parsedScore)) if (value !== null && value !== void 0) {
2107
+ if (typeof value === "object") serializedRecord[key] = JSON.stringify(value);
2108
+ else serializedRecord[key] = value;
2109
+ } else serializedRecord[key] = null;
2102
2110
  const now = /* @__PURE__ */ new Date();
2103
2111
  serializedRecord.id = id;
2104
2112
  serializedRecord.createdAt = now.toISOString();
@@ -2369,13 +2377,14 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2369
2377
  });
2370
2378
  }
2371
2379
  async prune(policies, options) {
2380
+ const targets = resolveRetentionTargets({
2381
+ policies,
2382
+ descriptor: WorkflowsStorageD1.retentionTables,
2383
+ order: ["runOwnership"]
2384
+ });
2372
2385
  return executeRetentionPrune({
2373
2386
  domain: "workflows",
2374
- targets: resolveRetentionTargets({
2375
- policies,
2376
- descriptor: WorkflowsStorageD1.retentionTables,
2377
- order: ["runOwnership"]
2378
- }),
2387
+ targets,
2379
2388
  options,
2380
2389
  cutoffFor: (target, now) => retentionCutoffMs(target.policy, now),
2381
2390
  deleteBatch: (_target, cutoff, limit) => this.#db.pruneRunRows({
@@ -2636,10 +2645,12 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2636
2645
  builder.whereAnd("CASE WHEN json_valid(snapshot) THEN json_extract(snapshot, '$.status') END = ?", status);
2637
2646
  countBuilder.whereAnd("CASE WHEN json_valid(snapshot) THEN json_extract(snapshot, '$.status') END = ?", status);
2638
2647
  }
2639
- if (resourceId) if (await this.#db.hasColumn(fullTableName, "resourceId")) {
2640
- builder.whereAnd("resourceId = ?", resourceId);
2641
- countBuilder.whereAnd("resourceId = ?", resourceId);
2642
- } else this.logger.warn(`[${fullTableName}] resourceId column not found. Skipping resourceId filter.`);
2648
+ if (resourceId) {
2649
+ if (await this.#db.hasColumn(fullTableName, "resourceId")) {
2650
+ builder.whereAnd("resourceId = ?", resourceId);
2651
+ countBuilder.whereAnd("resourceId = ?", resourceId);
2652
+ } else this.logger.warn(`[${fullTableName}] resourceId column not found. Skipping resourceId filter.`);
2653
+ }
2643
2654
  if (fromDate) {
2644
2655
  builder.whereAnd("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
2645
2656
  countBuilder.whereAnd("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);