unthrown icon indicating copy to clipboard operation
unthrown copied to clipboard

@unthrown/prisma: no coverage for batch $transaction([...])

Open btravers opened this issue 3 months ago • 0 comments

Context

$tryTransaction currently has a single signature — the interactive form:

$tryTransaction<C, T, E>(
  this: C,
  fn: (tx: Omit<C, TxDenyList>) => AsyncResult<T, E>,
  options?: { … },
): AsyncResult<T, E | PrismaQueryError>

Prisma's client also supports the batch form, $transaction([...]), which takes an array of PrismaPromises and runs them in one transaction without a callback. @unthrown/prisma 0.2.0 has no try* equivalent for it, so adopters fall back to the raw client:

// A batch `$transaction([...])` is not an interactive transaction, so
// `$tryTransaction` does not cover it: any failure here is technical and
// flows as a Defect.
return fromSafePromise(
  this.prisma.$transaction(
    data.services.map((service) =>
      this.prisma.missionPropertyRepairService.create({ data: { … }, select: … }),
    ),
  ),
).map((records) => records.map(mapPrismaToDto));

Why it matters

The batch form is the natural fit for "insert N rows atomically", and it is measurably cheaper than the interactive form (one round trip, no open transaction held across application code). Falling back to fromSafePromise + .create(...) loses everything the extension provides: a UniqueConstraintViolation or ForeignKeyViolation that would be a modeled Err via tryCreate instead arrives as an untyped Defect, so the call site cannot fold it into a domain error.

It is also a silent cliff — the try* methods are available on every delegate, so reaching for $transaction([...]) looks like ordinary usage until you notice the error channel collapsed.

Suggestion

A $tryTransaction overload (or a $tryBatchTransaction) accepting an array of the delegates' try* results, returning AsyncResult<[T1, T2, …], E1 | E2 | … | PrismaQueryError>. The awkward part is that try* returns an AsyncResult rather than a PrismaPromise, so the batch form probably needs the operations described lazily (thunks, or a builder) to keep Prisma's single-round-trip behaviour.

Happy to be told this is out of scope — in which case a line in the README saying the batch form is deliberately uncovered, and that its failures are Defects, would save the next adopter the investigation.

Found while migrating service-lease (Millenium) to unthrown.

btravers avatar Aug 11 '26 14:08 btravers