mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-01 07:23:58 +00:00
insert multiple JSON documents
This commit is contained in:
@@ -390,6 +390,25 @@ export function changeSetInsertNewRow(changeSet: ChangeSet, name?: NamedObjectIn
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function changeSetInsertDocuments(
|
||||||
|
changeSet: ChangeSet,
|
||||||
|
documents: any[],
|
||||||
|
name?: NamedObjectInfo
|
||||||
|
): ChangeSet {
|
||||||
|
const insertedRows = getChangeSetInsertedRows(changeSet, name);
|
||||||
|
return {
|
||||||
|
...changeSet,
|
||||||
|
inserts: [
|
||||||
|
...changeSet.inserts,
|
||||||
|
...documents.map((doc, index) => ({
|
||||||
|
...name,
|
||||||
|
insertedRowIndex: insertedRows.length + index,
|
||||||
|
fields: doc,
|
||||||
|
})),
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function changeSetContainsChanges(changeSet: ChangeSet) {
|
export function changeSetContainsChanges(changeSet: ChangeSet) {
|
||||||
if (!changeSet) return false;
|
if (!changeSet) return false;
|
||||||
return changeSet.deletes.length > 0 || changeSet.updates.length > 0 || changeSet.inserts.length > 0;
|
return changeSet.deletes.length > 0 || changeSet.updates.length > 0 || changeSet.inserts.length > 0;
|
||||||
|
|||||||
@@ -98,8 +98,9 @@ export class CollectionGridDisplay extends GridDisplay {
|
|||||||
changeSet
|
changeSet
|
||||||
) {
|
) {
|
||||||
super(config, setConfig, cache, setCache, driver);
|
super(config, setConfig, cache, setCache, driver);
|
||||||
const changedDocs = _.compact([...changeSet.inserts, ...changeSet.updates].map(chs => chs.document));
|
const changedDocs = _.compact(changeSet.updates.map(chs => chs.document));
|
||||||
this.columns = analyseCollectionDisplayColumns([...(loadedRows || []), ...changedDocs], this);
|
const insertedDocs = _.compact(changeSet.inserts.map(chs => chs.fields));
|
||||||
|
this.columns = analyseCollectionDisplayColumns([...(loadedRows || []), ...changedDocs, ...insertedDocs], this);
|
||||||
this.filterable = true;
|
this.filterable = true;
|
||||||
this.sortable = true;
|
this.sortable = true;
|
||||||
this.editable = true;
|
this.editable = true;
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import {
|
|||||||
setChangeSetRowData,
|
setChangeSetRowData,
|
||||||
compileMacroFunction,
|
compileMacroFunction,
|
||||||
runMacroOnValue,
|
runMacroOnValue,
|
||||||
|
changeSetInsertDocuments,
|
||||||
} from 'dbgate-datalib';
|
} from 'dbgate-datalib';
|
||||||
import Grider, { GriderRowStatus } from './Grider';
|
import Grider, { GriderRowStatus } from './Grider';
|
||||||
|
|
||||||
@@ -181,6 +182,12 @@ export default class ChangeSetGrider extends Grider {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
insertDocuments(documents: any[]): number {
|
||||||
|
const res = this.rowCountInUpdate;
|
||||||
|
this.applyModification(chs => changeSetInsertDocuments(chs, documents, this.display.baseTableOrCollection));
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
beginUpdate() {
|
beginUpdate() {
|
||||||
this.batchChangeSet = this.changeSet;
|
this.batchChangeSet = this.changeSet;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1094,7 +1094,7 @@
|
|||||||
return [row, col];
|
return [row, col];
|
||||||
}
|
}
|
||||||
|
|
||||||
function handlePaste(event) {
|
async function handlePaste(event) {
|
||||||
var pastedText = undefined;
|
var pastedText = undefined;
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if (window.clipboardData && window.clipboardData.getData) {
|
if (window.clipboardData && window.clipboardData.getData) {
|
||||||
@@ -1105,41 +1105,62 @@
|
|||||||
pastedText = event.clipboardData.getData('text/plain');
|
pastedText = event.clipboardData.getData('text/plain');
|
||||||
}
|
}
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
grider.beginUpdate();
|
|
||||||
const pasteRows = pastedText
|
let json = null;
|
||||||
.replace(/\r/g, '')
|
if (grider.canInsert) {
|
||||||
.split('\n')
|
try {
|
||||||
.map(row => row.split('\t'));
|
json = JSON.parse(pastedText);
|
||||||
const selectedRegular = cellsToRegularCells(selectedCells);
|
} catch (e) {
|
||||||
if (selectedRegular.length <= 1) {
|
json = null;
|
||||||
const startRow = isRegularCell(currentCell) ? currentCell[0] : grider.rowCount;
|
|
||||||
const startCol = isRegularCell(currentCell) ? currentCell[1] : 0;
|
|
||||||
let rowIndex = startRow;
|
|
||||||
for (const rowData of pasteRows) {
|
|
||||||
if (rowIndex >= grider.rowCountInUpdate) {
|
|
||||||
grider.insertRow();
|
|
||||||
}
|
|
||||||
let colIndex = startCol;
|
|
||||||
for (const cell of rowData) {
|
|
||||||
setCellValue([rowIndex, colIndex], cell == '(NULL)' ? null : cell);
|
|
||||||
colIndex += 1;
|
|
||||||
}
|
|
||||||
rowIndex += 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (selectedRegular.length > 1) {
|
|
||||||
const startRow: number = _.min(selectedRegular.map(x => x[0]));
|
if (json && (_.isArray(json) || _.isPlainObject(json))) {
|
||||||
const startCol: number = _.min(selectedRegular.map(x => x[1]));
|
const rowIndex = grider.insertDocuments(_.isArray(json) ? json : [json]);
|
||||||
for (const cell of selectedRegular) {
|
const cell = [rowIndex, (currentCell && currentCell[1]) || 0];
|
||||||
const [rowIndex, colIndex] = cell;
|
// @ts-ignore
|
||||||
const selectionRow = rowIndex - startRow;
|
currentCell = cell;
|
||||||
const selectionCol = colIndex - startCol;
|
// @ts-ignore
|
||||||
const pasteRow = pasteRows[selectionRow % pasteRows.length];
|
selectedCells = [cell];
|
||||||
const pasteCell = pasteRow[selectionCol % pasteRow.length];
|
await tick();
|
||||||
setCellValue(cell, pasteCell);
|
scrollIntoView(cell);
|
||||||
|
} else {
|
||||||
|
grider.beginUpdate();
|
||||||
|
const pasteRows = pastedText
|
||||||
|
.replace(/\r/g, '')
|
||||||
|
.split('\n')
|
||||||
|
.map(row => row.split('\t'));
|
||||||
|
const selectedRegular = cellsToRegularCells(selectedCells);
|
||||||
|
if (selectedRegular.length <= 1) {
|
||||||
|
const startRow = isRegularCell(currentCell) ? currentCell[0] : grider.rowCount;
|
||||||
|
const startCol = isRegularCell(currentCell) ? currentCell[1] : 0;
|
||||||
|
let rowIndex = startRow;
|
||||||
|
for (const rowData of pasteRows) {
|
||||||
|
if (rowIndex >= grider.rowCountInUpdate) {
|
||||||
|
grider.insertRow();
|
||||||
|
}
|
||||||
|
let colIndex = startCol;
|
||||||
|
for (const cell of rowData) {
|
||||||
|
setCellValue([rowIndex, colIndex], cell == '(NULL)' ? null : cell);
|
||||||
|
colIndex += 1;
|
||||||
|
}
|
||||||
|
rowIndex += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (selectedRegular.length > 1) {
|
||||||
|
const startRow: number = _.min(selectedRegular.map(x => x[0]));
|
||||||
|
const startCol: number = _.min(selectedRegular.map(x => x[1]));
|
||||||
|
for (const cell of selectedRegular) {
|
||||||
|
const [rowIndex, colIndex] = cell;
|
||||||
|
const selectionRow = rowIndex - startRow;
|
||||||
|
const selectionCol = colIndex - startCol;
|
||||||
|
const pasteRow = pasteRows[selectionRow % pasteRows.length];
|
||||||
|
const pasteCell = pasteRow[selectionCol % pasteRow.length];
|
||||||
|
setCellValue(cell, pasteCell);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
grider.endUpdate();
|
||||||
}
|
}
|
||||||
grider.endUpdate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function cellsToRegularCells(cells) {
|
function cellsToRegularCells(cells) {
|
||||||
@@ -1296,7 +1317,7 @@
|
|||||||
<ErrorInfo message={errorMessage} alignTop />
|
<ErrorInfo message={errorMessage} alignTop />
|
||||||
{:else if isDynamicStructure && isLoadedAll && grider?.rowCount == 0}
|
{:else if isDynamicStructure && isLoadedAll && grider?.rowCount == 0}
|
||||||
<div>
|
<div>
|
||||||
<ErrorInfo alignTop message="No rows loaded, check filter or add new documents" />
|
<ErrorInfo alignTop message="No rows loaded, check filter or add new documents. You could copy documents from ohter collections/tables with Copy advanved/Copy as JSON command." />
|
||||||
{#if display.filterCount > 0}
|
{#if display.filterCount > 0}
|
||||||
<FormStyledButton value="Reset filter" on:click={() => display.clearFilters()} />
|
<FormStyledButton value="Reset filter" on:click={() => display.clearFilters()} />
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
@@ -22,6 +22,9 @@ export default abstract class Grider {
|
|||||||
insertRow(): number {
|
insertRow(): number {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
insertDocuments(documents: any[]): number {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
revertRowChanges(index: number) {}
|
revertRowChanges(index: number) {}
|
||||||
revertAllChanges() {}
|
revertAllChanges() {}
|
||||||
undo() {}
|
undo() {}
|
||||||
|
|||||||
@@ -47,6 +47,15 @@ export default class FreeTableGrider extends Grider {
|
|||||||
};
|
};
|
||||||
return this.currentModel.rows.length - 1;
|
return this.currentModel.rows.length - 1;
|
||||||
}
|
}
|
||||||
|
insertDocuments(documents: any[]): number {
|
||||||
|
const model = this.currentModel;
|
||||||
|
this.currentModel = {
|
||||||
|
...model,
|
||||||
|
rows: [...model.rows, ...documents],
|
||||||
|
};
|
||||||
|
return this.currentModel.rows.length - documents.length;
|
||||||
|
}
|
||||||
|
|
||||||
deleteRow(index: number) {
|
deleteRow(index: number) {
|
||||||
const model = this.currentModel;
|
const model = this.currentModel;
|
||||||
this.currentModel = {
|
this.currentModel = {
|
||||||
|
|||||||
@@ -56,6 +56,7 @@
|
|||||||
import EditJsonModal from '../modals/EditJsonModal.svelte';
|
import EditJsonModal from '../modals/EditJsonModal.svelte';
|
||||||
import ChangeSetGrider from '../datagrid/ChangeSetGrider';
|
import ChangeSetGrider from '../datagrid/ChangeSetGrider';
|
||||||
import { setContext } from 'svelte';
|
import { setContext } from 'svelte';
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
export let tabid;
|
export let tabid;
|
||||||
export let conid;
|
export let conid;
|
||||||
@@ -139,8 +140,7 @@
|
|||||||
json: {},
|
json: {},
|
||||||
onSave: value => {
|
onSave: value => {
|
||||||
const grider = new ChangeSetGrider(loadedRows, $changeSetStore, dispatchChangeSet, display);
|
const grider = new ChangeSetGrider(loadedRows, $changeSetStore, dispatchChangeSet, display);
|
||||||
const newRowIndex = grider.insertRow();
|
grider.insertDocuments(_.isArray(value) ? value : [value]);
|
||||||
grider.setRowData(newRowIndex, value);
|
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user