Kinda Code
Home/Node/TypeORM: How to Limit Query Execution Time

TypeORM: How to Limit Query Execution Time

Last updated: April 23, 2022

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.