Skip to content

Instantly share code, notes, and snippets.

@alextes
Created November 10, 2021 09:25
Show Gist options
  • Save alextes/04e6f8dbeb2e27fe6d417d909850767c to your computer and use it in GitHub Desktop.
Save alextes/04e6f8dbeb2e27fe6d417d909850767c to your computer and use it in GitHub Desktop.
stream postgres rows to javascript
{
"name": "stream-rows",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"postgres": "^2.0.0-beta.10"
}
}
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