From dea6700a254c6f2c362020029343948fa72e31db Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Thu, 17 Jun 2021 13:58:21 +0200 Subject: [PATCH] pk editor --- packages/tools/src/diffTools.ts | 67 -------- packages/tools/src/index.ts | 1 + packages/tools/src/schemaEditorTools.ts | 111 +++++++++++++ .../ColumnsConstraintEditorModal.svelte | 157 ++++++++++++++++++ .../tableeditor/PrimaryKeyEditorModal.svelte | 17 ++ .../web/src/tableeditor/TableEditor.svelte | 4 + 6 files changed, 290 insertions(+), 67 deletions(-) create mode 100644 packages/tools/src/schemaEditorTools.ts create mode 100644 packages/web/src/tableeditor/ColumnsConstraintEditorModal.svelte create mode 100644 packages/web/src/tableeditor/PrimaryKeyEditorModal.svelte diff --git a/packages/tools/src/diffTools.ts b/packages/tools/src/diffTools.ts index f89daaf3..c49b7773 100644 --- a/packages/tools/src/diffTools.ts +++ b/packages/tools/src/diffTools.ts @@ -15,70 +15,3 @@ export function generateTablePairingId(table: TableInfo): TableInfo { } return table; } - -function processPrimaryKey(table: TableInfo, oldColumn: ColumnInfo, newColumn: ColumnInfo) { - if (!oldColumn?.isPrimaryKey && newColumn?.isPrimaryKey) { - if (!table.primaryKey) { - table.primaryKey = { - constraintType: 'primaryKey', - pureName: table.pureName, - schemaName: table.schemaName, - columns: [], - }; - } - table.primaryKey.columns = [ - ...table.primaryKey.columns, - { - columnName: newColumn.columnName, - }, - ]; - } - - console.log('processPrimaryKey', oldColumn, newColumn); - - if (oldColumn?.isPrimaryKey && !newColumn?.isPrimaryKey) { - if (table.primaryKey) { - table.primaryKey = { - ...table.primaryKey, - columns: table.primaryKey.columns.filter(x => x.columnName != oldColumn.columnName), - }; - if (table.primaryKey.columns.length == 0) { - table.primaryKey = null; - } - } - } -} - -export function editorAddColumn(table: TableInfo, column: ColumnInfo): TableInfo { - const res = { - ...table, - columns: [...table.columns, { ...column, pairingId: uuidv1() }], - }; - - processPrimaryKey(res, null, column); - - return res; -} - -export function editorModifyColumn(table: TableInfo, column: ColumnInfo): TableInfo { - const oldColumn = table?.columns?.find(x => x.pairingId == column.pairingId); - - const res = { - ...table, - columns: table.columns.map(col => (col.pairingId == column.pairingId ? column : col)), - }; - processPrimaryKey(res, oldColumn, column); - - return res; -} - -export function editorDeleteColumn(table: TableInfo, column: ColumnInfo): TableInfo { - const res = { - ...table, - columns: table.columns.filter(col => col.pairingId != column.pairingId), - }; - - processPrimaryKey(res, column, null); - - return res; -} diff --git a/packages/tools/src/index.ts b/packages/tools/src/index.ts index 515df9fc..5485b923 100644 --- a/packages/tools/src/index.ts +++ b/packages/tools/src/index.ts @@ -12,3 +12,4 @@ export * from './structureTools'; export * from './settingsExtractors'; export * from './filterName'; export * from './diffTools'; +export * from './schemaEditorTools'; diff --git a/packages/tools/src/schemaEditorTools.ts b/packages/tools/src/schemaEditorTools.ts new file mode 100644 index 00000000..79efaa86 --- /dev/null +++ b/packages/tools/src/schemaEditorTools.ts @@ -0,0 +1,111 @@ +import uuidv1 from 'uuid/v1'; +import { ColumnInfo, ConstraintInfo, PrimaryKeyInfo, TableInfo } from 'dbgate-types'; + +function processPrimaryKey(table: TableInfo, oldColumn: ColumnInfo, newColumn: ColumnInfo) { + if (!oldColumn?.isPrimaryKey && newColumn?.isPrimaryKey) { + if (!table.primaryKey) { + table.primaryKey = { + constraintType: 'primaryKey', + pureName: table.pureName, + schemaName: table.schemaName, + columns: [], + }; + } + table.primaryKey.columns = [ + ...table.primaryKey.columns, + { + columnName: newColumn.columnName, + }, + ]; + } + + console.log('processPrimaryKey', oldColumn, newColumn); + + if (oldColumn?.isPrimaryKey && !newColumn?.isPrimaryKey) { + if (table.primaryKey) { + table.primaryKey = { + ...table.primaryKey, + columns: table.primaryKey.columns.filter(x => x.columnName != oldColumn.columnName), + }; + if (table.primaryKey.columns.length == 0) { + table.primaryKey = null; + } + } + } +} + +export function editorAddColumn(table: TableInfo, column: ColumnInfo): TableInfo { + const res = { + ...table, + columns: [...table.columns, { ...column, pairingId: uuidv1() }], + }; + + processPrimaryKey(res, null, column); + + return res; +} + +export function editorModifyColumn(table: TableInfo, column: ColumnInfo): TableInfo { + const oldColumn = table?.columns?.find(x => x.pairingId == column.pairingId); + + const res = { + ...table, + columns: table.columns.map(col => (col.pairingId == column.pairingId ? column : col)), + }; + processPrimaryKey(res, oldColumn, column); + + return res; +} + +export function editorDeleteColumn(table: TableInfo, column: ColumnInfo): TableInfo { + const res = { + ...table, + columns: table.columns.filter(col => col.pairingId != column.pairingId), + }; + + processPrimaryKey(res, column, null); + + return res; +} + +export function editorAddConstraint(table: TableInfo, constraint: ConstraintInfo): TableInfo { + const res = { + ...table, + }; + + if (constraint.constraintType == 'primaryKey') { + res.primaryKey = { + pairingId: uuidv1(), + ...constraint, + } as PrimaryKeyInfo; + } + + return res; +} + +export function editorModifyConstraint(table: TableInfo, constraint: ConstraintInfo): TableInfo { + const res = { + ...table, + }; + + if (constraint.constraintType == 'primaryKey') { + res.primaryKey = { + ...res.primaryKey, + ...constraint, + }; + } + + return res; +} + +export function editorDeleteConstraint(table: TableInfo, constraint: ConstraintInfo): TableInfo { + const res = { + ...table, + }; + + if (constraint.constraintType == 'primaryKey') { + res.primaryKey = null; + } + + return res; +} diff --git a/packages/web/src/tableeditor/ColumnsConstraintEditorModal.svelte b/packages/web/src/tableeditor/ColumnsConstraintEditorModal.svelte new file mode 100644 index 00000000..58bc385a --- /dev/null +++ b/packages/web/src/tableeditor/ColumnsConstraintEditorModal.svelte @@ -0,0 +1,157 @@ + + + + {constraintInfo ? `Edit ${constraintLabel}` : `Add ${constraintLabel}`} + +
+
+
Constraint name
+
+ (constraintName = e.target['value'])} focused /> +
+
+ + {#each columns as column, index} +
+
Column {index + 1}
+
+ ({ + label: col.columnName, + value: col.columnName, + }))} + /> +
+
+ { + const x = [...columns]; + x.splice(index, 1); + columns = x; + }} + /> +
+
+ {/each} + +
+
Add new column
+
+ {#key columns.length} + { + if (e.detail) + columns = [ + ...columns, + { + columnName: e.detail, + }, + ]; + }} + isNative + options={[ + { + label: 'Choose column', + value: '', + }, + ...tableInfo.columns.map(col => ({ + label: col.columnName, + value: col.columnName, + })), + ]} + /> + {/key} +
+
+
+ + + { + closeCurrentModal(); + if (constraintInfo) { + setTableInfo(tbl => editorModifyConstraint(tbl, getConstraint())); + } else { + setTableInfo(tbl => editorAddConstraint(tbl, getConstraint())); + } + }} + /> + + + {#if constraintInfo} + { + closeCurrentModal(); + setTableInfo(tbl => editorDeleteConstraint(tbl, constraintInfo)); + }} + /> + {/if} + +
+ + diff --git a/packages/web/src/tableeditor/PrimaryKeyEditorModal.svelte b/packages/web/src/tableeditor/PrimaryKeyEditorModal.svelte new file mode 100644 index 00000000..fc86db83 --- /dev/null +++ b/packages/web/src/tableeditor/PrimaryKeyEditorModal.svelte @@ -0,0 +1,17 @@ + + + diff --git a/packages/web/src/tableeditor/TableEditor.svelte b/packages/web/src/tableeditor/TableEditor.svelte index fc3da5f8..643d6a0d 100644 --- a/packages/web/src/tableeditor/TableEditor.svelte +++ b/packages/web/src/tableeditor/TableEditor.svelte @@ -30,6 +30,7 @@ import { useDbCore } from '../utility/metadataLoaders'; import ColumnEditorModal from './ColumnEditorModal.svelte'; +import PrimaryKeyEditorModal from './PrimaryKeyEditorModal.svelte'; export const activator = createActivator('TableEditor', true); @@ -111,6 +112,9 @@ showModal(PrimaryKeyEditorModal, { constraintInfo: e.detail, tableInfo, setTableInfo })} + columns={[ { fieldName: 'columns',