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,
async saveJslData({ folder, file, jslid }) {
async saveJslData({ folder, file, jslid, changeSet }) {
const source = getJslFileName(jslid);
const target = path.join(resolveArchiveFolder(folder), `${file}.jsonl`);
await fs.copyFile(source, target);
socket.emitChanged(`archive-files-changed`, { folder });
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);
socket.emitChanged(`archive-files-changed`, { folder });
}
return true;
},

View File

@ -147,6 +147,12 @@ module.exports = {
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({ jslid }) {
const file = `${getJslFileName(jslid)}.stats`;

View File

@ -18,7 +18,8 @@
<script lang="ts">
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';
@ -29,9 +30,11 @@
import runCommand from '../commands/runCommand';
import JslDataGrid from '../datagrid/JslDataGrid.svelte';
import { showModal } from '../modals/modalTools';
import SaveArchiveModal from '../modals/SaveArchiveModal.svelte';
import useEditorData from '../query/useEditorData';
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 createUndoReducer from '../utility/createUndoReducer';
@ -43,6 +46,7 @@
export let tabid;
let infoLoadCounter = 0;
let jslidChecked = false;
const quickExportHandlerRef = createQuickExportHandlerRef();
@ -68,12 +72,34 @@
}
}
export async function save() {
await apiCall('archive/modify-file', {
folder: archiveFolder,
file: archiveFile,
changeSet: $changeSetStore.value,
async function saveAs() {
showModal(SaveArchiveModal, {
onSave: doSaveAs,
});
}
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;
dispatchChangeSet({ type: 'reset', value: createChangeSet() });
if (structureChanged) {
@ -83,22 +109,53 @@
runCommand('dataGrid.refresh');
}
export function canSave() {
return changeSetContainsChanges($changeSetStore?.value);
export async function save() {
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>
<ToolStripContainer>
<JslDataGrid
jslid={jslid || `archive://${archiveFolder}/${archiveFile}`}
supportsReload
allowChangeChangeSetStructure
changeSetState={$changeSetStore}
focusOnVisible
{changeSetStore}
{dispatchChangeSet}
{infoLoadCounter}
/>
{#if jslidChecked || !jslid}
<JslDataGrid
jslid={jslid || `archive://${archiveFolder}/${archiveFile}`}
supportsReload
allowChangeChangeSetStructure
changeSetState={$changeSetStore}
focusOnVisible
{changeSetStore}
{dispatchChangeSet}
{infoLoadCounter}
/>
{/if}
<svelte:fragment slot="toolstrip">
<ToolStripCommandButton command="dataGrid.refresh" />
<ToolStripExportButton command="jslTableGrid.export" {quickExportHandlerRef} />

View File

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

View File

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