Currently, I have some code that query from Neo4j
database then write to a CSV file on server:
const session = driver.session(); const query = '<my query>'; var file = fs.createWriteStream('export.csv'); file.write('<some CSV header here'); await new Promise((resolve, reject) => { session.run(query).subscribe({ onNext: i => { file.write('<data of record i>'); }, onCompleted: () => { session.close() file.end() resolve() }, onError: error => { console.log(error) reject() } }) }) return response.download('export.csv');
Everything is fine until now I have more data in the database so the process to write a CSV file takes a long time, then the client receives a timeout error.
I have found a couple of solutions that use pipe()
to send stream directly to the client but I tried it and failed because the result of session.run()
is an observable not stream. Is there any other way?
Note: I’m using AdonisJs for backend
Advertisement
Answer
You can write data directly into the response like this:
const session = driver.session(); const query = '<my query>'; response.response.writeHead(200, { 'Content-Type': 'application/csv', 'Content-Disposition': 'attachment; filename="export.csv"' }); response.response.write('<some CSV header here'); await new Promise((resolve, reject) => { const results = session.run(query).subscribe({ onNext: i => { response.response.write('<data of record i>'); }, onCompleted: () => { session.close() response.response.end() resolve() }, onError: error => { console.log(error) reject() } }) })