mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 12:35:59 +00:00
added plugins
This commit is contained in:
13
plugins/dbgate-plugin-csv/src/backend/index.js
Normal file
13
plugins/dbgate-plugin-csv/src/backend/index.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const reader = require('./reader');
|
||||
const writer = require('./writer');
|
||||
|
||||
module.exports = {
|
||||
packageName: 'dbgate-plugin-csv',
|
||||
shellApi: {
|
||||
reader,
|
||||
writer,
|
||||
},
|
||||
initialize(dbgateEnv) {
|
||||
reader.initialize(dbgateEnv);
|
||||
},
|
||||
};
|
||||
67
plugins/dbgate-plugin-csv/src/backend/reader.js
Normal file
67
plugins/dbgate-plugin-csv/src/backend/reader.js
Normal file
@@ -0,0 +1,67 @@
|
||||
const zipObject = require('lodash/zipObject');
|
||||
const csv = require('csv');
|
||||
const fs = require('fs');
|
||||
const stream = require('stream');
|
||||
|
||||
let dbgateApi;
|
||||
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 = {
|
||||
__isStreamHeader: true,
|
||||
columns: chunk.map((columnName) => ({ columnName })),
|
||||
};
|
||||
this.push(this.structure);
|
||||
} else {
|
||||
this.structure = {
|
||||
__isStreamHeader: true,
|
||||
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 reader({ fileName, encoding = 'utf-8', header = true, delimiter, limitRows = undefined }) {
|
||||
console.log(`Reading file ${fileName}`);
|
||||
const csvStream = csv.parse({
|
||||
// @ts-ignore
|
||||
delimiter,
|
||||
skip_lines_with_error: true,
|
||||
to_line: limitRows ? limitRows + 1 : undefined,
|
||||
});
|
||||
const downloadedFile = await dbgateApi.download(fileName);
|
||||
const fileStream = fs.createReadStream(downloadedFile, encoding);
|
||||
const csvPrepare = new CsvPrepareStream({ header });
|
||||
fileStream.pipe(csvStream);
|
||||
csvStream.pipe(csvPrepare);
|
||||
return csvPrepare;
|
||||
}
|
||||
|
||||
reader.initialize = (dbgateEnv) => {
|
||||
dbgateApi = dbgateEnv.dbgateApi;
|
||||
};
|
||||
|
||||
module.exports = reader;
|
||||
36
plugins/dbgate-plugin-csv/src/backend/writer.js
Normal file
36
plugins/dbgate-plugin-csv/src/backend/writer.js
Normal file
@@ -0,0 +1,36 @@
|
||||
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;
|
||||
return csvPrepare;
|
||||
}
|
||||
|
||||
module.exports = writer;
|
||||
Reference in New Issue
Block a user