Skip to content

Commit

Permalink
Merge pull request #285 from dhanush-2397/dev
Browse files Browse the repository at this point in the history
Write to CSV function changes
  • Loading branch information
pandutibil authored Oct 12, 2023
2 parents 6fd85bb + b5ee4d5 commit a95f221
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/ingestion/services/generic-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,22 @@ export class GenericFunction {
} while (true);
// get the lock on the file
this.currentlyLockedFiles[fileName] = true;

const fileExists = fs.existsSync(fileName);
const stream = fs.createWriteStream(fileName,{flags:'a'});
await csv.write(inputArray, { headers: true})
.pipe(stream)
.on('finish', () => {
if(!fileExists){
await csv.write(inputArray, { headers: true }).pipe(stream);
}else{
await csv.write(inputArray).pipe(stream);
}
// await csv.write(inputArray, { headers: true})
// .pipe(stream)
stream.on('finish', () => {
console.log('CSV file has been written successfully');
// delete the lock after writing
delete this.currentlyLockedFiles[fileName];
resolve('done')
})
.on('error', (err) => {
stream.on('error', (err) => {
console.error('Error writing CSV file:', err);
reject(err)
});
Expand Down

0 comments on commit a95f221

Please sign in to comment.