mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
table editor
This commit is contained in:
parent
dea6700a25
commit
bf725dd563
@ -1,8 +1,41 @@
|
|||||||
import uuidv1 from 'uuid/v1';
|
import uuidv1 from 'uuid/v1';
|
||||||
import { ColumnInfo, ConstraintInfo, PrimaryKeyInfo, TableInfo } from 'dbgate-types';
|
import { ColumnInfo, ConstraintInfo, PrimaryKeyInfo, TableInfo } from 'dbgate-types';
|
||||||
|
|
||||||
function processPrimaryKey(table: TableInfo, oldColumn: ColumnInfo, newColumn: ColumnInfo) {
|
export interface EditorColumnInfo extends ColumnInfo {
|
||||||
|
isPrimaryKey?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function fillEditorColumnInfo(column: ColumnInfo, table: TableInfo): EditorColumnInfo {
|
||||||
|
return {
|
||||||
|
isPrimaryKey: !!(table.primaryKey && table.primaryKey.columns.find(x => x.columnName == column.columnName)),
|
||||||
|
...column,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function processPrimaryKey(table: TableInfo, oldColumn: EditorColumnInfo, newColumn: EditorColumnInfo) {
|
||||||
if (!oldColumn?.isPrimaryKey && newColumn?.isPrimaryKey) {
|
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) {
|
if (!table.primaryKey) {
|
||||||
table.primaryKey = {
|
table.primaryKey = {
|
||||||
constraintType: 'primaryKey',
|
constraintType: 'primaryKey',
|
||||||
@ -34,7 +67,7 @@ function processPrimaryKey(table: TableInfo, oldColumn: ColumnInfo, newColumn: C
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function editorAddColumn(table: TableInfo, column: ColumnInfo): TableInfo {
|
export function editorAddColumn(table: TableInfo, column: EditorColumnInfo): TableInfo {
|
||||||
const res = {
|
const res = {
|
||||||
...table,
|
...table,
|
||||||
columns: [...table.columns, { ...column, pairingId: uuidv1() }],
|
columns: [...table.columns, { ...column, pairingId: uuidv1() }],
|
||||||
@ -45,7 +78,7 @@ export function editorAddColumn(table: TableInfo, column: ColumnInfo): TableInfo
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function editorModifyColumn(table: TableInfo, column: ColumnInfo): 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 = {
|
const res = {
|
||||||
@ -57,7 +90,7 @@ export function editorModifyColumn(table: TableInfo, column: ColumnInfo): TableI
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function editorDeleteColumn(table: TableInfo, column: ColumnInfo): TableInfo {
|
export function editorDeleteColumn(table: TableInfo, column: EditorColumnInfo): TableInfo {
|
||||||
const res = {
|
const res = {
|
||||||
...table,
|
...table,
|
||||||
columns: table.columns.filter(col => col.pairingId != column.pairingId),
|
columns: table.columns.filter(col => col.pairingId != column.pairingId),
|
||||||
|
@ -21,7 +21,6 @@ function fillTableExtendedInfo(db: DatabaseInfo): DatabaseInfo {
|
|||||||
columns: (obj.columns || []).map(column => ({
|
columns: (obj.columns || []).map(column => ({
|
||||||
pureName: obj.pureName,
|
pureName: obj.pureName,
|
||||||
schemaName: obj.schemaName,
|
schemaName: obj.schemaName,
|
||||||
isPrimaryKey: !!(obj.primaryKey && obj.primaryKey.columns.find(x => x.columnName == column.columnName)),
|
|
||||||
...column,
|
...column,
|
||||||
})),
|
})),
|
||||||
primaryKey: obj.primaryKey
|
primaryKey: obj.primaryKey
|
||||||
|
1
packages/types/dbinfo.d.ts
vendored
1
packages/types/dbinfo.d.ts
vendored
@ -52,7 +52,6 @@ export interface ColumnInfo extends NamedObjectInfo {
|
|||||||
isSparse: boolean;
|
isSparse: boolean;
|
||||||
defaultValue: string;
|
defaultValue: string;
|
||||||
defaultConstraint: string;
|
defaultConstraint: string;
|
||||||
isPrimaryKey?: boolean; // only used in editor
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DatabaseObjectInfo extends NamedObjectInfo {
|
export interface DatabaseObjectInfo extends NamedObjectInfo {
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
import ElectronFilesInput from '../impexp/ElectronFilesInput.svelte';
|
import ElectronFilesInput from '../impexp/ElectronFilesInput.svelte';
|
||||||
import DropDownButton from '../elements/DropDownButton.svelte';
|
import DropDownButton from '../elements/DropDownButton.svelte';
|
||||||
import DataTypeEditor from './DataTypeEditor.svelte';
|
import DataTypeEditor from './DataTypeEditor.svelte';
|
||||||
import { editorAddColumn, editorDeleteColumn, editorModifyColumn } from 'dbgate-tools';
|
import { editorAddColumn, editorDeleteColumn, editorModifyColumn, fillEditorColumnInfo } from 'dbgate-tools';
|
||||||
|
|
||||||
export let columnInfo;
|
export let columnInfo;
|
||||||
export let setTableInfo;
|
export let setTableInfo;
|
||||||
@ -22,7 +22,7 @@
|
|||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<FormProvider initialValues={columnInfo}>
|
<FormProvider initialValues={fillEditorColumnInfo(columnInfo, tableInfo)}>
|
||||||
<ModalBase {...$$restProps}>
|
<ModalBase {...$$restProps}>
|
||||||
<svelte:fragment slot="header"
|
<svelte:fragment slot="header"
|
||||||
>{columnInfo ? 'Edit column' : `Add column ${(tableInfo?.columns || []).length + 1}`}</svelte:fragment
|
>{columnInfo ? 'Edit column' : `Add column ${(tableInfo?.columns || []).length + 1}`}</svelte:fragment
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
return {
|
return {
|
||||||
pairingId: uuidv1(),
|
pairingId: uuidv1(),
|
||||||
...constraintInfo,
|
...constraintInfo,
|
||||||
|
columns,
|
||||||
pureName: tableInfo.pureName,
|
pureName: tableInfo.pureName,
|
||||||
schemaName: tableInfo.schemaName,
|
schemaName: tableInfo.schemaName,
|
||||||
constraintName,
|
constraintName,
|
||||||
@ -56,14 +57,21 @@
|
|||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="label col-3">Column {index + 1}</div>
|
<div class="label col-3">Column {index + 1}</div>
|
||||||
<div class="col-6">
|
<div class="col-6">
|
||||||
<SelectField
|
{#key column.columnName}
|
||||||
value={column.columnName}
|
<SelectField
|
||||||
isNative
|
value={column.columnName}
|
||||||
options={tableInfo.columns.map(col => ({
|
isNative
|
||||||
label: col.columnName,
|
options={tableInfo.columns.map(col => ({
|
||||||
value: col.columnName,
|
label: col.columnName,
|
||||||
}))}
|
value: col.columnName,
|
||||||
/>
|
}))}
|
||||||
|
on:change={e => {
|
||||||
|
if (e.detail) {
|
||||||
|
columns = columns.map((col, i) => (i == index ? { columnName: e.detail } : col));
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
{/key}
|
||||||
</div>
|
</div>
|
||||||
<div class="col-3 button">
|
<div class="col-3 button">
|
||||||
<FormStyledButton
|
<FormStyledButton
|
||||||
|
Loading…
Reference in New Issue
Block a user