Created
November 10, 2021 09:25
-
-
Save alextes/04e6f8dbeb2e27fe6d417d909850767c to your computer and use it in GitHub Desktop.
stream postgres rows to javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "stream-rows", | |
"version": "1.0.0", | |
"main": "index.js", | |
"dependencies": { | |
"postgres": "^2.0.0-beta.10" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const postgres = require("postgres"); | |
const sql = postgres({ | |
host: process.env.PGHOST, | |
database: process.env.PGDATABASE, | |
password: process.env.PGPASSWORD, | |
username: process.env.PGUSER, | |
port: process.env.PGPORT, | |
ssl: "prefer", | |
max: 2, | |
connection: { | |
application_name: "script", | |
}, | |
idle_timeout: 30, | |
}); | |
const delay = (ms) => | |
new Promise((resolve) => { | |
setTimeout(resolve, ms); | |
}); | |
console.log("streaming rows from twitter table"); | |
const main = async () => { | |
await sql` | |
SELECT * FROM twitter_accounts | |
`.cursor(1, async (row) => { | |
console.log(row); | |
await delay(2000); | |
}); | |
console.log("done streaming rows"); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment