From bf725dd5636d20447691b16564e97408cc1a03c1 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Thu, 17 Jun 2021 14:29:45 +0200 Subject: [PATCH] table editor --- packages/tools/src/schemaEditorTools.ts | 41 +++++++++++++++++-- packages/tools/src/structureTools.ts | 1 - packages/types/dbinfo.d.ts | 1 - .../src/tableeditor/ColumnEditorModal.svelte | 4 +- .../ColumnsConstraintEditorModal.svelte | 24 +++++++---- 5 files changed, 55 insertions(+), 16 deletions(-) diff --git a/packages/tools/src/schemaEditorTools.ts b/packages/tools/src/schemaEditorTools.ts index 79efaa86..4ea63bbe 100644 --- a/packages/tools/src/schemaEditorTools.ts +++ b/packages/tools/src/schemaEditorTools.ts @@ -1,8 +1,41 @@ import uuidv1 from 'uuid/v1'; import { ColumnInfo, ConstraintInfo, PrimaryKeyInfo, TableInfo } from 'dbgate-types'; -function processPrimaryKey(table: TableInfo, oldColumn: ColumnInfo, newColumn: ColumnInfo) { +export interface EditorColumnInfo extends ColumnInfo { + isPrimaryKey?: boolean; +} + +export function fillEditorColumnInfo(column: ColumnInfo, table: TableInfo): EditorColumnInfo { + return { + isPrimaryKey: !!(table.primaryKey && table.primaryKey.columns.find(x => x.columnName == column.columnName)), + ...column, + }; +} + +function processPrimaryKey(table: TableInfo, oldColumn: EditorColumnInfo, newColumn: EditorColumnInfo) { if (!oldColumn?.isPrimaryKey && newColumn?.isPrimaryKey) { + // let primaryKey = table.primaryKey + // if (!primaryKey) { + // primaryKey = { + // constraintType: 'primaryKey', + // pureName: table.pureName, + // schemaName: table.schemaName, + // columns: [], + // }; + // } + // return { + // ...table, + // primaryKey: { + // ...primaryKey, + // columns:[ + // ...primaryKey.columns, + // { + // columnName: newColumn.columnName, + // }, + + // ] + // } + // } if (!table.primaryKey) { table.primaryKey = { constraintType: 'primaryKey', @@ -34,7 +67,7 @@ function processPrimaryKey(table: TableInfo, oldColumn: ColumnInfo, newColumn: C } } -export function editorAddColumn(table: TableInfo, column: ColumnInfo): TableInfo { +export function editorAddColumn(table: TableInfo, column: EditorColumnInfo): TableInfo { const res = { ...table, columns: [...table.columns, { ...column, pairingId: uuidv1() }], @@ -45,7 +78,7 @@ export function editorAddColumn(table: TableInfo, column: ColumnInfo): TableInfo return res; } -export function editorModifyColumn(table: TableInfo, column: ColumnInfo): TableInfo { +export function editorModifyColumn(table: TableInfo, column: EditorColumnInfo): TableInfo { const oldColumn = table?.columns?.find(x => x.pairingId == column.pairingId); const res = { @@ -57,7 +90,7 @@ export function editorModifyColumn(table: TableInfo, column: ColumnInfo): TableI return res; } -export function editorDeleteColumn(table: TableInfo, column: ColumnInfo): TableInfo { +export function editorDeleteColumn(table: TableInfo, column: EditorColumnInfo): TableInfo { const res = { ...table, columns: table.columns.filter(col => col.pairingId != column.pairingId), diff --git a/packages/tools/src/structureTools.ts b/packages/tools/src/structureTools.ts index 6f863f4e..aabcae76 100644 --- a/packages/tools/src/structureTools.ts +++ b/packages/tools/src/structureTools.ts @@ -21,7 +21,6 @@ function fillTableExtendedInfo(db: DatabaseInfo): DatabaseInfo { columns: (obj.columns || []).map(column => ({ pureName: obj.pureName, schemaName: obj.schemaName, - isPrimaryKey: !!(obj.primaryKey && obj.primaryKey.columns.find(x => x.columnName == column.columnName)), ...column, })), primaryKey: obj.primaryKey diff --git a/packages/types/dbinfo.d.ts b/packages/types/dbinfo.d.ts index 87a608d2..6c4effef 100644 --- a/packages/types/dbinfo.d.ts +++ b/packages/types/dbinfo.d.ts @@ -52,7 +52,6 @@ export interface ColumnInfo extends NamedObjectInfo { isSparse: boolean; defaultValue: string; defaultConstraint: string; - isPrimaryKey?: boolean; // only used in editor } export interface DatabaseObjectInfo extends NamedObjectInfo { diff --git a/packages/web/src/tableeditor/ColumnEditorModal.svelte b/packages/web/src/tableeditor/ColumnEditorModal.svelte index efc76819..c58c37af 100644 --- a/packages/web/src/tableeditor/ColumnEditorModal.svelte +++ b/packages/web/src/tableeditor/ColumnEditorModal.svelte @@ -13,7 +13,7 @@ import ElectronFilesInput from '../impexp/ElectronFilesInput.svelte'; import DropDownButton from '../elements/DropDownButton.svelte'; import DataTypeEditor from './DataTypeEditor.svelte'; - import { editorAddColumn, editorDeleteColumn, editorModifyColumn } from 'dbgate-tools'; + import { editorAddColumn, editorDeleteColumn, editorModifyColumn, fillEditorColumnInfo } from 'dbgate-tools'; export let columnInfo; export let setTableInfo; @@ -22,7 +22,7 @@ - + {columnInfo ? 'Edit column' : `Add column ${(tableInfo?.columns || []).length + 1}`}
Column {index + 1}
- ({ - label: col.columnName, - value: col.columnName, - }))} - /> + {#key column.columnName} + ({ + label: col.columnName, + value: col.columnName, + }))} + on:change={e => { + if (e.detail) { + columns = columns.map((col, i) => (i == index ? { columnName: e.detail } : col)); + } + }} + /> + {/key}