javascriptnode-postgres

Querying data with pg-cursor


I have the following code to read the first 10 rows from a query with a PG Cursor in a CloudFlare worker:

const client = new Client(`<DATABASE URL>`)
await client.connect()
const cursor = client.query(new Cursor(queryString))
let rows = await cursor.read(10)
cursor.close(() => client.release())

However, this is returning the following:

ReferenceError: setImmediate is not defined
    at Cursor2._sendRows (file:///Users/neil/code/console-query-runner/node_modules/pg-cursor/index.js:120:5)
    at Cursor2.handlePortalSuspended (file:///Users/neil/code/console-query-runner/node_modules/pg-cursor/index.js:140:10)
    at Client2._handlePortalSuspended (file:///Users/neil/code/console-query-runner/node_modules/pg/lib/client.js:372:22)
    at emitOne (node-modules-polyfills:events:84:13)
    at Connection.emit2 [as emit] (node-modules-polyfills:events:171:7)
...

From doing some digging it seems setImmediate is some built in Node-ism which isn't available in CF Workers by default somehow. How can I resolve this?


Solution

  • It seems you need to shim setImmediate in this instances:

    $ npm i setimmediate
    

    then require('setimmediate') as early as possible in your app.