mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-02 06:43:59 +00:00
table editor
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import uuidv1 from 'uuid/v1';
|
import uuidv1 from 'uuid/v1';
|
||||||
|
import _omit from 'lodash/omit';
|
||||||
import { ColumnInfo, ConstraintInfo, PrimaryKeyInfo, TableInfo } from 'dbgate-types';
|
import { ColumnInfo, ConstraintInfo, PrimaryKeyInfo, TableInfo } from 'dbgate-types';
|
||||||
|
|
||||||
export interface EditorColumnInfo extends ColumnInfo {
|
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) {
|
if (!oldColumn?.isPrimaryKey && newColumn?.isPrimaryKey) {
|
||||||
// let primaryKey = table.primaryKey
|
let primaryKey = table.primaryKey;
|
||||||
// if (!primaryKey) {
|
if (!primaryKey) {
|
||||||
// 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',
|
constraintType: 'primaryKey',
|
||||||
pureName: table.pureName,
|
pureName: table.pureName,
|
||||||
schemaName: table.schemaName,
|
schemaName: table.schemaName,
|
||||||
columns: [],
|
columns: [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
table.primaryKey.columns = [
|
return {
|
||||||
...table.primaryKey.columns,
|
...table,
|
||||||
|
primaryKey: {
|
||||||
|
...primaryKey,
|
||||||
|
columns: [
|
||||||
|
...primaryKey.columns,
|
||||||
{
|
{
|
||||||
columnName: newColumn.columnName,
|
columnName: newColumn.columnName,
|
||||||
},
|
},
|
||||||
];
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('processPrimaryKey', oldColumn, newColumn);
|
|
||||||
|
|
||||||
if (oldColumn?.isPrimaryKey && !newColumn?.isPrimaryKey) {
|
if (oldColumn?.isPrimaryKey && !newColumn?.isPrimaryKey) {
|
||||||
if (table.primaryKey) {
|
let primaryKey = table.primaryKey;
|
||||||
table.primaryKey = {
|
if (primaryKey) {
|
||||||
...table.primaryKey,
|
primaryKey = {
|
||||||
|
...primaryKey,
|
||||||
columns: table.primaryKey.columns.filter(x => x.columnName != oldColumn.columnName),
|
columns: table.primaryKey.columns.filter(x => x.columnName != oldColumn.columnName),
|
||||||
};
|
};
|
||||||
if (table.primaryKey.columns.length == 0) {
|
if (primaryKey.columns.length == 0) {
|
||||||
table.primaryKey = null;
|
return {
|
||||||
}
|
...table,
|
||||||
|
primaryKey: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
...table,
|
||||||
|
primaryKey,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function editorAddColumn(table: TableInfo, column: EditorColumnInfo): TableInfo {
|
export function editorAddColumn(table: TableInfo, column: EditorColumnInfo): TableInfo {
|
||||||
const res = {
|
let res = {
|
||||||
...table,
|
...table,
|
||||||
columns: [...table.columns, { ...column, pairingId: uuidv1() }],
|
columns: [...table.columns, { ...column, pairingId: uuidv1() }],
|
||||||
};
|
};
|
||||||
|
|
||||||
processPrimaryKey(res, null, column);
|
res = processPrimaryKey(res, null, column);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -81,22 +75,22 @@ export function editorAddColumn(table: TableInfo, column: EditorColumnInfo): Tab
|
|||||||
export function editorModifyColumn(table: TableInfo, column: EditorColumnInfo): TableInfo {
|
export function editorModifyColumn(table: TableInfo, column: EditorColumnInfo): TableInfo {
|
||||||
const oldColumn = table?.columns?.find(x => x.pairingId == column.pairingId);
|
const oldColumn = table?.columns?.find(x => x.pairingId == column.pairingId);
|
||||||
|
|
||||||
const res = {
|
let res = {
|
||||||
...table,
|
...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;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function editorDeleteColumn(table: TableInfo, column: EditorColumnInfo): TableInfo {
|
export function editorDeleteColumn(table: TableInfo, column: EditorColumnInfo): TableInfo {
|
||||||
const res = {
|
let res = {
|
||||||
...table,
|
...table,
|
||||||
columns: table.columns.filter(col => col.pairingId != column.pairingId),
|
columns: table.columns.filter(col => col.pairingId != column.pairingId),
|
||||||
};
|
};
|
||||||
|
|
||||||
processPrimaryKey(res, column, null);
|
res = processPrimaryKey(res, column, null);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user