feat: export database as zip sql file

This commit is contained in:
Fabio Di Stasio 2022-03-18 18:12:13 +01:00
parent db628f7722
commit 8f3efabb69
3 changed files with 40 additions and 13 deletions

View File

@ -1,4 +1,5 @@
import fs from 'fs'; import fs from 'fs';
import { createGzip } from 'zlib';
import path from 'path'; import path from 'path';
import EventEmitter from 'events'; import EventEmitter from 'events';
@ -8,12 +9,19 @@ export class BaseExporter extends EventEmitter {
this._tables = tables; this._tables = tables;
this._options = options; this._options = options;
this._isCancelled = false; this._isCancelled = false;
this._outputStream = fs.createWriteStream(this._options.outputFile, { this._outputFileStream = fs.createWriteStream(this._options.outputFile, { flags: 'w' });
flags: 'w' this._processedStream = null;
});
this._state = {}; this._state = {};
this._outputStream.once('error', err => { if (this._options.outputFormat === 'sql.zip') {
const outputZipStream = createGzip();
outputZipStream.pipe(this._outputFileStream);
this._processedStream = outputZipStream;
}
else
this._processedStream = this._outputFileStream;
this._processedStream.once('error', err => {
this._isCancelled = true; this._isCancelled = true;
this.emit('error', err); this.emit('error', err);
}); });
@ -29,7 +37,7 @@ export class BaseExporter extends EventEmitter {
throw err; throw err;
} }
finally { finally {
this._outputStream.end(); this._processedStream.end();
this.emit('end'); this.emit('end');
} }
} }
@ -68,7 +76,7 @@ export class BaseExporter extends EventEmitter {
const fileName = path.basename(this._options.outputFile); const fileName = path.basename(this._options.outputFile);
this.emit('error', `The file ${fileName} is not accessible`); this.emit('error', `The file ${fileName} is not accessible`);
} }
this._outputStream.write(data); this._processedStream.write(data);
} }
dump () { dump () {

View File

@ -181,11 +181,10 @@
</div> </div>
</div> </div>
<div class="column col-4"> <div class="column col-4">
<h4> <h5 class="h5">
{{ $t('word.options') }} {{ $t('word.options') }}
</h4> </h5>
<span>{{ $t('word.includes') }}:</span> <span class="h6">{{ $t('word.includes') }}:</span>
<label <label
v-for="(_, key) in options.includes" v-for="(_, key) in options.includes"
:key="key" :key="key"
@ -194,7 +193,7 @@
<input v-model="options.includes[key]" type="checkbox"><i class="form-icon" /> {{ $t(`word.${key}`) }} <input v-model="options.includes[key]" type="checkbox"><i class="form-icon" /> {{ $t(`word.${key}`) }}
</label> </label>
<div class="mt-4 mb-2"> <div class="h6 mt-4 mb-2">
{{ $t('message.newInserStmtEvery') }}: {{ $t('message.newInserStmtEvery') }}:
</div> </div>
<div class="columns"> <div class="columns">
@ -217,6 +216,22 @@
</select> </select>
</div> </div>
</div> </div>
<div class="h6 mb-2 mt-4">
{{ $t('message.ourputFormat') }}:
</div>
<div class="columns">
<div class="column h5 mb-4">
<select v-model="options.outputFormat" class="form-select">
<option value="sql">
{{ $t('message.singleFile', {ext: '.sql'}) }}
</option>
<option value="sql.zip">
{{ $t('message.zipCompressedFile', {ext: '.sql'}) }}
</option>
</select>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>
@ -275,6 +290,7 @@ export default {
tables: [], tables: [],
options: { options: {
includes: {}, includes: {},
outputFormat: 'sql',
sqlInsertAfter: 250, sqlInsertAfter: 250,
sqlInsertDivider: 'bytes' sqlInsertDivider: 'bytes'
}, },
@ -299,7 +315,7 @@ export default {
}, },
filename () { filename () {
const date = moment().format('YYYY-MM-DD'); const date = moment().format('YYYY-MM-DD');
return `${this.selectedSchema}_${date}.sql`; return `${this.selectedSchema}_${date}.${this.options.outputFormat}`;
}, },
dumpFilePath () { dumpFilePath () {
return `${this.basePath}/${this.filename}`; return `${this.basePath}/${this.filename}`;

View File

@ -283,7 +283,10 @@ module.exports = {
manualCommit: 'Manual commit', manualCommit: 'Manual commit',
actionSuccessful: '{action} successful', actionSuccessful: '{action} successful',
importQueryErrors: 'Warning: {n} error has accurrend | Warning: {n} errors occurred', importQueryErrors: 'Warning: {n} error has accurrend | Warning: {n} errors occurred',
executedQueries: '{n} query executed | {n} queries executed' executedQueries: '{n} query executed | {n} queries executed',
ourputFormat: 'Output format',
singleFile: 'Single {ext} file',
zipCompressedFile: 'ZIP compressed {ext} file'
}, },
faker: { faker: {
address: 'Address', address: 'Address',