I want to add timeout to pg-promise queries so they will fail after some amount of time if database have not yet responded. Is there any recommended way to do that or should I make custom wrapper that will handle timer and reject promise if it's too late?
相关问题
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- google-drive can't get push notifications
- How to reimport module with ES6 import
- Django distinct is not working
- PostgreSQL: left outer join syntax
相关文章
- postgresql 关于使用between and 中是字符串的问题
- node连接远程oracle报错
- postgresql 月份差计算问题
- Using boolean expression in order by clause
- How can make folder with Firebase Cloud Functions
- Table valued Parameter Equivalent in Postgresql
- in redshift postgresql can I skip columns with the
- Oracle equivalent of PostgreSQL INSERT…RETURNING *
From the author of pg-promise...
pg-promise doesn't support query cancellation, because it is a hack to work-around incorrect database design or bad query execution.
PostgreSQL supports events that should be used when executing time-consuming queries, so instead of waiting, one can set an event listener to be triggered when specific data/view becomes available. See LISTEN/NOTIFY example.
You can extend pg-promise with your own custom query method that will time out with a reject (see example below), but that's again another work-around on top of a design problem.
Example using Bluebird:
Then you can use
db.queryTimeout(query, values, delay)
on every level.Alternatively, if you are using Bluebird, you can chain
.timeout(delay)
to any of the existing methods:See also:
UPDATE
From version 8.5.3, pg-promise started supporting query timeouts, via property
query_timeout
within the connection object.You can either override the defaults:
Or specify it within the connection object: