antares/src/main/workers/exporter.ts

71 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-04-15 23:13:23 +02:00
import * as antares from 'common/interfaces/antares';
import * as fs from 'fs';
2022-04-15 23:13:23 +02:00
import { MySQLClient } from '../libs/clients/MySQLClient';
import { PostgreSQLClient } from '../libs/clients/PostgreSQLClient';
import { ClientsFactory } from '../libs/ClientsFactory';
2022-04-15 23:13:23 +02:00
import MysqlExporter from '../libs/exporters/sql/MysqlExporter';
import PostgreSQLExporter from '../libs/exporters/sql/PostgreSQLExporter';
2022-04-15 23:13:23 +02:00
let exporter: antares.Exporter;
process.on('message', async ({ type, client, tables, options }: any) => {
if (type === 'init') {
2022-03-12 10:01:22 +01:00
const connection = await ClientsFactory.getClient({
client: client.name,
params: client.config,
poolSize: 5
2022-04-15 23:13:23 +02:00
}) as MySQLClient | PostgreSQLClient;
await connection.connect();
switch (client.name) {
case 'mysql':
case 'maria':
2022-04-15 23:13:23 +02:00
exporter = new MysqlExporter(connection as MySQLClient, tables, options);
break;
2022-03-21 18:32:45 +01:00
case 'pg':
2022-04-15 23:13:23 +02:00
exporter = new PostgreSQLExporter(connection as PostgreSQLClient, tables, options);
2022-03-21 18:32:45 +01:00
break;
default:
process.send({
type: 'error',
payload: `"${client.name}" exporter not aviable`
});
return;
}
exporter.once('error', err => {
console.error(err);
process.send({
type: 'error',
payload: err.toString()
});
});
exporter.once('end', () => {
process.send({
type: 'end',
payload: { cancelled: exporter.isCancelled }
});
connection.destroy();
});
exporter.once('cancel', () => {
fs.unlinkSync(exporter.outputFile);
process.send({ type: 'cancel' });
});
exporter.on('progress', state => {
process.send({
type: 'export-progress',
payload: state
});
});
exporter.run();
}
else if (type === 'cancel')
exporter.cancel();
});
2022-04-15 23:13:23 +02:00
process.on('beforeExit', console.log);