Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LRT: Interactive Transactions Epic #7956

Closed
23 tasks done
matthewmueller opened this issue Jun 30, 2021 · 0 comments · Fixed by #8384
Closed
23 tasks done

LRT: Interactive Transactions Epic #7956

matthewmueller opened this issue Jun 30, 2021 · 0 comments · Fixed by #8384
Assignees
Labels
kind/epic A high-level initiative that has many subtasks. team/client Issue for team Client. topic: interactiveTransactions topic: transaction

Comments

@matthewmueller
Copy link
Contributor

matthewmueller commented Jun 30, 2021

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: 'john@doe.io'
      }
    })

    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: 'john@doe.io'
          }
        }
      }
    })

    // 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: 'wrong@email.io'
          }
        }
      }
    })

    // 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

@matthewmueller matthewmueller self-assigned this Jun 30, 2021
@matthewmueller matthewmueller added kind/epic A high-level initiative that has many subtasks. and removed kind/feature A request for a new feature. labels Jul 2, 2021
@matthewmueller matthewmueller changed the title [Epic] Long-Running Transactions LRT: Long-Running Transactions Epic Jul 2, 2021
@prisma prisma locked and limited conversation to collaborators Jul 2, 2021
@millsp millsp linked a pull request Jul 31, 2021 that will close this issue
@thebiglabasky thebiglabasky changed the title LRT: Long-Running Transactions Epic LRT: Interactive Transactions Epic Aug 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/epic A high-level initiative that has many subtasks. team/client Issue for team Client. topic: interactiveTransactions topic: transaction
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant