Skip to content

Commit

Permalink
improve csv output stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Etienne Laurent committed Apr 3, 2024
1 parent 79d29a2 commit 69c786d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
45 changes: 16 additions & 29 deletions lib/formats/csv.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('node:fs');
const { stringify } = require('csv-stringify');
const parse = require('csv-parse');
// const parse = require('csv-parse');

module.exports = {
label: 'CSV',
Expand All @@ -26,38 +26,25 @@ async function input(filepath) {
};

async function output(apos, filepath, data) {
console.log('data');
console.dir(data, { depth: 9 });

return new Promise((resolve, reject) => {
const result = [];
const stringifier = stringify({
header: true,
columns: getColumnsNames(data)
});

stringifier.on('readable', function() {
let row;
while ((row = stringifier.read()) !== null) {
result.push(row);
}
});

stringifier.on('error', reject);
const stream = fs.createWriteStream(filepath);
const stringifier = stringify({
header: true,
columns: getColumnsNames(data)
});

stringifier.on('finish', function() {
console.log('result');
console.dir(result);
resolve(result);
});
stringifier.pipe(stream);

stringifier.pipe(fs.createWriteStream(filepath));
// plunge each doc into the stream
data.forEach(record => {
stringifier.write(record);
});

data.forEach(record => {
stringifier.write(record);
});
stringifier.end();

stringifier.end();
return new Promise((resolve, reject) => {
stringifier.on('error', reject);
stream.on('error', reject);
stream.on('finish', resolve);
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/methods/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ module.exports = self => {
const filepath = path.join(self.apos.attachment.uploadfs.getTempPath(), filename);

try {
const { attachmentError } = await format.output(self.apos, filepath, data);
if (attachmentError) {
const result = await format.output(self.apos, filepath, data);
if (result && result.attachmentError) {
await self.apos.notify(req, 'aposImportExport:exportAttachmentError', {
interpolate: { format: format.label },
icon: 'alert-circle-icon',
Expand Down

0 comments on commit 69c786d

Please sign in to comment.