Repository navigation
Open
Labels
Description
Problem
Currently there's no way to cancel a Prisma query. Some queries are long-running and consume a lot of database resources, therefore it would be great if we had the ability to cancel them.
Example scenarios:
- Background job running some long queries is asynchronously cancelled. The db queries should also be immediately cancelled so that we can cleanup and close the job.
- We issue HTTP request and a Prisma query in parallel. HTTP request fails, so there's no point in waiting for Prisma query to finish - it can be cancelled.
Note that databases natively support cancelling queries (#1, #2, #3)
Suggested solution
It seems that AbortController is becoming a standard for cancelling async operations. It's implemented in all major runtimes, including Node > 15.
- It's used in
fetch - It's used in a number of nodejs APIs, e.g.
child_process: https://nodejs.dev/en/api/v18/child_process/ - It's discussed as a solution to query cancellation problem in other ORMs (knex, sequelize)
- It's supported in a number of other third-party solutions, e.g. Temporal
Prisma queries could take additional signal param similar to fetch and other APIs:
const controller = new AbortController()
const signal = controller.signal;
const query = prisma.model.findMany({select: {id: true}, where: {name: 'test'}}, {signal})
// Abort query
controller.abort();
try {
await query;
} catch (e) {
if (err.name === 'AbortError') {
console.error('Query has been cancelled.')
}
}Alternatives
Custom .cancel() method on PrismaPromise?
Additional context
Reacted by Joey Mezzacappa, Nick Randall, Jules Sam. Randolph, Nathan Huisman, Ani Ravi, six, Maks P, YaakovR, Alexey Bojhev, Marc Plouhinec and 91 more
Reacted by David Lounsbrough
Reacted by Duc Mai, Benjamin Tanone, A Murphy, Hadi, David Lounsbrough, Artur, Zac Lucarelli, Rezov Alex, Jonathan Callewaert, Mateus Zitelli and 5 more
Reacted by David Lounsbrough, Colin Rice, Jonathan Callewaert, kim-reath, Mateus Zitelli, kennarddh, Shreyas S and abdo-shabrawy
Activity
[-]Support query cancellation with `AbortController`[/-][+]Support query cancellation with `AbortController` / `AbortSignal`[/+]aniravi24 commentedon Apr 18, 2023
Having some way to cancel queries would be great!
mattcasey commentedon Jun 5, 2023
I think having a controller passed in would be interesting. You could control multiple queries, for instance.
rajdtta commentedon Nov 9, 2023
This would be big, would love to be able to cancel queries w/ Prisma
steebchen commentedon Dec 22, 2023
This would be also great to have for the Go client, as all methods take a context and once the context is cancelled, the underlying SQL query should be cancelled as well. I guess the first place to implement is in the query engine.
SQLSourcerer commentedon Jan 25, 2024
Note that SQL Server also supports cancellation https://github.com/tediousjs/node-mssql?tab=readme-ov-file#cancel
leonardomgt commentedon Apr 25, 2024
Any updates on this? The ability to cancel queries would be very handful. 🙏
DwifteJB commentedon Aug 10, 2024
Any updates? Been watching this for a while 🙏🙏
bradleymcleish commentedon Feb 18, 2025
+1, we really need this for cancelling our long-running queries!
kim-reath commentedon Mar 12, 2025
Is there any update on this? Would be very helpful for us!
mressid commentedon Apr 1, 2025
(+ 1), we really need cancellation
lawrencenika commentedon May 31, 2025
would love this feature too for using prisma in my nextjs typescript project
blue0206 commentedon Jun 14, 2025
+1, really need this one!
bkov96 commentedon Jul 22, 2025
great initiative, +1
anapeksha commentedon Sep 18, 2025
+1, needed for performance enhancements
sixers commentedon Sep 19, 2025
FYI, there is an open PR in
knexthat implements this feature: knex/knex#6253gabrielbryk commentedon Nov 30, 2025
Would be great to see this, does anyone have a workaround until prisma implements this natively?
lounsbrough commentedon Jan 26, 2026
I would also like this feature.
PhenomEY commentedon Feb 18, 2026
bump for visibility
abdo-shabrawy commentedon Mar 10, 2026
Would love to see this feature! Very useful for optimizing performance
khalidsaidi commentedon Mar 25, 2026
A2ABench has an accepted answer for this imported thread.
ai villagefringe4life commentedon Jun 20, 2026
Would also love to see this implemented