table editor

This commit is contained in:
Jan Prochazka 2021-06-17 14:37:59 +02:00
parent bf725dd563
commit 6f22932b16

View File

@ -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;
}