SQL file export

This commit is contained in:
Jan Prochazka 2021-08-19 12:41:43 +02:00
parent e3bdad6d77
commit b5c313e517
3 changed files with 70 additions and 2 deletions

View File

@ -8,6 +8,7 @@ const consoleObjectWriter = require('./consoleObjectWriter');
const jsonLinesWriter = require('./jsonLinesWriter'); const jsonLinesWriter = require('./jsonLinesWriter');
const jsonArrayWriter = require('./jsonArrayWriter'); const jsonArrayWriter = require('./jsonArrayWriter');
const jsonLinesReader = require('./jsonLinesReader'); const jsonLinesReader = require('./jsonLinesReader');
const sqlDataWriter = require('./sqlDataWriter');
const jslDataReader = require('./jslDataReader'); const jslDataReader = require('./jslDataReader');
const archiveWriter = require('./archiveWriter'); const archiveWriter = require('./archiveWriter');
const archiveReader = require('./archiveReader'); const archiveReader = require('./archiveReader');
@ -29,6 +30,7 @@ const dbgateApi = {
jsonLinesWriter, jsonLinesWriter,
jsonArrayWriter, jsonArrayWriter,
jsonLinesReader, jsonLinesReader,
sqlDataWriter,
fakeObjectReader, fakeObjectReader,
consoleObjectWriter, consoleObjectWriter,
jslDataReader, jslDataReader,

View File

@ -0,0 +1,48 @@
const fs = require('fs');
const stream = require('stream');
const path = require('path');
const { driverBase } = require('dbgate-tools');
class SqlizeStream extends stream.Transform {
constructor({ fileName }) {
super({ objectMode: true });
this.wasHeader = false;
this.tableName = path.parse(fileName).name;
}
_transform(chunk, encoding, done) {
let skip = false;
if (!this.wasHeader) {
if (
chunk.__isStreamHeader ||
// TODO remove isArray test
Array.isArray(chunk.columns)
) {
skip = true;
this.tableName = chunk.pureName;
}
this.wasHeader = true;
}
if (!skip) {
const dmp = driverBase.createDumper();
dmp.put(
'^insert ^into %f (%,i) ^values (%,v);\n',
{ pureName: this.tableName },
Object.keys(chunk),
Object.values(chunk)
);
this.push(dmp.s);
}
done();
}
}
async function sqlDataWriter({ fileName, driver, encoding = 'utf-8' }) {
console.log(`Writing file ${fileName}`);
const stringify = new SqlizeStream({ fileName });
const fileStream = fs.createWriteStream(fileName, encoding);
stringify.pipe(fileStream);
stringify['finisher'] = fileStream;
return stringify;
}
module.exports = sqlDataWriter;

View File

@ -15,6 +15,13 @@ const jsonFormat = {
writerFunc: 'jsonArrayWriter', writerFunc: 'jsonArrayWriter',
}; };
const sqlFormat = {
storageType: 'sql',
extension: 'sql',
name: 'SQL',
writerFunc: 'sqlDataWriter',
};
const jsonlQuickExport = { const jsonlQuickExport = {
label: 'JSON lines', label: 'JSON lines',
extension: 'jsonl', extension: 'jsonl',
@ -37,8 +44,19 @@ const jsonQuickExport = {
}), }),
}; };
const sqlQuickExport = {
label: 'SQL',
extension: 'sql',
createWriter: fileName => ({
functionName: 'sqlDataWriter',
props: {
fileName,
},
}),
};
export function buildFileFormats(plugins): FileFormatDefinition[] { export function buildFileFormats(plugins): FileFormatDefinition[] {
const res = [jsonlFormat, jsonFormat]; const res = [jsonlFormat, jsonFormat, sqlFormat];
for (const { content } of plugins) { for (const { content } of plugins) {
const { fileFormats } = content; const { fileFormats } = content;
if (fileFormats) res.push(...fileFormats); if (fileFormats) res.push(...fileFormats);
@ -47,7 +65,7 @@ export function buildFileFormats(plugins): FileFormatDefinition[] {
} }
export function buildQuickExports(plugins): QuickExportDefinition[] { export function buildQuickExports(plugins): QuickExportDefinition[] {
const res = [jsonQuickExport, jsonlQuickExport]; const res = [jsonQuickExport, jsonlQuickExport, sqlQuickExport];
for (const { content } of plugins) { for (const { content } of plugins) {
if (content.quickExports) res.push(...content.quickExports); if (content.quickExports) res.push(...content.quickExports);
} }