mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 12:35:59 +00:00
xml plugin initial import
This commit is contained in:
10
plugins/dbgate-plugin-xml/src/backend/index.js
Normal file
10
plugins/dbgate-plugin-xml/src/backend/index.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const reader = require('./reader');
|
||||
const writer = require('./writer');
|
||||
|
||||
module.exports = {
|
||||
packageName: 'dbgate-plugin-xml',
|
||||
shellApi: {
|
||||
reader,
|
||||
writer,
|
||||
},
|
||||
};
|
||||
42
plugins/dbgate-plugin-xml/src/backend/reader.js
Normal file
42
plugins/dbgate-plugin-xml/src/backend/reader.js
Normal file
@@ -0,0 +1,42 @@
|
||||
const fs = require('fs');
|
||||
const stream = require('stream');
|
||||
const NodeXmlStream = require('node-xml-stream');
|
||||
|
||||
class ParseStream extends stream.Transform {
|
||||
constructor({ elementName }) {
|
||||
super({ objectMode: true });
|
||||
this.rowsWritten = 0;
|
||||
this.parser = new NodeXmlStream();
|
||||
this.stack = [];
|
||||
this.parser.on('opentag', (name, attrs) => {
|
||||
this.stack.push({ name, attrs, nodes: {} });
|
||||
});
|
||||
this.parser.on('text', (text) => {
|
||||
if (this.stack.length >= 2) {
|
||||
this.stack[this.stack.length - 2].nodes[this.stack[this.stack.length - 1].name] = text;
|
||||
}
|
||||
});
|
||||
this.parser.on('closetag', (name, attrs) => {
|
||||
if (name == elementName) {
|
||||
this.rowsWritten += 1;
|
||||
this.push({ ...this.stack[this.stack.length - 1].attrs, ...this.stack[this.stack.length - 1].nodes });
|
||||
}
|
||||
this.stack.splice(-1);
|
||||
});
|
||||
}
|
||||
_transform(chunk, encoding, done) {
|
||||
this.parser.write(chunk);
|
||||
done();
|
||||
}
|
||||
}
|
||||
|
||||
async function reader({ fileName, encoding = 'utf-8', elementName }) {
|
||||
console.log(`Reading file ${fileName}`);
|
||||
|
||||
const fileStream = fs.createReadStream(fileName, encoding);
|
||||
const parser = new ParseStream({ elementName });
|
||||
fileStream.pipe(parser);
|
||||
return parser;
|
||||
}
|
||||
|
||||
module.exports = reader;
|
||||
23
plugins/dbgate-plugin-xml/src/backend/writer.js
Normal file
23
plugins/dbgate-plugin-xml/src/backend/writer.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const fs = require('fs');
|
||||
const stream = require('stream');
|
||||
|
||||
class StringifyStream extends stream.Transform {
|
||||
constructor() {
|
||||
super({ objectMode: true });
|
||||
}
|
||||
_transform(chunk, encoding, done) {
|
||||
this.push(JSON.stringify(chunk) + '\n');
|
||||
done();
|
||||
}
|
||||
}
|
||||
|
||||
async function writer({ fileName, encoding = 'utf-8' }) {
|
||||
console.log(`Writing file ${fileName}`);
|
||||
const stringify = new StringifyStream();
|
||||
const fileStream = fs.createWriteStream(fileName, encoding);
|
||||
stringify.pipe(fileStream);
|
||||
stringify['finisher'] = fileStream;
|
||||
return stringify;
|
||||
}
|
||||
|
||||
module.exports = writer;
|
||||
27
plugins/dbgate-plugin-xml/src/frontend/index.js
Normal file
27
plugins/dbgate-plugin-xml/src/frontend/index.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const fileFormat = {
|
||||
packageName: 'dbgate-plugin-xml',
|
||||
// file format identifier
|
||||
storageType: 'xml@dbgate-plugin-xml',
|
||||
// file extension without leading dot
|
||||
extension: 'xml',
|
||||
// human readable file format name
|
||||
name: 'XML file',
|
||||
// function name from backend, which contains reader factory, postfixed by package name
|
||||
readerFunc: 'reader@dbgate-plugin-xml',
|
||||
// function name from backend, which contains writer factory, postfixed by package name
|
||||
writerFunc: 'writer@dbgate-plugin-xml',
|
||||
// optional list of format arguments, which can be edited from UI
|
||||
args: [
|
||||
{
|
||||
type: 'text',
|
||||
name: 'elementName',
|
||||
label: 'Element name',
|
||||
apiName: 'elementName',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
export default {
|
||||
fileFormats: [fileFormat],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user