Skip to content

LRT: Interactive Transactions Epic #7956

Closed

Description

@matthewmueller

Problem

Long-running transactions are a very commonly used functionality of database that has influenced how a lot of developers think and build their applications.

While there are alternative ways to conceive a feature without them, leading to more scalable code, these ways aren't obvious and can complicate the code of applications that wouldn't suffer from long-running transaction shortcomings.

Moreover, users can't bear the cost of porting pre-existing application heavily relying on long-running transactions to a new paradigm.

Solution

Our new transaction API will allow you to add arbitrary logic within a transaction. This gives users a simple mental model for working with transactions and reduces the burden of porting existing applications to Prisma.

Code Example
import { PrismaClient } from '@prisma/client'

async function main() {
  const prisma = new PrismaClient()

  const posts = await prisma.$transaction(async (tx) => {
    // we can do our usual queries/await them
    const user = await tx.user.findUnique({
      where: {
        email: '[email protected]'
      }
    })

    if (!user) {
      // we can also throw errors of our own
      throw new Error('User does not exist')
    }

    // we can process long running operations
    const copiedPost1 = await fetchData(1)

    // data can be persisted after "long" ops
    const post1 = await tx.post.create({
      data: {
        title: copiedPost1['title'],
        content: copiedPost1['content'],
        author: {
          connect: {
              email: '[email protected]'
          }
        }
      }
    })

    // unhandled exceptions trigger rollback
    const post2 = await tx.post.create({
      data: {
        title: undefined as any as string,
        content: undefined as any as string,
        author: {
          connect: {
              email: '[email protected]'
          }
        }
      }
    })

    // we can return any custom return value
    return [post1, post2]
  })
}

// PS: we agree that this is a bad practice
function fetchData(id: string | number) {
  return fetch(`https://myservice.co/api/${id}`)
  .then(response => response.json()) as
  Promise<{title: string, content: string}>
}

Tasks

TypeScript Client

Query Engine

Note: The following issues might be easy to fix while implementing LRT, but not necessary for completing the epic.

References

Activity

  1. added and removed
    kind/feature
    on Jul 2, 2021
  2. changed the title [-][Epic] Long-Running Transactions[/-] [+]LRT: Long-Running Transactions Epic[/+] on Jul 2, 2021
  3. locked and limited conversation to collaborators on Jul 2, 2021
  4. linked a pull request that will close this issuefeat(client): interactive transactions #8384on Jul 31, 2021
  5. changed the title [-]LRT: Long-Running Transactions Epic[/-] [+]LRT: Interactive Transactions Epic[/+] on Aug 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions