mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 10:46:00 +00:00
pk editor
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -12,3 +12,4 @@ export * from './structureTools';
|
||||
export * from './settingsExtractors';
|
||||
export * from './filterName';
|
||||
export * from './diffTools';
|
||||
export * from './schemaEditorTools';
|
||||
|
||||
111
packages/tools/src/schemaEditorTools.ts
Normal file
111
packages/tools/src/schemaEditorTools.ts
Normal file
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user