preview in import dialog

This commit is contained in:
Jan Prochazka
2020-11-15 09:43:44 +01:00
parent 844ebf129a
commit 075146403a
10 changed files with 142 additions and 22 deletions

View File

@@ -3,10 +3,12 @@ const stream = require('stream');
const byline = require('byline');
class ParseStream extends stream.Transform {
constructor({ header }) {
constructor({ header, limitRows }) {
super({ objectMode: true });
this.header = header;
this.wasHeader = false;
this.limitRows = limitRows;
this.rowsWritten = 0;
}
_transform(chunk, encoding, done) {
const obj = JSON.parse(chunk);
@@ -14,17 +16,19 @@ class ParseStream extends stream.Transform {
if (!this.header) this.push({ columns: Object.keys(obj).map((columnName) => ({ columnName })) });
this.wasHeader = true;
}
this.push(obj);
if (!this.limitRows || this.rowsWritten < this.limitRows) {
this.push(obj);
}
done();
}
}
async function jsonLinesReader({ fileName, encoding = 'utf-8', header = true }) {
async function jsonLinesReader({ fileName, encoding = 'utf-8', header = true, limitRows = undefined }) {
console.log(`Reading file ${fileName}`);
const fileStream = fs.createReadStream(fileName, encoding);
const liner = byline(fileStream);
const parser = new ParseStream({ header });
const parser = new ParseStream({ header, limitRows });
liner.pipe(parser);
return parser;
}