@mastra/cloudflare-d1 1.4.2-alpha.0 → 1.4.3-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.
@@ -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.2-alpha.0"
6
+ version: "1.4.3-alpha.0"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.4.2-alpha.0",
2
+ "version": "1.4.3-alpha.0",
3
3
  "package": "@mastra/cloudflare-d1",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -447,6 +447,21 @@ var D1DB = class extends _mastra_core_base.MastraBase {
447
447
  }, error);
448
448
  }
449
449
  }
450
+ /**
451
+ * Delete up to `limit` rows keyed by `runId` whose epoch-ms `column` is older
452
+ * than `cutoff`. Used to prune the run fencing tables.
453
+ */
454
+ async pruneRunRows({ tableName, column, cutoff, limit }) {
455
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(column)) throw new Error(`Invalid retention identifier: ${tableName}.${column}`);
456
+ const table = this.getTableName(tableName);
457
+ const rows = await this.executeQuery({
458
+ sql: `DELETE FROM ${table} WHERE runId IN (
459
+ SELECT runId FROM ${table} WHERE ${column} < ? ORDER BY ${column} LIMIT ?
460
+ ) RETURNING runId`,
461
+ params: [cutoff, limit]
462
+ });
463
+ return Array.isArray(rows) ? rows.length : 0;
464
+ }
450
465
  async executeQuery(options) {
451
466
  if (this.binding) return this.executeWorkersBindingQuery(options);
452
467
  else if (this.client) return this.executeRestQuery(options);
@@ -1064,7 +1079,18 @@ function addSqliteMetadataValuePredicate(conditions, params, path, value) {
1064
1079
  conditions.push(`CASE WHEN json_valid(content) THEN json_type(content, ?) = ? AND json_extract(content, ?) = ? ELSE 0 END`);
1065
1080
  params.push(path, value ? "true" : "false", path, value ? 1 : 0);
1066
1081
  }
1067
- var MemoryStorageD1 = class extends _mastra_core_storage.MemoryStorage {
1082
+ var MemoryStorageD1 = class MemoryStorageD1 extends _mastra_core_storage.MemoryStorage {
1083
+ /**
1084
+ * Tables `prune()` can clean up. Run fences anchor on `retiredAt` (epoch ms), which stays unset until the
1085
+ * execution holding the fence settles, so a running execution's fence is
1086
+ * never pruned.
1087
+ */
1088
+ static retentionTables = { runFences: {
1089
+ table: _mastra_core_storage.TABLE_MEMORY_RUN_FENCES,
1090
+ column: "retiredAt",
1091
+ indexed: false,
1092
+ anchorType: "epoch-ms"
1093
+ } };
1068
1094
  supportsPartialThreadUpdate = true;
1069
1095
  #db;
1070
1096
  constructor(config) {
@@ -1094,6 +1120,24 @@ var MemoryStorageD1 = class extends _mastra_core_storage.MemoryStorage {
1094
1120
  schema: _mastra_core_storage.RUN_FENCING_TABLE_SCHEMAS[_mastra_core_storage.TABLE_MEMORY_RUN_FENCES]
1095
1121
  });
1096
1122
  }
1123
+ async prune(policies, options) {
1124
+ return (0, _mastra_core_storage.executeRetentionPrune)({
1125
+ domain: "memory",
1126
+ targets: (0, _mastra_core_storage.resolveRetentionTargets)({
1127
+ policies,
1128
+ descriptor: MemoryStorageD1.retentionTables,
1129
+ order: ["runFences"]
1130
+ }),
1131
+ options,
1132
+ cutoffFor: (target, now) => (0, _mastra_core_storage.retentionCutoffMs)(target.policy, now),
1133
+ deleteBatch: (_target, cutoff, limit) => this.#db.pruneRunRows({
1134
+ tableName: _mastra_core_storage.TABLE_MEMORY_RUN_FENCES,
1135
+ column: "retiredAt",
1136
+ cutoff: Number(cutoff),
1137
+ limit
1138
+ })
1139
+ });
1140
+ }
1097
1141
  async dangerouslyClearAll() {
1098
1142
  await this.#db.clearTable({ tableName: _mastra_core_storage.TABLE_MESSAGES });
1099
1143
  await this.#db.clearTable({ tableName: _mastra_core_storage.TABLE_THREADS });
@@ -2318,7 +2362,18 @@ function toRunOwnershipRecord(runId, row) {
2318
2362
  live: leaseExpiresAt !== null && leaseExpiresAt > Number(row.nowMs)
2319
2363
  };
2320
2364
  }
2321
- var WorkflowsStorageD1 = class extends _mastra_core_storage.WorkflowsStorage {
2365
+ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends _mastra_core_storage.WorkflowsStorage {
2366
+ /**
2367
+ * Tables `prune()` can clean up. Run ownership records anchor on `updatedAt` (epoch ms), which every
2368
+ * claim, renewal and release refreshes, so a live execution's record is never
2369
+ * older than its renew interval.
2370
+ */
2371
+ static retentionTables = { runOwnership: {
2372
+ table: _mastra_core_storage.TABLE_WORKFLOW_RUN_OWNERS,
2373
+ column: "updatedAt",
2374
+ indexed: false,
2375
+ anchorType: "epoch-ms"
2376
+ } };
2322
2377
  #db;
2323
2378
  constructor(config) {
2324
2379
  super();
@@ -2337,6 +2392,24 @@ var WorkflowsStorageD1 = class extends _mastra_core_storage.WorkflowsStorage {
2337
2392
  schema: _mastra_core_storage.RUN_FENCING_TABLE_SCHEMAS[_mastra_core_storage.TABLE_WORKFLOW_RUN_OWNERS]
2338
2393
  });
2339
2394
  }
2395
+ async prune(policies, options) {
2396
+ return (0, _mastra_core_storage.executeRetentionPrune)({
2397
+ domain: "workflows",
2398
+ targets: (0, _mastra_core_storage.resolveRetentionTargets)({
2399
+ policies,
2400
+ descriptor: WorkflowsStorageD1.retentionTables,
2401
+ order: ["runOwnership"]
2402
+ }),
2403
+ options,
2404
+ cutoffFor: (target, now) => (0, _mastra_core_storage.retentionCutoffMs)(target.policy, now),
2405
+ deleteBatch: (_target, cutoff, limit) => this.#db.pruneRunRows({
2406
+ tableName: _mastra_core_storage.TABLE_WORKFLOW_RUN_OWNERS,
2407
+ column: "updatedAt",
2408
+ cutoff: Number(cutoff),
2409
+ limit
2410
+ })
2411
+ });
2412
+ }
2340
2413
  async dangerouslyClearAll() {
2341
2414
  await this.#db.clearTable({ tableName: _mastra_core_storage.TABLE_WORKFLOW_SNAPSHOT });
2342
2415
  await this.#db.clearTable({ tableName: _mastra_core_storage.TABLE_WORKFLOW_RUN_OWNERS });