@mastra/cloudflare-d1 1.4.5-alpha.2 → 1.5.0-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.
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.
@@ -564,7 +562,7 @@ var D1DB = class extends MastraBase {
564
562
  const existingColumns = await this.getTableColumns(fullTableName);
565
563
  const existingColumnNames = new Set(existingColumns.map((col) => col.name));
566
564
  for (const [columnName, column] of Object.entries(args.schema)) if (!existingColumnNames.has(columnName) && args.ifNotExists.includes(columnName)) {
567
- const sql = `ALTER TABLE ${fullTableName} ADD COLUMN ${columnName} ${this.getSqlType(column.type)} ${this.getDefaultValue(column.type)}`;
565
+ const sql = `ALTER TABLE ${fullTableName} ADD COLUMN ${columnName} ${this.getSqlType(column.type)} ${column.nullable ? "DEFAULT NULL" : this.getDefaultValue(column.type)}`;
568
566
  await this.executeQuery({ sql });
569
567
  this.logger.debug(`Added column ${columnName} to table ${fullTableName}`);
570
568
  }
@@ -743,10 +741,13 @@ function rowToTask(row) {
743
741
  suspendedAt: row.suspendedAt ? new Date(row.suspendedAt) : void 0,
744
742
  completedAt: row.completedAt ? new Date(row.completedAt) : void 0,
745
743
  ownerId: row.ownerId || void 0,
746
- leaseExpiresAt: row.leaseExpiresAt ? new Date(row.leaseExpiresAt) : void 0
744
+ leaseExpiresAt: row.leaseExpiresAt ? new Date(row.leaseExpiresAt) : void 0,
745
+ expiresAt: row.expiresAt ? new Date(row.expiresAt) : void 0,
746
+ metadata: parseJson(row.metadata)
747
747
  };
748
748
  }
749
749
  var BackgroundTasksStorageD1 = class extends BackgroundTasksStorage {
750
+ supportsTaskExpiry = true;
750
751
  #db;
751
752
  constructor(config) {
752
753
  super();
@@ -765,7 +766,9 @@ var BackgroundTasksStorageD1 = class extends BackgroundTasksStorage {
765
766
  "suspend_payload",
766
767
  "suspendedAt",
767
768
  "ownerId",
768
- "leaseExpiresAt"
769
+ "leaseExpiresAt",
770
+ "expiresAt",
771
+ "metadata"
769
772
  ]
770
773
  });
771
774
  }
@@ -795,7 +798,9 @@ var BackgroundTasksStorageD1 = class extends BackgroundTasksStorage {
795
798
  "suspendedAt",
796
799
  "completedAt",
797
800
  "ownerId",
798
- "leaseExpiresAt"
801
+ "leaseExpiresAt",
802
+ "expiresAt",
803
+ "metadata"
799
804
  ], [
800
805
  task.id,
801
806
  task.toolCallId,
@@ -817,7 +822,9 @@ var BackgroundTasksStorageD1 = class extends BackgroundTasksStorage {
817
822
  task.suspendedAt?.toISOString() ?? null,
818
823
  task.completedAt?.toISOString() ?? null,
819
824
  task.ownerId ?? null,
820
- task.leaseExpiresAt?.toISOString() ?? null
825
+ task.leaseExpiresAt?.toISOString() ?? null,
826
+ task.expiresAt?.toISOString() ?? null,
827
+ serializeJson(task.metadata)
821
828
  ]).build();
822
829
  await this.#db.executeQuery({
823
830
  sql,
@@ -867,6 +874,14 @@ var BackgroundTasksStorageD1 = class extends BackgroundTasksStorage {
867
874
  sets.push("leaseExpiresAt = ?");
868
875
  params.push(update.leaseExpiresAt?.toISOString() ?? null);
869
876
  }
877
+ if ("expiresAt" in update) {
878
+ sets.push("expiresAt = ?");
879
+ params.push(update.expiresAt?.toISOString() ?? null);
880
+ }
881
+ if ("metadata" in update) {
882
+ sets.push("metadata = ?");
883
+ params.push(serializeJson(update.metadata));
884
+ }
870
885
  if (sets.length === 0) return false;
871
886
  params.push(taskId);
872
887
  let predicates = "";
@@ -888,6 +903,13 @@ var BackgroundTasksStorageD1 = class extends BackgroundTasksStorage {
888
903
  params.push(options.expectedLeaseExpiresAt.toISOString());
889
904
  }
890
905
  }
906
+ if (options?.expectedSuspendedAt !== void 0) {
907
+ if (options.expectedSuspendedAt === null) predicates += " AND suspendedAt IS NULL";
908
+ else {
909
+ predicates += " AND suspendedAt = ?";
910
+ params.push(options.expectedSuspendedAt.toISOString());
911
+ }
912
+ }
891
913
  const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
892
914
  const result = await this.#db.executeQuery({
893
915
  sql: `UPDATE ${fullTableName} SET ${sets.join(", ")} WHERE id = ?${predicates} RETURNING id`,
@@ -934,7 +956,12 @@ var BackgroundTasksStorageD1 = class extends BackgroundTasksStorage {
934
956
  builder = builder.whereAnd("tool_call_id = ?", filter.toolCallId);
935
957
  countBuilder = countBuilder.whereAnd("tool_call_id = ?", filter.toolCallId);
936
958
  }
937
- const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "suspendedAt" ? "suspendedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
959
+ const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "suspendedAt" ? "suspendedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : filter.dateFilterBy === "expiresAt" ? "expiresAt" : "createdAt";
960
+ if (filter.hasExpiresAt !== void 0) {
961
+ const clause = filter.hasExpiresAt ? "expiresAt IS NOT NULL" : "expiresAt IS NULL";
962
+ builder = builder.whereAnd(clause);
963
+ countBuilder = countBuilder.whereAnd(clause);
964
+ }
938
965
  if (filter.fromDate) {
939
966
  builder = builder.whereAnd(`${dateCol} >= ?`, filter.fromDate.toISOString());
940
967
  countBuilder = countBuilder.whereAnd(`${dateCol} >= ?`, filter.fromDate.toISOString());
@@ -980,7 +1007,8 @@ var BackgroundTasksStorageD1 = class extends BackgroundTasksStorage {
980
1007
  conditions.push(`status IN (${statuses.map(() => "?").join(",")})`);
981
1008
  params.push(...statuses);
982
1009
  }
983
- const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "suspendedAt" ? "suspendedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
1010
+ const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "suspendedAt" ? "suspendedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : filter.dateFilterBy === "expiresAt" ? "expiresAt" : "createdAt";
1011
+ if (filter.hasExpiresAt !== void 0) conditions.push(filter.hasExpiresAt ? "expiresAt IS NOT NULL" : "expiresAt IS NULL");
984
1012
  if (filter.fromDate) {
985
1013
  conditions.push(`${dateCol} >= ?`);
986
1014
  params.push(filter.fromDate.toISOString());