TypeORM: How to Limit Query Execution Time

Last updated on April 23, 2022 Pennywise Loading... Post a 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
0 Comments
Inline Feedbacks
View all comments

You May Also Like