mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
csvReader follows dbgate stream api
This commit is contained in:
parent
f68bdafd9f
commit
dc7c44b797
@ -1,15 +1,54 @@
|
|||||||
|
const _ = require('lodash');
|
||||||
const csv = require('csv');
|
const csv = require('csv');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const stream = require('stream');
|
||||||
|
|
||||||
async function csvReader({ fileName, encoding = 'utf-8', ...options }) {
|
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(
|
||||||
|
_.zipObject(
|
||||||
|
this.structure.columns.map((x) => x.columnName),
|
||||||
|
chunk
|
||||||
|
)
|
||||||
|
);
|
||||||
|
done();
|
||||||
|
} else {
|
||||||
|
if (this.header) {
|
||||||
|
this.structure = { columns: chunk.map((columnName) => ({ columnName })) };
|
||||||
|
this.push(this.structure);
|
||||||
|
} else {
|
||||||
|
this.structure = { columns: chunk.map((value, index) => ({ columnName: `col${index + 1}` })) };
|
||||||
|
this.push(this.structure);
|
||||||
|
this.push(
|
||||||
|
_.zipObject(
|
||||||
|
this.structure.columns.map((x) => x.columnName),
|
||||||
|
chunk
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function csvReader({ fileName, encoding = 'utf-8', header = true, delimiter, quoted }) {
|
||||||
console.log(`Reading file ${fileName}`);
|
console.log(`Reading file ${fileName}`);
|
||||||
const csvStream = csv.parse({
|
const csvStream = csv.parse({
|
||||||
columns: true,
|
// @ts-ignore
|
||||||
...options,
|
delimiter,
|
||||||
|
quoted,
|
||||||
});
|
});
|
||||||
const fileStream = fs.createReadStream(fileName, encoding);
|
const fileStream = fs.createReadStream(fileName, encoding);
|
||||||
|
const csvPrepare = new CsvPrepareStream({ header });
|
||||||
fileStream.pipe(csvStream);
|
fileStream.pipe(csvStream);
|
||||||
return csvStream;
|
csvStream.pipe(csvPrepare);
|
||||||
|
return csvPrepare;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = csvReader;
|
module.exports = csvReader;
|
||||||
|
@ -3,21 +3,21 @@ const dbgateApi = require('@dbgate/api');
|
|||||||
async function run() {
|
async function run() {
|
||||||
const csvReader = await dbgateApi.csvReader({
|
const csvReader = await dbgateApi.csvReader({
|
||||||
fileName: 'test.csv',
|
fileName: 'test.csv',
|
||||||
header: true,
|
// header: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
const tableWriter = await dbgateApi.tableWriter({
|
// const tableWriter = await dbgateApi.tableWriter({
|
||||||
connection: {
|
// connection: {
|
||||||
server: 'localhost',
|
// server: 'localhost',
|
||||||
engine: 'mysql',
|
// engine: 'mysql',
|
||||||
user: 'root',
|
// user: 'root',
|
||||||
password: 'test',
|
// password: 'test',
|
||||||
port: '3307',
|
// port: '3307',
|
||||||
database: 'Chinook',
|
// database: 'Chinook',
|
||||||
},
|
// },
|
||||||
pureName: 'importedTable'
|
// pureName: 'importedTable'
|
||||||
});
|
// });
|
||||||
|
|
||||||
const consoleWriter = await dbgateApi.consoleObjectWriter();
|
const consoleWriter = await dbgateApi.consoleObjectWriter();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user