mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 17:16:01 +00:00
create table - changed workflow
This commit is contained in:
@@ -56,27 +56,7 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleNewTable = () => {
|
const handleNewTable = () => {
|
||||||
const tooltip = `${getConnectionLabel(connection)}\n${name}`;
|
newTable(connection, name);
|
||||||
openNewTab(
|
|
||||||
{
|
|
||||||
title: 'Table #',
|
|
||||||
tooltip,
|
|
||||||
icon: 'img table-structure',
|
|
||||||
tabComponent: 'TableStructureTab',
|
|
||||||
props: {
|
|
||||||
conid: connection._id,
|
|
||||||
database: name,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
editor: {
|
|
||||||
columns: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
forceNewTab: true,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDropDatabase = () => {
|
const handleDropDatabase = () => {
|
||||||
@@ -410,6 +390,7 @@
|
|||||||
import NewCollectionModal from '../modals/NewCollectionModal.svelte';
|
import NewCollectionModal from '../modals/NewCollectionModal.svelte';
|
||||||
import hasPermission from '../utility/hasPermission';
|
import hasPermission from '../utility/hasPermission';
|
||||||
import { openImportExportTab } from '../utility/importExportTools';
|
import { openImportExportTab } from '../utility/importExportTools';
|
||||||
|
import newTable from '../tableeditor/newTable';
|
||||||
|
|
||||||
export let data;
|
export let data;
|
||||||
export let passProps;
|
export let passProps;
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ import NewCollectionModal from '../modals/NewCollectionModal.svelte';
|
|||||||
import ConfirmModal from '../modals/ConfirmModal.svelte';
|
import ConfirmModal from '../modals/ConfirmModal.svelte';
|
||||||
import localforage from 'localforage';
|
import localforage from 'localforage';
|
||||||
import { openImportExportTab } from '../utility/importExportTools';
|
import { openImportExportTab } from '../utility/importExportTools';
|
||||||
|
import newTable from '../tableeditor/newTable';
|
||||||
|
|
||||||
// function themeCommand(theme: ThemeDefinition) {
|
// function themeCommand(theme: ThemeDefinition) {
|
||||||
// return {
|
// return {
|
||||||
@@ -253,26 +254,7 @@ registerCommand({
|
|||||||
const $currentDatabase = get(currentDatabase);
|
const $currentDatabase = get(currentDatabase);
|
||||||
const connection = _.get($currentDatabase, 'connection') || {};
|
const connection = _.get($currentDatabase, 'connection') || {};
|
||||||
const database = _.get($currentDatabase, 'name');
|
const database = _.get($currentDatabase, 'name');
|
||||||
|
newTable(connection, database);
|
||||||
openNewTab(
|
|
||||||
{
|
|
||||||
title: 'Table #',
|
|
||||||
icon: 'img table-structure',
|
|
||||||
tabComponent: 'TableStructureTab',
|
|
||||||
props: {
|
|
||||||
conid: connection._id,
|
|
||||||
database,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
editor: {
|
|
||||||
columns: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
forceNewTab: true,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -5,11 +5,16 @@
|
|||||||
import { writable } from 'svelte/store';
|
import { writable } from 'svelte/store';
|
||||||
import FormProviderCore from '../forms/FormProviderCore.svelte';
|
import FormProviderCore from '../forms/FormProviderCore.svelte';
|
||||||
import createRef from '../utility/createRef';
|
import createRef from '../utility/createRef';
|
||||||
|
import FormSchemaSelect from '../impexp/FormSchemaSelect.svelte';
|
||||||
|
import FormTextField from '../forms/FormTextField.svelte';
|
||||||
|
import FormSelectField from '../forms/FormSelectField.svelte';
|
||||||
|
|
||||||
export let title;
|
export let title;
|
||||||
export let fieldDefinitions;
|
export let fieldDefinitions;
|
||||||
export let values;
|
export let values;
|
||||||
export let onChangeValues;
|
export let onChangeValues;
|
||||||
|
export let pureNameTitle = null;
|
||||||
|
export let schemaList = null;
|
||||||
|
|
||||||
let collapsed = false;
|
let collapsed = false;
|
||||||
|
|
||||||
@@ -32,6 +37,16 @@
|
|||||||
</div>
|
</div>
|
||||||
{#if !collapsed}
|
{#if !collapsed}
|
||||||
<FormProviderCore values={valuesStore}>
|
<FormProviderCore values={valuesStore}>
|
||||||
|
{#if schemaList?.length > 0}
|
||||||
|
<FormSelectField
|
||||||
|
name="schemaName"
|
||||||
|
label="Schema"
|
||||||
|
options={schemaList.map(x => ({ label: x.schemaName, value: x.schemaName }))}
|
||||||
|
/>
|
||||||
|
{/if}
|
||||||
|
{#if pureNameTitle}
|
||||||
|
<FormTextField name="pureName" label={pureNameTitle} />
|
||||||
|
{/if}
|
||||||
<FormArgumentList args={fieldDefinitions} />
|
<FormArgumentList args={fieldDefinitions} />
|
||||||
</FormProviderCore>
|
</FormProviderCore>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
@@ -91,6 +91,7 @@
|
|||||||
export let dbInfo;
|
export let dbInfo;
|
||||||
export let driver;
|
export let driver;
|
||||||
export let resetCounter;
|
export let resetCounter;
|
||||||
|
export let isCreateTable;
|
||||||
|
|
||||||
$: isWritable = !!setTableInfo;
|
$: isWritable = !!setTableInfo;
|
||||||
|
|
||||||
@@ -165,15 +166,14 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="wrapper">
|
<div class="wrapper">
|
||||||
{#if tableFormOptions}
|
{#if tableInfo && (tableFormOptions || isCreateTable)}
|
||||||
{#key resetCounter}
|
{#key resetCounter}
|
||||||
<ObjectFieldsEditor
|
<ObjectFieldsEditor
|
||||||
title="Table properties"
|
title="Table properties"
|
||||||
fieldDefinitions={tableFormOptions}
|
fieldDefinitions={tableFormOptions ?? []}
|
||||||
values={_.pick(
|
pureNameTitle="Table name"
|
||||||
tableInfo,
|
schemaList={dbInfo?.schemas?.length >= 0 ? dbInfo?.schemas : null}
|
||||||
tableFormOptions.map(x => x.name)
|
values={_.pick(tableInfo, ['schemaName', 'pureName', ...(tableFormOptions ?? []).map(x => x.name)])}
|
||||||
)}
|
|
||||||
onChangeValues={vals => {
|
onChangeValues={vals => {
|
||||||
if (!_.isEmpty(vals)) {
|
if (!_.isEmpty(vals)) {
|
||||||
setTableInfo(tbl => ({ ...tbl, ...vals }));
|
setTableInfo(tbl => ({ ...tbl, ...vals }));
|
||||||
|
|||||||
48
packages/web/src/tableeditor/newTable.ts
Normal file
48
packages/web/src/tableeditor/newTable.ts
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
import _ from 'lodash';
|
||||||
|
import openNewTab from '../utility/openNewTab';
|
||||||
|
import { findEngineDriver, getConnectionLabel } from 'dbgate-tools';
|
||||||
|
import { getExtensions } from '../stores';
|
||||||
|
|
||||||
|
export default function newTable(connection, database) {
|
||||||
|
const tooltip = `${getConnectionLabel(connection)}\n${database}`;
|
||||||
|
const driver = findEngineDriver(connection, getExtensions());
|
||||||
|
openNewTab(
|
||||||
|
{
|
||||||
|
title: 'Table #',
|
||||||
|
tooltip,
|
||||||
|
icon: 'img table-structure',
|
||||||
|
tabComponent: 'TableStructureTab',
|
||||||
|
props: {
|
||||||
|
conid: connection._id,
|
||||||
|
database,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
editor: {
|
||||||
|
current: {
|
||||||
|
pureName: 'new_table',
|
||||||
|
schemaName: driver?.dialect?.defaultSchemaName,
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
columnName: 'id',
|
||||||
|
dataType: 'int',
|
||||||
|
isNullable: false,
|
||||||
|
isPrimaryKey: true,
|
||||||
|
isAutoIncrement: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
primaryKey: {
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
columnName: 'id',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
forceNewTab: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
toolbar: true,
|
toolbar: true,
|
||||||
isRelatedToTab: true,
|
isRelatedToTab: true,
|
||||||
icon: 'icon close',
|
icon: 'icon close',
|
||||||
testEnabled: () => getCurrentEditor()?.canSave(),
|
testEnabled: () => getCurrentEditor()?.canResetChanges(),
|
||||||
onClick: () => getCurrentEditor().reset(),
|
onClick: () => getCurrentEditor().reset(),
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
@@ -90,30 +90,11 @@
|
|||||||
return objectTypeField == 'tables' && !!$editorValue && !$connection?.isReadOnly;
|
return objectTypeField == 'tables' && !!$editorValue && !$connection?.isReadOnly;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function save() {
|
export function canResetChanges() {
|
||||||
if ($editorValue.base) {
|
return canSave() && !!$editorValue.base;
|
||||||
doSave(null);
|
|
||||||
} else {
|
|
||||||
showModal(InputTextModal, {
|
|
||||||
header: 'Set table name',
|
|
||||||
value: savedName || 'newTable',
|
|
||||||
label: 'Table name',
|
|
||||||
onConfirm: name => {
|
|
||||||
savedName = name;
|
|
||||||
setEditorData(tbl => ({
|
|
||||||
base: tbl.base,
|
|
||||||
current: {
|
|
||||||
...tbl.current,
|
|
||||||
pureName: name,
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
doSave(name);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function doSave(createTableName) {
|
export function save() {
|
||||||
const { sql, recreates } = getAlterTableScript(
|
const { sql, recreates } = getAlterTableScript(
|
||||||
$editorValue.base,
|
$editorValue.base,
|
||||||
extendTableInfo(fillConstraintNames($editorValue.current, driver.dialect)),
|
extendTableInfo(fillConstraintNames($editorValue.current, driver.dialect)),
|
||||||
@@ -127,29 +108,18 @@
|
|||||||
sql,
|
sql,
|
||||||
recreates,
|
recreates,
|
||||||
onConfirm: () => {
|
onConfirm: () => {
|
||||||
handleConfirmSql(sql, createTableName);
|
handleConfirmSql(sql);
|
||||||
},
|
},
|
||||||
engine: driver.engine,
|
engine: driver.engine,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleConfirmSql(sql, createTableName) {
|
async function handleConfirmSql(sql) {
|
||||||
const resp = await apiCall('database-connections/run-script', { conid, database, sql, useTransaction: true });
|
const resp = await apiCall('database-connections/run-script', { conid, database, sql, useTransaction: true });
|
||||||
const { errorMessage } = resp || {};
|
const { errorMessage } = resp || {};
|
||||||
if (errorMessage) {
|
if (errorMessage) {
|
||||||
showModal(ErrorMessageModal, { title: 'Error when saving', message: errorMessage });
|
showModal(ErrorMessageModal, { title: 'Error when saving', message: errorMessage });
|
||||||
} else {
|
} else {
|
||||||
if (createTableName) {
|
|
||||||
changeTab(tabid, tab => ({
|
|
||||||
...tab,
|
|
||||||
title: createTableName,
|
|
||||||
props: {
|
|
||||||
...tab.props,
|
|
||||||
pureName: createTableName,
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
await apiCall('database-connections/sync-model', { conid, database });
|
await apiCall('database-connections/sync-model', { conid, database });
|
||||||
showSnackbarSuccess('Saved to database');
|
showSnackbarSuccess('Saved to database');
|
||||||
clearEditorData();
|
clearEditorData();
|
||||||
@@ -175,6 +145,7 @@
|
|||||||
dbInfo={$dbInfo}
|
dbInfo={$dbInfo}
|
||||||
{driver}
|
{driver}
|
||||||
{resetCounter}
|
{resetCounter}
|
||||||
|
isCreateTable={objectTypeField == 'tables' && !$editorValue?.base}
|
||||||
setTableInfo={objectTypeField == 'tables' && !$connection?.isReadOnly && hasPermission(`dbops/model/edit`)
|
setTableInfo={objectTypeField == 'tables' && !$connection?.isReadOnly && hasPermission(`dbops/model/edit`)
|
||||||
? tableInfoUpdater =>
|
? tableInfoUpdater =>
|
||||||
setEditorData(tbl =>
|
setEditorData(tbl =>
|
||||||
@@ -191,7 +162,10 @@
|
|||||||
: null}
|
: null}
|
||||||
/>
|
/>
|
||||||
<svelte:fragment slot="toolstrip">
|
<svelte:fragment slot="toolstrip">
|
||||||
<ToolStripCommandButton command="tableStructure.save" />
|
<ToolStripCommandButton
|
||||||
|
command="tableStructure.save"
|
||||||
|
buttonLabel={$editorValue?.base ? 'Alter table' : 'Create table'}
|
||||||
|
/>
|
||||||
<ToolStripCommandButton command="tableStructure.reset" />
|
<ToolStripCommandButton command="tableStructure.reset" />
|
||||||
<ToolStripCommandButton command="tableEditor.addColumn" />
|
<ToolStripCommandButton command="tableEditor.addColumn" />
|
||||||
<ToolStripCommandButton command="tableEditor.addIndex" hideDisabled />
|
<ToolStripCommandButton command="tableEditor.addIndex" hideDisabled />
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ function findFreeNumber(numbers: number[]) {
|
|||||||
// return res;
|
// return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function openNewTab(newTab, initialData = undefined, options = undefined) {
|
export default async function openNewTab(newTab, initialData: any = undefined, options: any = undefined) {
|
||||||
const oldTabs = getOpenedTabs();
|
const oldTabs = getOpenedTabs();
|
||||||
const activeTab = getActiveTab();
|
const activeTab = getActiveTab();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user