mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 16:36:00 +00:00
using stream.pipeline for better handling errors
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
const EnsureStreamHeaderStream = require('../utility/EnsureStreamHeaderStream');
|
||||
const Stream = require('stream');
|
||||
const ColumnMapTransformStream = require('../utility/ColumnMapTransformStream');
|
||||
const streamPipeline = require('../utility/streamPipeline');
|
||||
|
||||
/**
|
||||
* Copies reader to writer. Used for import, export tables and transfer data between tables
|
||||
@@ -9,7 +9,7 @@ const ColumnMapTransformStream = require('../utility/ColumnMapTransformStream');
|
||||
* @param {object} options - options
|
||||
* @returns {Promise}
|
||||
*/
|
||||
function copyStream(input, output, options) {
|
||||
async function copyStream(input, output, options) {
|
||||
const { columns } = options || {};
|
||||
|
||||
const transforms = [];
|
||||
@@ -20,49 +20,19 @@ function copyStream(input, output, options) {
|
||||
transforms.push(new EnsureStreamHeaderStream());
|
||||
}
|
||||
|
||||
// return new Promise((resolve, reject) => {
|
||||
// Stream.pipeline(input, ...transforms, output, err => {
|
||||
// if (err) {
|
||||
// reject(err);
|
||||
// } else {
|
||||
// resolve();
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const finisher = output['finisher'] || output;
|
||||
finisher.on('finish', resolve);
|
||||
|
||||
input.on('error', err => {
|
||||
// console.log('&&&&&&&&&&&&&&&&&&&&&&& CATCH ERROR IN COPY STREAM &&&&&&&&&&&&&&&&&&&&&&');
|
||||
// console.log(err);
|
||||
process.send({
|
||||
msgtype: 'copyStreamError',
|
||||
runid: this.runid,
|
||||
copyStreamError: err,
|
||||
});
|
||||
try {
|
||||
await streamPipeline(input, transforms, output);
|
||||
} catch (err) {
|
||||
process.send({
|
||||
msgtype: 'copyStreamError',
|
||||
runid: this.runid,
|
||||
copyStreamError: {
|
||||
message: err.message,
|
||||
...err,
|
||||
},
|
||||
});
|
||||
|
||||
input.on('error', reject);
|
||||
|
||||
finisher.on('error', reject);
|
||||
|
||||
let lastStream = input;
|
||||
for (const tran of transforms) {
|
||||
lastStream.pipe(tran);
|
||||
lastStream = tran;
|
||||
}
|
||||
lastStream.pipe(output);
|
||||
|
||||
// if (output.requireFixedStructure) {
|
||||
// const ensureHeader = new EnsureStreamHeaderStream();
|
||||
// input.pipe(ensureHeader);
|
||||
// ensureHeader.pipe(output);
|
||||
// } else {
|
||||
// input.pipe(output);
|
||||
// }
|
||||
});
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = copyStream;
|
||||
|
||||
@@ -5,6 +5,7 @@ const { splitQueryStream } = require('dbgate-query-splitter/lib/splitQueryStream
|
||||
const download = require('./download');
|
||||
const stream = require('stream');
|
||||
const { getLogger } = require('dbgate-tools');
|
||||
const streamPipeline = require('../utility/streamPipeline');
|
||||
|
||||
const logger = getLogger('importDb');
|
||||
|
||||
@@ -43,17 +44,6 @@ class ImportStream extends stream.Transform {
|
||||
}
|
||||
}
|
||||
|
||||
function awaitStreamEnd(stream) {
|
||||
return new Promise((resolve, reject) => {
|
||||
stream.once('end', () => {
|
||||
resolve(true);
|
||||
});
|
||||
stream.once('error', err => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function importDatabase({ connection = undefined, systemConnection = undefined, driver = undefined, inputFile }) {
|
||||
logger.info(`Importing database`);
|
||||
|
||||
@@ -72,9 +62,8 @@ async function importDatabase({ connection = undefined, systemConnection = undef
|
||||
returnRichInfo: true,
|
||||
});
|
||||
const importStream = new ImportStream(dbhan, driver);
|
||||
// @ts-ignore
|
||||
splittedStream.pipe(importStream);
|
||||
await awaitStreamEnd(importStream);
|
||||
|
||||
await streamPipeline(splittedStream, importStream);
|
||||
} finally {
|
||||
if (!systemConnection) {
|
||||
await driver.close(dbhan);
|
||||
|
||||
@@ -53,8 +53,7 @@ async function jsonLinesReader({ fileName, encoding = 'utf-8', limitRows = undef
|
||||
);
|
||||
const liner = byline(fileStream);
|
||||
const parser = new ParseStream({ limitRows });
|
||||
liner.pipe(parser);
|
||||
return parser;
|
||||
return [liner, parser];
|
||||
}
|
||||
|
||||
module.exports = jsonLinesReader;
|
||||
|
||||
@@ -10,7 +10,6 @@ const download = require('./download');
|
||||
|
||||
const logger = getLogger('jsonReader');
|
||||
|
||||
|
||||
class ParseStream extends stream.Transform {
|
||||
constructor({ limitRows, jsonStyle, keyField }) {
|
||||
super({ objectMode: true });
|
||||
@@ -72,8 +71,12 @@ async function jsonReader({
|
||||
// @ts-ignore
|
||||
encoding
|
||||
);
|
||||
|
||||
const parseJsonStream = parser();
|
||||
fileStream.pipe(parseJsonStream);
|
||||
|
||||
const resultPipe = [fileStream, parseJsonStream];
|
||||
|
||||
// fileStream.pipe(parseJsonStream);
|
||||
|
||||
const parseStream = new ParseStream({ limitRows, jsonStyle, keyField });
|
||||
|
||||
@@ -81,15 +84,20 @@ async function jsonReader({
|
||||
|
||||
if (rootField) {
|
||||
const filterStream = pick({ filter: rootField });
|
||||
parseJsonStream.pipe(filterStream);
|
||||
filterStream.pipe(tramsformer);
|
||||
} else {
|
||||
parseJsonStream.pipe(tramsformer);
|
||||
resultPipe.push(filterStream);
|
||||
// parseJsonStream.pipe(filterStream);
|
||||
// filterStream.pipe(tramsformer);
|
||||
}
|
||||
// else {
|
||||
// parseJsonStream.pipe(tramsformer);
|
||||
// }
|
||||
|
||||
tramsformer.pipe(parseStream);
|
||||
resultPipe.push(tramsformer);
|
||||
resultPipe.push(parseStream);
|
||||
|
||||
return parseStream;
|
||||
// tramsformer.pipe(parseStream);
|
||||
|
||||
return resultPipe;
|
||||
}
|
||||
|
||||
module.exports = jsonReader;
|
||||
|
||||
@@ -99,9 +99,10 @@ async function jsonWriter({ fileName, jsonStyle, keyField = '_key', rootField, e
|
||||
logger.info(`Writing file ${fileName}`);
|
||||
const stringify = new StringifyStream({ jsonStyle, keyField, rootField });
|
||||
const fileStream = fs.createWriteStream(fileName, encoding);
|
||||
stringify.pipe(fileStream);
|
||||
stringify['finisher'] = fileStream;
|
||||
return stringify;
|
||||
return [stringify, fileStream];
|
||||
// stringify.pipe(fileStream);
|
||||
// stringify['finisher'] = fileStream;
|
||||
// return stringify;
|
||||
}
|
||||
|
||||
module.exports = jsonWriter;
|
||||
|
||||
@@ -141,8 +141,9 @@ async function modifyJsonLinesReader({
|
||||
);
|
||||
const liner = byline(fileStream);
|
||||
const parser = new ParseStream({ limitRows, changeSet, mergedRows, mergeKey, mergeMode });
|
||||
liner.pipe(parser);
|
||||
return parser;
|
||||
return [liner, parser];
|
||||
// liner.pipe(parser);
|
||||
// return parser;
|
||||
}
|
||||
|
||||
module.exports = modifyJsonLinesReader;
|
||||
|
||||
@@ -44,9 +44,10 @@ async function sqlDataWriter({ fileName, dataName, driver, encoding = 'utf-8' })
|
||||
logger.info(`Writing file ${fileName}`);
|
||||
const stringify = new SqlizeStream({ fileName, dataName });
|
||||
const fileStream = fs.createWriteStream(fileName, encoding);
|
||||
stringify.pipe(fileStream);
|
||||
stringify['finisher'] = fileStream;
|
||||
return stringify;
|
||||
return [stringify, fileStream];
|
||||
// stringify.pipe(fileStream);
|
||||
// stringify['finisher'] = fileStream;
|
||||
// return stringify;
|
||||
}
|
||||
|
||||
module.exports = sqlDataWriter;
|
||||
|
||||
18
packages/api/src/utility/streamPipeline.js
Normal file
18
packages/api/src/utility/streamPipeline.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const stream = require('stream');
|
||||
const _ = require('lodash');
|
||||
|
||||
function streamPipeline(...processedStreams) {
|
||||
const streams = _.flattenDeep(processedStreams);
|
||||
return new Promise((resolve, reject) => {
|
||||
// @ts-ignore
|
||||
stream.pipeline(...streams, err => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = streamPipeline;
|
||||
Reference in New Issue
Block a user