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