quick export - current archive

This commit is contained in:
Jan Prochazka
2023-02-24 17:22:11 +01:00
parent d024b6f25c
commit a519c78301
4 changed files with 68 additions and 26 deletions

View File

@@ -37,8 +37,9 @@ export interface PluginDefinition {
export interface QuickExportDefinition { export interface QuickExportDefinition {
label: string; label: string;
createWriter: (fileName: string) => { functionName: string; props: any }; createWriter: (fileName: string, dataName?: string) => { functionName: string; props: any };
extension: string; extension: string;
noFilenameDependency?: boolean;
} }
export interface ExtensionsDirectory { export interface ExtensionsDirectory {

View File

@@ -251,3 +251,9 @@ export function subscribeApiDependendStores() {
} }
}); });
} }
let currentArchiveValue = null;
currentArchive.subscribe(value => {
currentArchiveValue = value;
});
export const getCurrentArchive = () => currentArchiveValue;

View File

@@ -1,5 +1,5 @@
import type { QuickExportDefinition } from 'dbgate-types'; import type { QuickExportDefinition } from 'dbgate-types';
import { getExtensions } from '../stores'; import { currentArchive, getCurrentArchive, getExtensions } from '../stores';
export function createQuickExportMenuItems(handler: (fmt: QuickExportDefinition) => Function, advancedExportMenuItem) { export function createQuickExportMenuItems(handler: (fmt: QuickExportDefinition) => Function, advancedExportMenuItem) {
const extensions = getExtensions(); const extensions = getExtensions();
@@ -9,6 +9,22 @@ export function createQuickExportMenuItems(handler: (fmt: QuickExportDefinition)
onClick: handler(fmt), onClick: handler(fmt),
})), })),
{ divider: true }, { divider: true },
{
text: 'Current archive',
onClick: handler({
extension: 'jsonl',
label: 'Current archive',
noFilenameDependency: true,
createWriter: (fileName, dataName) => ({
functionName: 'archiveWriter',
props: {
fileName: dataName,
folderName: getCurrentArchive(),
},
}),
}),
},
{ divider: true },
{ {
text: 'More...', text: 'More...',
...advancedExportMenuItem, ...advancedExportMenuItem,

View File

@@ -7,6 +7,7 @@ import { normalizeExportColumnMap } from '../impexp/createImpExpScript';
import { getCurrentConfig } from '../stores'; import { getCurrentConfig } from '../stores';
import { showModal } from '../modals/modalTools'; import { showModal } from '../modals/modalTools';
import RunScriptModal from '../modals/RunScriptModal.svelte'; import RunScriptModal from '../modals/RunScriptModal.svelte';
import { QuickExportDefinition } from 'dbgate-types';
export async function importSqlDump(inputFile, connection) { export async function importSqlDump(inputFile, connection) {
const script = getCurrentConfig().allowShellScripting ? new ScriptWriter() : new ScriptWriterJson(); const script = getCurrentConfig().allowShellScripting ? new ScriptWriter() : new ScriptWriterJson();
@@ -117,35 +118,53 @@ export async function saveExportedFile(filters, defaultPath, extension, dataName
}); });
} }
export async function exportQuickExportFile(dataName, reader, format, columnMap = null) { function generateQuickExportScript(
await saveExportedFile( reader,
[{ name: format.label, extensions: [format.extension] }], format: QuickExportDefinition,
`${dataName}.${format.extension}`, filePath: string,
format.extension, dataName: string,
dataName, columnMap
filePath => { ) {
const script = getCurrentConfig().allowShellScripting ? new ScriptWriter() : new ScriptWriterJson(); const script = getCurrentConfig().allowShellScripting ? new ScriptWriter() : new ScriptWriterJson();
const sourceVar = script.allocVariable(); const sourceVar = script.allocVariable();
script.assign(sourceVar, reader.functionName, reader.props); script.assign(sourceVar, reader.functionName, reader.props);
const targetVar = script.allocVariable(); const targetVar = script.allocVariable();
const writer = format.createWriter(filePath, dataName); const writer = format.createWriter(filePath, dataName);
script.assign(targetVar, writer.functionName, writer.props); script.assign(targetVar, writer.functionName, writer.props);
const colmap = normalizeExportColumnMap(columnMap); const colmap = normalizeExportColumnMap(columnMap);
let colmapVar = null; let colmapVar = null;
if (colmap) { if (colmap) {
colmapVar = script.allocVariable(); colmapVar = script.allocVariable();
script.assignValue(colmapVar, colmap); script.assignValue(colmapVar, colmap);
} }
script.copyStream(sourceVar, targetVar, colmapVar); script.copyStream(sourceVar, targetVar, colmapVar);
script.endLine(); script.endLine();
return script.getScript(); return script.getScript();
} }
);
export async function exportQuickExportFile(dataName, reader, format: QuickExportDefinition, columnMap = null) {
if (format.noFilenameDependency) {
const script = generateQuickExportScript(reader, format, null, dataName, columnMap);
runImportExportScript({
script,
runningMessage: `Exporting ${dataName}`,
canceledMessage: `Export ${dataName} canceled`,
finishedMessage: `Export ${dataName} finished`,
});
} else {
await saveExportedFile(
[{ name: format.label, extensions: [format.extension] }],
`${dataName}.${format.extension}`,
format.extension,
dataName,
filePath => generateQuickExportScript(reader, format, filePath, dataName, columnMap)
);
}
} }
// export async function exportSqlDump(connection, databaseName) { // export async function exportSqlDump(connection, databaseName) {