TypeORM: How to Limit Query Execution Time

Updated: April 23, 2022 By: Pennywise One comment

When performing CRUD operations with TypeORM, you can set timeout (maximum execution time) for a certain query like so:

const productRepository = dataSource.getRepository(Product);

const products = await productRepository
    .createQueryBuilder('product')
    .orderBy('product.id', 'DESC')
    .maxExecutionTime(3000) // 3000 milliseconds
    .getMany()

If the query above takes more than 3000 milliseconds, it will be dropped to avoid crashing the server. You can set another amount of time that makes sense to you.

Further reading:

You can also check out our Node.js category page for the latest tutorials and examples.

Subscribe
Notify of
guest
1 Comment
Inline Feedbacks
View all comments
Antonis
Antonis
5 months ago

That works only with MySQL/MARIADB. With Postgresql does not work

Related Articles