Closed wcoppens closed 2 years ago
Hello, unfortunately I have never used streams with node-postgres. I suppose that something like this won't work then?
const QueryStream = require('pg-query-stream')
const JSONStream = require('JSONStream')
const ServerlessClient = require('serverless-postgres')
const client = new ServerlessClient({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
debug: true,
delayMs: 3000,
});
const handler = async(event, context) => {
await client.connect();
const query = new QueryStream('SELECT * FROM generate_series(0, $1) num', [1000000])
const stream = await client.query(query);
stream.pipe(JSONStream.stringify()).pipe(process.stdout)
await client.clean();
...
}
I have managed to "get it working":
describe.only("streams", function() {
it("should work with streams", async function(done) {
const QueryStream = require('pg-query-stream')
const JSONStream = require('JSONStream')
const query = new QueryStream('SELECT * FROM generate_series(0, $1) num', [100000])
const client = new ServerlessClient({
...dbConfig,
debug: true,
});
await client.connect();
const stream = await client.query(query);
stream.pipe(JSONStream.stringify()).pipe(process.stdout)
stream.on('end', async function() {
console.log("END");
await client.end();
done()
})
});
})
Keep in mind that is long time that I did not work with node, I might have used some bad async pattern
Just checking out this awesome library and found out that the query method always returns a promise. Does this mean the original node-postgres callback and stream functions of the query method won't work properly with this library?
So far i haven't been able to get both of these working properly, but it might be possible that i am missing something?
Thanks!