added plugins

This commit is contained in:
Jan Prochazka
2021-04-13 16:17:53 +02:00
parent 446e7c139f
commit 4d5cc119f2
115 changed files with 5519 additions and 24 deletions

View 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);
},
};

View 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;

View 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;

View File

@@ -0,0 +1,46 @@
const fileFormat = {
packageName: 'dbgate-plugin-csv',
// file format identifier
storageType: 'csv',
// file extension without leading dot
extension: 'csv',
// human readable file format name
name: 'CSV',
// function name from backend, which contains reader factory, postfixed by package name
readerFunc: 'reader@dbgate-plugin-csv',
// function name from backend, which contains writer factory, postfixed by package name
writerFunc: 'writer@dbgate-plugin-csv',
// optional list of format arguments, which can be edited from UI
args: [
{
type: 'select',
name: 'delimiter',
label: 'Delimiter',
options: [
{ name: 'Comma (,)', value: ',' },
{ name: 'Semicolon (;)', value: ';' },
{ name: 'Tab', value: '\t' },
{ name: 'Pipe (|)', value: '|' },
],
apiName: 'delimiter',
},
{
type: 'checkbox',
name: 'quoted',
label: 'Quoted',
apiName: 'quoted',
direction: 'target',
},
{
type: 'checkbox',
name: 'header',
label: 'Has header row',
apiName: 'header',
default: true,
},
],
};
export default {
fileFormats: [fileFormat],
};