取消knex.js中的流式Postgres查询

时间:2019-06-04 13:14:16

标签: postgresql stream knex.js

是否可以在knex.js中取消流式,长时间运行的查询? 尝试过stream.emit('close'),但似乎没有关闭查询,因为knex.destroy()调用从未解决。

const query = knex('tablename')
const stream = query.stream({ batchSize: 2000 })

process.on('SIGINT', () => stream.emit('close', 128 + 2))
process.on('SIGTERM', () => stream.emit('close', 128 + 15))

stream
  .on('data', onData)
  .on('close', (exitCode = 0) => {
    knex.destroy()
      .then(() => console.log('This is never resolved if the query has finished'))
      .catch(err => console.error('could not close connection gracefully', err))

    console.log('Finished')
    process.exitCode = exitCode
  })
  .on('end', () => {
    console.log('End')
    stream.emit('close')
  })

0 个答案:

没有答案