mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 05:36:00 +00:00
preview in import dialog
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user