mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 07:56:01 +00:00
38 lines
1.1 KiB
JavaScript
38 lines
1.1 KiB
JavaScript
const csv = require('csv');
|
|
const fs = require('fs');
|
|
const stream = require('stream');
|
|
|
|
class CsvPrepareStream extends stream.Transform {
|
|
constructor({ header }) {
|
|
super({ objectMode: true });
|
|
this.structure = null;
|
|
this.header = header;
|
|
}
|
|
_transform(chunk, encoding, done) {
|
|
if (this.structure) {
|
|
this.push(this.structure.columns.map((col) => chunk[col.columnName]));
|
|
done();
|
|
} else {
|
|
this.structure = chunk;
|
|
if (this.header) {
|
|
this.push(chunk.columns.map((x) => x.columnName));
|
|
}
|
|
done();
|
|
}
|
|
}
|
|
}
|
|
|
|
async function writer({ fileName, encoding = 'utf-8', header = true, delimiter, quoted }) {
|
|
console.log(`Writing file ${fileName}`);
|
|
const csvPrepare = new CsvPrepareStream({ header });
|
|
const csvStream = csv.stringify({ delimiter, quoted });
|
|
const fileStream = fs.createWriteStream(fileName, encoding);
|
|
csvPrepare.pipe(csvStream);
|
|
csvStream.pipe(fileStream);
|
|
csvPrepare['finisher'] = fileStream;
|
|
csvPrepare.requireFixedStructure = true;
|
|
return csvPrepare;
|
|
}
|
|
|
|
module.exports = writer;
|