save jsl data

This commit is contained in:
Jan Prochazka
2023-02-25 11:34:19 +01:00
parent a77492440e
commit 1c73920dd5
5 changed files with 108 additions and 31 deletions

View File

@@ -169,11 +169,20 @@ module.exports = {
}, },
saveJslData_meta: true, saveJslData_meta: true,
async saveJslData({ folder, file, jslid }) { async saveJslData({ folder, file, jslid, changeSet }) {
const source = getJslFileName(jslid); const source = getJslFileName(jslid);
const target = path.join(resolveArchiveFolder(folder), `${file}.jsonl`); const target = path.join(resolveArchiveFolder(folder), `${file}.jsonl`);
if (changeSet) {
const reader = await dbgateApi.modifyJsonLinesReader({
fileName: source,
changeSet,
});
const writer = await dbgateApi.jsonLinesWriter({ fileName: target });
await dbgateApi.copyStream(reader, writer);
} else {
await fs.copyFile(source, target); await fs.copyFile(source, target);
socket.emitChanged(`archive-files-changed`, { folder }); socket.emitChanged(`archive-files-changed`, { folder });
}
return true; return true;
}, },

View File

@@ -147,6 +147,12 @@ module.exports = {
return datastore.getRows(offset, limit, _.isEmpty(filters) ? null : filters, _.isEmpty(sort) ? null : sort); return datastore.getRows(offset, limit, _.isEmpty(filters) ? null : filters, _.isEmpty(sort) ? null : sort);
}, },
exists_meta: true,
async exists({ jslid }) {
const fileName = getJslFileName(jslid);
return fs.existsSync(fileName);
},
getStats_meta: true, getStats_meta: true,
getStats({ jslid }) { getStats({ jslid }) {
const file = `${getJslFileName(jslid)}.stats`; const file = `${getJslFileName(jslid)}.stats`;

View File

@@ -18,7 +18,8 @@
<script lang="ts"> <script lang="ts">
import { changeSetContainsChanges, createChangeSet } from 'dbgate-datalib'; import { changeSetContainsChanges, createChangeSet } from 'dbgate-datalib';
import { tick } from 'svelte'; import localforage from 'localforage';
import { onMount, tick } from 'svelte';
import ToolStripCommandButton from '../buttons/ToolStripCommandButton.svelte'; import ToolStripCommandButton from '../buttons/ToolStripCommandButton.svelte';
@@ -29,9 +30,11 @@
import runCommand from '../commands/runCommand'; import runCommand from '../commands/runCommand';
import JslDataGrid from '../datagrid/JslDataGrid.svelte'; import JslDataGrid from '../datagrid/JslDataGrid.svelte';
import { showModal } from '../modals/modalTools';
import SaveArchiveModal from '../modals/SaveArchiveModal.svelte';
import useEditorData from '../query/useEditorData'; import useEditorData from '../query/useEditorData';
import { apiCall } from '../utility/api'; import { apiCall } from '../utility/api';
import { markTabSaved, markTabUnsaved } from '../utility/common'; import { changeTab, markTabSaved, markTabUnsaved, sleep } from '../utility/common';
import createActivator, { getActiveComponent } from '../utility/createActivator'; import createActivator, { getActiveComponent } from '../utility/createActivator';
import createUndoReducer from '../utility/createUndoReducer'; import createUndoReducer from '../utility/createUndoReducer';
@@ -43,6 +46,7 @@
export let tabid; export let tabid;
let infoLoadCounter = 0; let infoLoadCounter = 0;
let jslidChecked = false;
const quickExportHandlerRef = createQuickExportHandlerRef(); const quickExportHandlerRef = createQuickExportHandlerRef();
@@ -68,12 +72,34 @@
} }
} }
export async function save() { async function saveAs() {
await apiCall('archive/modify-file', { showModal(SaveArchiveModal, {
folder: archiveFolder, onSave: doSaveAs,
file: archiveFile,
changeSet: $changeSetStore.value,
}); });
}
const doSaveAs = async (folder, file) => {
await apiCall('archive/save-jsl-data', {
folder,
file,
jslid,
changeSet: changeSetContainsChanges($changeSetStore?.value) ? $changeSetStore.value : null,
});
changeTab(tabid, tab => ({
...tab,
title: file,
props: { archiveFile: file, archiveFolder: folder },
archiveFile: file,
archiveFolder: folder,
}));
if (changeSetContainsChanges($changeSetStore?.value)) {
await sleep(100);
afterSaveChangeSet();
}
};
async function afterSaveChangeSet() {
const structureChanged = !!$changeSetStore.value?.structure; const structureChanged = !!$changeSetStore.value?.structure;
dispatchChangeSet({ type: 'reset', value: createChangeSet() }); dispatchChangeSet({ type: 'reset', value: createChangeSet() });
if (structureChanged) { if (structureChanged) {
@@ -83,12 +109,42 @@
runCommand('dataGrid.refresh'); runCommand('dataGrid.refresh');
} }
export function canSave() { export async function save() {
return changeSetContainsChanges($changeSetStore?.value); if (jslid) {
saveAs();
} else {
await apiCall('archive/modify-file', {
folder: archiveFolder,
file: archiveFile,
changeSet: $changeSetStore.value,
});
await afterSaveChangeSet();
} }
}
export function canSave() {
return jslid || changeSetContainsChanges($changeSetStore?.value);
}
async function checkJslid() {
if (jslid) {
if (!(await apiCall('jsldata/exists', { jslid }))) {
const rows = await localforage.getItem(`tabdata_rows_${tabid}`);
if (rows) {
await apiCall('jsldata/save-rows', { jslid, rows });
}
}
}
jslidChecked = true;
}
onMount(() => {
checkJslid();
});
</script> </script>
<ToolStripContainer> <ToolStripContainer>
{#if jslidChecked || !jslid}
<JslDataGrid <JslDataGrid
jslid={jslid || `archive://${archiveFolder}/${archiveFile}`} jslid={jslid || `archive://${archiveFolder}/${archiveFile}`}
supportsReload supportsReload
@@ -99,6 +155,7 @@
{dispatchChangeSet} {dispatchChangeSet}
{infoLoadCounter} {infoLoadCounter}
/> />
{/if}
<svelte:fragment slot="toolstrip"> <svelte:fragment slot="toolstrip">
<ToolStripCommandButton command="dataGrid.refresh" /> <ToolStripCommandButton command="dataGrid.refresh" />
<ToolStripExportButton command="jslTableGrid.export" {quickExportHandlerRef} /> <ToolStripExportButton command="jslTableGrid.export" {quickExportHandlerRef} />

View File

@@ -5,13 +5,18 @@ import openNewTab from './openNewTab';
export async function openJsonLinesData(rows) { export async function openJsonLinesData(rows) {
const jslid = uuidv1(); const jslid = uuidv1();
await apiCall('jsldata/save-rows', { jslid, rows }); // await apiCall('jsldata/save-rows', { jslid, rows });
openNewTab({ openNewTab(
{
tabComponent: 'ArchiveFileTab', tabComponent: 'ArchiveFileTab',
icon: 'img archive', icon: 'img archive',
title: 'Data #', title: 'Data #',
props: { props: {
jslid, jslid,
}, },
}); },
{
rows,
}
);
} }

View File

@@ -65,7 +65,7 @@ export default async function openNewTab(newTab, initialData = undefined, option
const tabid = uuidv1(); const tabid = uuidv1();
if (initialData) { if (initialData) {
for (const key of _.keys(initialData)) { for (const key of _.keys(initialData)) {
if (key == 'editor') { if (key == 'editor' || key == 'rows') {
await localforage.setItem(`tabdata_${key}_${tabid}`, initialData[key]); await localforage.setItem(`tabdata_${key}_${tabid}`, initialData[key]);
} else { } else {
localStorage.setItem(`tabdata_${key}_${tabid}`, JSON.stringify(initialData[key])); localStorage.setItem(`tabdata_${key}_${tabid}`, JSON.stringify(initialData[key]));