From 29e6dad713a9ecf97d25a29a37467256fa8980ee Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Mon, 5 Apr 2021 13:21:52 +0200 Subject: [PATCH] mongo changeset WIP --- packages/datalib/src/ChangeSet.ts | 3 ++ packages/datalib/src/CollectionGridDisplay.ts | 6 +++- packages/datalib/src/GridDisplay.ts | 29 +++++++++++++++---- packages/web/src/datagrid/ChangeSetGrider.ts | 8 +++-- .../src/jsonview/CollectionJsonView.svelte | 3 +- 5 files changed, 38 insertions(+), 11 deletions(-) diff --git a/packages/datalib/src/ChangeSet.ts b/packages/datalib/src/ChangeSet.ts index 37d8c675..8fd21d61 100644 --- a/packages/datalib/src/ChangeSet.ts +++ b/packages/datalib/src/ChangeSet.ts @@ -6,6 +6,7 @@ export interface ChangeSetItem { pureName: string; schemaName?: string; insertedRowIndex?: number; + document?: any; condition?: { [column: string]: string }; fields?: { [column: string]: string }; } @@ -77,6 +78,7 @@ export function setChangeSetValue( definition: ChangeSetFieldDefinition, value: string ): ChangeSet { + console.log('SET', changeSet, definition, value); if (!changeSet || !definition) return changeSet; let [fieldName, existingItem] = findExistingChangeSetItem(changeSet, definition); if (fieldName == 'deletes') { @@ -332,6 +334,7 @@ export function getChangeSetInsertedRows(changeSet: ChangeSet, name?: NamedObjec } export function changeSetInsertNewRow(changeSet: ChangeSet, name?: NamedObjectInfo): ChangeSet { + console.log('INSERT', name); const insertedRows = getChangeSetInsertedRows(changeSet, name); return { ...changeSet, diff --git a/packages/datalib/src/CollectionGridDisplay.ts b/packages/datalib/src/CollectionGridDisplay.ts index 5987df74..3d89bd9a 100644 --- a/packages/datalib/src/CollectionGridDisplay.ts +++ b/packages/datalib/src/CollectionGridDisplay.ts @@ -39,9 +39,11 @@ export class CollectionGridDisplay extends GridDisplay { this.columns = this.getDisplayColumns(loadedRows || []); this.filterable = true; this.sortable = true; - this.editable = false; + this.editable = true; this.supportsReload = true; this.isDynamicStructure = true; + this.changeSetKeyFields = ['_id']; + this.baseCollection = collection; } getDisplayColumns(rows) { @@ -95,6 +97,8 @@ export class CollectionGridDisplay extends GridDisplay { isStructured: true, parentHeaderText: createHeaderText(basePath), filterType: 'mongo', + pureName: this.collection.pureName, + schemaName: this.collection.schemaName, }; } } diff --git a/packages/datalib/src/GridDisplay.ts b/packages/datalib/src/GridDisplay.ts index b56ae30a..ddc2ef07 100644 --- a/packages/datalib/src/GridDisplay.ts +++ b/packages/datalib/src/GridDisplay.ts @@ -1,6 +1,14 @@ import _ from 'lodash'; import { GridConfig, GridCache, GridConfigColumns, createGridCache, GroupFunc } from './GridConfig'; -import { ForeignKeyInfo, TableInfo, ColumnInfo, EngineDriver, NamedObjectInfo, DatabaseInfo } from 'dbgate-types'; +import { + ForeignKeyInfo, + TableInfo, + ColumnInfo, + EngineDriver, + NamedObjectInfo, + DatabaseInfo, + CollectionInfo, +} from 'dbgate-types'; import { parseFilter, getFilterType } from 'dbgate-filterparser'; import { filterName } from './filterName'; import { ChangeSetFieldDefinition, ChangeSetRowDefinition } from './ChangeSet'; @@ -56,6 +64,10 @@ export abstract class GridDisplay { ) {} columns: DisplayColumn[]; baseTable?: TableInfo; + baseCollection?: CollectionInfo; + get baseTableOrCollection(): NamedObjectInfo { + return this.baseTable || this.baseCollection; + } changeSetKeyFields: string[] = null; sortable = false; filterable = false; @@ -391,8 +403,12 @@ export abstract class GridDisplay { getChangeSetField(row, uniqueName, insertedRowIndex): ChangeSetFieldDefinition { const col = this.columns.find(x => x.uniqueName == uniqueName); if (!col) return null; - if (!this.baseTable) return null; - if (this.baseTable.pureName != col.pureName || this.baseTable.schemaName != col.schemaName) return null; + const baseTableOrCollection = this.baseTableOrCollection; + if (!baseTableOrCollection) return null; + if (baseTableOrCollection.pureName != col.pureName || baseTableOrCollection.schemaName != col.schemaName) { + return null; + } + return { ...this.getChangeSetRow(row, insertedRowIndex), uniqueName: uniqueName, @@ -401,10 +417,11 @@ export abstract class GridDisplay { } getChangeSetRow(row, insertedRowIndex): ChangeSetRowDefinition { - if (!this.baseTable) return null; + const baseTableOrCollection = this.baseTableOrCollection; + if (!baseTableOrCollection) return null; return { - pureName: this.baseTable.pureName, - schemaName: this.baseTable.schemaName, + pureName: baseTableOrCollection.pureName, + schemaName: baseTableOrCollection.schemaName, insertedRowIndex, condition: insertedRowIndex == null ? this.getChangeSetCondition(row) : null, }; diff --git a/packages/web/src/datagrid/ChangeSetGrider.ts b/packages/web/src/datagrid/ChangeSetGrider.ts index af3167d5..4cb7428b 100644 --- a/packages/web/src/datagrid/ChangeSetGrider.ts +++ b/packages/web/src/datagrid/ChangeSetGrider.ts @@ -39,7 +39,7 @@ export default class ChangeSetGrider extends Grider { ) { super(); this.changeSet = changeSetState && changeSetState.value; - this.insertedRows = getChangeSetInsertedRows(this.changeSet, display?.baseTable); + this.insertedRows = getChangeSetInsertedRows(this.changeSet, display?.baseTableOrCollection); this.setChangeSet = value => dispatchChangeSet({ type: 'set', value }); this.rowCacheIndexes = new Set(); this.rowDataCache = {}; @@ -47,6 +47,8 @@ export default class ChangeSetGrider extends Grider { this.rowDefinitionsCache = {}; this.batchChangeSet = null; this.compiledMacroFunc = compileMacroFunction(macro, this._errors); + + console.log('changeSet', this.changeSet); } get errors() { @@ -110,7 +112,7 @@ export default class ChangeSetGrider extends Grider { } get canInsert() { - return !!this.display.baseTable; + return !!this.display.baseTableOrCollection; } getRowData(index: number) { @@ -157,7 +159,7 @@ export default class ChangeSetGrider extends Grider { insertRow(): number { const res = this.rowCountInUpdate; - this.applyModification(chs => changeSetInsertNewRow(chs, this.display.baseTable)); + this.applyModification(chs => changeSetInsertNewRow(chs, this.display.baseTableOrCollection)); return res; } diff --git a/packages/web/src/jsonview/CollectionJsonView.svelte b/packages/web/src/jsonview/CollectionJsonView.svelte index 02733734..1e1739e4 100644 --- a/packages/web/src/jsonview/CollectionJsonView.svelte +++ b/packages/web/src/jsonview/CollectionJsonView.svelte @@ -98,9 +98,10 @@