From 6f22932b16556e3c58ff4c2ea5337a298ebab878 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Thu, 17 Jun 2021 14:37:59 +0200 Subject: [PATCH] table editor --- packages/tools/src/schemaEditorTools.ts | 82 ++++++++++++------------- 1 file changed, 38 insertions(+), 44 deletions(-) diff --git a/packages/tools/src/schemaEditorTools.ts b/packages/tools/src/schemaEditorTools.ts index 4ea63bbe..51d02063 100644 --- a/packages/tools/src/schemaEditorTools.ts +++ b/packages/tools/src/schemaEditorTools.ts @@ -1,4 +1,5 @@ import uuidv1 from 'uuid/v1'; +import _omit from 'lodash/omit'; import { ColumnInfo, ConstraintInfo, PrimaryKeyInfo, TableInfo } from 'dbgate-types'; export interface EditorColumnInfo extends ColumnInfo { @@ -12,68 +13,61 @@ export function fillEditorColumnInfo(column: ColumnInfo, table: TableInfo): Edit }; } -function processPrimaryKey(table: TableInfo, oldColumn: EditorColumnInfo, newColumn: EditorColumnInfo) { +function processPrimaryKey(table: TableInfo, oldColumn: EditorColumnInfo, newColumn: EditorColumnInfo): TableInfo { 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 = { + let primaryKey = table.primaryKey; + if (!primaryKey) { + primaryKey = { constraintType: 'primaryKey', pureName: table.pureName, schemaName: table.schemaName, columns: [], }; } - table.primaryKey.columns = [ - ...table.primaryKey.columns, - { - columnName: newColumn.columnName, + return { + ...table, + primaryKey: { + ...primaryKey, + columns: [ + ...primaryKey.columns, + { + columnName: newColumn.columnName, + }, + ], }, - ]; + }; } - console.log('processPrimaryKey', oldColumn, newColumn); - if (oldColumn?.isPrimaryKey && !newColumn?.isPrimaryKey) { - if (table.primaryKey) { - table.primaryKey = { - ...table.primaryKey, + let primaryKey = table.primaryKey; + if (primaryKey) { + primaryKey = { + ...primaryKey, columns: table.primaryKey.columns.filter(x => x.columnName != oldColumn.columnName), }; - if (table.primaryKey.columns.length == 0) { - table.primaryKey = null; + if (primaryKey.columns.length == 0) { + return { + ...table, + primaryKey: null, + }; } + return { + ...table, + primaryKey, + }; } } + + return table; } export function editorAddColumn(table: TableInfo, column: EditorColumnInfo): TableInfo { - const res = { + let res = { ...table, columns: [...table.columns, { ...column, pairingId: uuidv1() }], }; - processPrimaryKey(res, null, column); + res = processPrimaryKey(res, null, column); return res; } @@ -81,22 +75,22 @@ export function editorAddColumn(table: TableInfo, column: EditorColumnInfo): Tab export function editorModifyColumn(table: TableInfo, column: EditorColumnInfo): TableInfo { const oldColumn = table?.columns?.find(x => x.pairingId == column.pairingId); - const res = { + let res = { ...table, - columns: table.columns.map(col => (col.pairingId == column.pairingId ? column : col)), + columns: table.columns.map(col => (col.pairingId == column.pairingId ? _omit(column, ['isPrimaryKey']) : col)), }; - processPrimaryKey(res, oldColumn, column); + res = processPrimaryKey(res, fillEditorColumnInfo(oldColumn, table), column); return res; } export function editorDeleteColumn(table: TableInfo, column: EditorColumnInfo): TableInfo { - const res = { + let res = { ...table, columns: table.columns.filter(col => col.pairingId != column.pairingId), }; - processPrimaryKey(res, column, null); + res = processPrimaryKey(res, column, null); return res; }