mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-03 07:03:59 +00:00
JSON export - support for object style, key field, root field
This commit is contained in:
@@ -1,17 +1,22 @@
|
|||||||
const { getLogger } = require('dbgate-tools');
|
const { getLogger } = require('dbgate-tools');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const stream = require('stream');
|
const stream = require('stream');
|
||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
const logger = getLogger('jsonArrayWriter');
|
const logger = getLogger('jsonArrayWriter');
|
||||||
|
|
||||||
class StringifyStream extends stream.Transform {
|
class StringifyStream extends stream.Transform {
|
||||||
constructor() {
|
constructor({ jsonStyle, keyField, rootField }) {
|
||||||
super({ objectMode: true });
|
super({ objectMode: true });
|
||||||
this.wasHeader = false;
|
this.wasHeader = false;
|
||||||
this.wasRecord = false;
|
this.wasRecord = false;
|
||||||
|
this.jsonStyle = jsonStyle;
|
||||||
|
this.keyField = keyField;
|
||||||
|
this.rootField = rootField;
|
||||||
}
|
}
|
||||||
_transform(chunk, encoding, done) {
|
_transform(chunk, encoding, done) {
|
||||||
let skip = false;
|
let skip = false;
|
||||||
|
const keyField = this.keyField || '_key';
|
||||||
|
|
||||||
if (!this.wasHeader) {
|
if (!this.wasHeader) {
|
||||||
skip = chunk.__isStreamHeader;
|
skip = chunk.__isStreamHeader;
|
||||||
@@ -19,30 +24,71 @@ class StringifyStream extends stream.Transform {
|
|||||||
}
|
}
|
||||||
if (!skip) {
|
if (!skip) {
|
||||||
if (!this.wasRecord) {
|
if (!this.wasRecord) {
|
||||||
|
if (this.rootField) {
|
||||||
|
if (this.jsonStyle === 'object') {
|
||||||
|
this.push(`{"${this.rootField}": {\n`);
|
||||||
|
} else {
|
||||||
|
this.push(`{"${this.rootField}": [\n`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (this.jsonStyle === 'object') {
|
||||||
|
this.push('{\n');
|
||||||
|
} else {
|
||||||
this.push('[\n');
|
this.push('[\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.push(',\n');
|
this.push(',\n');
|
||||||
}
|
}
|
||||||
this.wasRecord = true;
|
this.wasRecord = true;
|
||||||
|
|
||||||
|
if (this.jsonStyle === 'object') {
|
||||||
|
const key = chunk[keyField] ?? chunk[Object.keys(chunk)[0]];
|
||||||
|
this.push(`"${key}": ${JSON.stringify(_.omit(chunk, [keyField]))}`);
|
||||||
|
} else {
|
||||||
this.push(JSON.stringify(chunk));
|
this.push(JSON.stringify(chunk));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
|
|
||||||
_flush(done) {
|
_flush(done) {
|
||||||
if (!this.wasRecord) {
|
if (!this.wasRecord) {
|
||||||
|
if (this.rootField) {
|
||||||
|
if (this.jsonStyle === 'object') {
|
||||||
|
this.push(`{"${this.rootField}": {}}\n`);
|
||||||
|
} else {
|
||||||
|
this.push(`{"${this.rootField}": []}\n`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (this.jsonStyle === 'object') {
|
||||||
|
this.push('{}\n');
|
||||||
|
} else {
|
||||||
this.push('[]\n');
|
this.push('[]\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (this.rootField) {
|
||||||
|
if (this.jsonStyle === 'object') {
|
||||||
|
this.push('\n}}\n');
|
||||||
|
} else {
|
||||||
|
this.push('\n]}\n');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (this.jsonStyle === 'object') {
|
||||||
|
this.push('\n}\n');
|
||||||
} else {
|
} else {
|
||||||
this.push('\n]\n');
|
this.push('\n]\n');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function jsonWriter({ fileName, encoding = 'utf-8' }) {
|
async function jsonWriter({ fileName, jsonStyle, keyField, rootField, encoding = 'utf-8' }) {
|
||||||
logger.info(`Writing file ${fileName}`);
|
logger.info(`Writing file ${fileName}`);
|
||||||
const stringify = new StringifyStream();
|
const stringify = new StringifyStream({ jsonStyle, keyField, rootField });
|
||||||
const fileStream = fs.createWriteStream(fileName, encoding);
|
const fileStream = fs.createWriteStream(fileName, encoding);
|
||||||
stringify.pipe(fileStream);
|
stringify.pipe(fileStream);
|
||||||
stringify['finisher'] = fileStream;
|
stringify['finisher'] = fileStream;
|
||||||
|
|||||||
@@ -22,19 +22,19 @@ const jsonFormat = {
|
|||||||
name: 'jsonStyle',
|
name: 'jsonStyle',
|
||||||
label: 'JSON style',
|
label: 'JSON style',
|
||||||
options: [
|
options: [
|
||||||
{ name: 'Array', value: 'array' },
|
{ name: 'Array', value: '' },
|
||||||
{ name: 'Object', value: 'object' },
|
{ name: 'Object', value: 'object' },
|
||||||
],
|
],
|
||||||
apiName: 'jsonStyle',
|
apiName: 'jsonStyle',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'textbox',
|
type: 'text',
|
||||||
name: 'keyField',
|
name: 'keyField',
|
||||||
label: 'Key field',
|
label: 'Key field (only for "Object" style)',
|
||||||
apiName: 'keyField',
|
apiName: 'keyField',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'textbox',
|
type: 'text',
|
||||||
name: 'rootField',
|
name: 'rootField',
|
||||||
label: 'Root field',
|
label: 'Root field',
|
||||||
apiName: 'rootField',
|
apiName: 'rootField',
|
||||||
|
|||||||
Reference in New Issue
Block a user