@mastra/dsql 1.5.5-alpha.0 → 1.5.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
@@ -2526,11 +2526,13 @@ var MemoryDSQL = class MemoryDSQL extends MemoryStorage {
2526
2526
  queryParams.push(resourceId);
2527
2527
  }
2528
2528
  if (filter?.dateRange?.start) {
2529
- conditions.push(`"createdAtZ" >= $${paramIndex++}::timestamptz`);
2529
+ const operator = filter.dateRange.startExclusive ? ">" : ">=";
2530
+ conditions.push(`"createdAtZ" ${operator} $${paramIndex++}::timestamptz`);
2530
2531
  queryParams.push(filter.dateRange.start);
2531
2532
  }
2532
2533
  if (filter?.dateRange?.end) {
2533
- conditions.push(`"createdAtZ" <= $${paramIndex++}::timestamptz`);
2534
+ const operator = filter.dateRange.endExclusive ? "<" : "<=";
2535
+ conditions.push(`"createdAtZ" ${operator} $${paramIndex++}::timestamptz`);
2534
2536
  queryParams.push(filter.dateRange.end);
2535
2537
  }
2536
2538
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
@@ -3932,6 +3934,9 @@ var WorkflowsDSQL = class WorkflowsDSQL extends WorkflowsStorage {
3932
3934
  supportsConcurrentUpdates() {
3933
3935
  return true;
3934
3936
  }
3937
+ supportsGuardedSnapshotWrites() {
3938
+ return true;
3939
+ }
3935
3940
  /**
3936
3941
  * Returns default index definitions for the workflows domain tables.
3937
3942
  * Currently no default indexes are defined for workflows.
@@ -4226,26 +4231,29 @@ var WorkflowsDSQL = class WorkflowsDSQL extends WorkflowsStorage {
4226
4231
  }, error);
4227
4232
  }
4228
4233
  }
4229
- async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt, fence }) {
4234
+ async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt, fence, expectedStatus }) {
4230
4235
  try {
4231
4236
  const now = /* @__PURE__ */ new Date();
4232
4237
  const createdAtValue = createdAt ? createdAt : now;
4233
4238
  const updatedAtValue = updatedAt ? updatedAt : now;
4234
4239
  const resolvedFence = resolveRunFence(this, fence, runId);
4235
- await withRetry(() => withRunFence(this.#db.client, this.#runOwnersTable(), resolvedFence, "persistWorkflowSnapshot", (q) => q.none(`INSERT INTO ${getTableName({
4240
+ const guard = expectedStatus === void 0 ? "" : ` WHERE t.snapshot::jsonb ->> 'status' = ANY($7::text[])`;
4241
+ return await withRetry(() => withRunFence(this.#db.client, this.#runOwnersTable(), resolvedFence, "persistWorkflowSnapshot", (q) => q.oneOrNone(`INSERT INTO ${getTableName({
4236
4242
  indexName: TABLE_WORKFLOW_SNAPSHOT,
4237
4243
  schemaName: getSchemaName(this.#schema)
4238
- })} (workflow_name, run_id, "resourceId", snapshot, "createdAt", "updatedAt")
4244
+ })} AS t (workflow_name, run_id, "resourceId", snapshot, "createdAt", "updatedAt")
4239
4245
  VALUES ($1, $2, $3, $4, $5, $6)
4240
4246
  ON CONFLICT (workflow_name, run_id) DO UPDATE
4241
- SET "resourceId" = $3, snapshot = $4, "updatedAt" = $6`, [
4247
+ SET "resourceId" = $3, snapshot = $4, "updatedAt" = $6${guard}
4248
+ RETURNING 1 AS written`, [
4242
4249
  workflowName,
4243
4250
  runId,
4244
4251
  resourceId,
4245
4252
  JSON.stringify(snapshot),
4246
4253
  createdAtValue.toISOString(),
4247
- updatedAtValue.toISOString()
4248
- ])));
4254
+ updatedAtValue.toISOString(),
4255
+ ...expectedStatus === void 0 ? [] : [Array.isArray(expectedStatus) ? expectedStatus : [expectedStatus]]
4256
+ ]))) !== null;
4249
4257
  } catch (error) {
4250
4258
  if (isRunFenceConflictError(error)) throw error;
4251
4259
  throw new MastraError({
@@ -4255,6 +4263,24 @@ var WorkflowsDSQL = class WorkflowsDSQL extends WorkflowsStorage {
4255
4263
  }, error);
4256
4264
  }
4257
4265
  }
4266
+ async loadWorkflowRunStatus({ workflowName, runId }) {
4267
+ try {
4268
+ return (await this.#db.client.oneOrNone(`SELECT snapshot::jsonb ->> 'status' AS status FROM ${getTableName({
4269
+ indexName: TABLE_WORKFLOW_SNAPSHOT,
4270
+ schemaName: getSchemaName(this.#schema)
4271
+ })} WHERE "workflow_name" = $1 AND "run_id" = $2 ORDER BY "createdAt" DESC LIMIT 1`, [workflowName, runId]))?.status ?? null;
4272
+ } catch (error) {
4273
+ throw new MastraError({
4274
+ id: createStorageErrorId("DSQL", "LOAD_WORKFLOW_RUN_STATUS", "FAILED"),
4275
+ domain: ErrorDomain.STORAGE,
4276
+ category: ErrorCategory.THIRD_PARTY,
4277
+ details: {
4278
+ workflowName,
4279
+ runId
4280
+ }
4281
+ }, error);
4282
+ }
4283
+ }
4258
4284
  async loadWorkflowSnapshot({ workflowName, runId }) {
4259
4285
  try {
4260
4286
  const result = await this.#db.load({