create table or collection from object list

This commit is contained in:
Jan Prochazka 2021-12-09 10:47:52 +01:00
parent bd92e86216
commit 83544170f3
6 changed files with 84 additions and 5 deletions

View File

@ -115,7 +115,7 @@ registerCommand({
registerCommand({ registerCommand({
id: 'new.table', id: 'new.table',
category: 'New', category: 'New',
icon: 'img table', icon: 'icon table',
name: 'Table', name: 'Table',
toolbar: true, toolbar: true,
toolbarName: 'New table', toolbarName: 'New table',
@ -147,6 +147,41 @@ registerCommand({
}, },
}); });
registerCommand({
id: 'new.collection',
category: 'New',
icon: 'icon table',
name: 'Collection',
toolbar: true,
toolbarName: 'New collection',
testEnabled: () => {
const driver = findEngineDriver(get(currentDatabase)?.connection, getExtensions());
return !!get(currentDatabase) && driver?.dialect?.nosql;
},
onClick: async () => {
const $currentDatabase = get(currentDatabase);
const connection = _.get($currentDatabase, 'connection') || {};
const database = _.get($currentDatabase, 'name');
const dbid = { conid: connection._id, database };
showModal(InputTextModal, {
value: '',
label: 'New collection name',
header: 'Create collection',
onConfirm: async newCollection => {
await axiosInstance.request({
url: 'database-connections/run-script',
method: 'post',
params: dbid,
data: { sql: `db.createCollection('${newCollection}')` },
});
axiosInstance.post('database-connections/sync-model', dbid);
},
});
},
});
registerCommand({ registerCommand({
id: 'new.markdown', id: 'new.markdown',
category: 'New', category: 'New',

View File

@ -0,0 +1,13 @@
<script lang="ts">
import FontIcon from '../icons/FontIcon.svelte';
import InlineButton from './InlineButton.svelte';
export let filter;
</script>
{#if filter}
<InlineButton on:click={() => (filter = '')} title="Clear filter">
<FontIcon icon="icon close" />
</InlineButton>
{/if}

View File

@ -2,6 +2,7 @@
export let disabled = false; export let disabled = false;
export let square = false; export let square = false;
export let narrow = false; export let narrow = false;
export let title = null;
let domButton; let domButton;
@ -10,7 +11,7 @@
} }
</script> </script>
<div class="outer buttonLike" class:disabled class:square class:narrow on:click bind:this={domButton}> <div class="outer buttonLike" {title} class:disabled class:square class:narrow on:click bind:this={domButton}>
<div class="inner"> <div class="inner">
<slot /> <slot />
</div> </div>

View File

@ -38,6 +38,7 @@
'icon filter': 'mdi mdi-filter', 'icon filter': 'mdi mdi-filter',
'icon filter-off': 'mdi mdi-filter-off', 'icon filter-off': 'mdi mdi-filter-off',
'icon reload': 'mdi mdi-reload', 'icon reload': 'mdi mdi-reload',
'icon refresh': 'mdi mdi-refresh',
'icon undo': 'mdi mdi-undo', 'icon undo': 'mdi mdi-undo',
'icon redo': 'mdi mdi-redo', 'icon redo': 'mdi mdi-redo',
'icon save': 'mdi mdi-content-save', 'icon save': 'mdi mdi-content-save',
@ -78,6 +79,7 @@
'icon checkbox-marked': 'mdi mdi-checkbox-marked-outline', 'icon checkbox-marked': 'mdi mdi-checkbox-marked-outline',
'icon dots-horizontal': 'mdi mdi-dots-horizontal', 'icon dots-horizontal': 'mdi mdi-dots-horizontal',
'icon dots-vertical': 'mdi mdi-dots-vertical', 'icon dots-vertical': 'mdi mdi-dots-vertical',
'icon add': 'mdi mdi-plus-circle',
'icon run': 'mdi mdi-play', 'icon run': 'mdi mdi-play',
'icon chevron-down': 'mdi mdi-chevron-down', 'icon chevron-down': 'mdi mdi-chevron-down',

View File

@ -14,6 +14,8 @@
import runCommand from '../commands/runCommand'; import runCommand from '../commands/runCommand';
import getConnectionLabel from '../utility/getConnectionLabel'; import getConnectionLabel from '../utility/getConnectionLabel';
import { useConnectionColorFactory } from '../utility/useConnectionColor'; import { useConnectionColorFactory } from '../utility/useConnectionColor';
import FontIcon from '../icons/FontIcon.svelte';
import CloseSearchButton from '../elements/CloseSearchButton.svelte';
const connections = useConnectionList(); const connections = useConnectionList();
const serverStatus = useServerStatus(); const serverStatus = useServerStatus();
@ -36,7 +38,13 @@
<SearchBoxWrapper> <SearchBoxWrapper>
<SearchInput placeholder="Search connection or database" bind:value={filter} /> <SearchInput placeholder="Search connection or database" bind:value={filter} />
<InlineButton on:click={handleRefreshConnections}>Refresh</InlineButton> <CloseSearchButton bind:filter />
<InlineButton on:click={() => runCommand('new.connection')} title="Add new connection">
<FontIcon icon="img add" />
</InlineButton>
<InlineButton on:click={handleRefreshConnections} title="Refresh connection list">
<FontIcon icon="icon refresh" />
</InlineButton>
</SearchBoxWrapper> </SearchBoxWrapper>
<WidgetsInnerContainer> <WidgetsInnerContainer>
<AppObjectList <AppObjectList

View File

@ -16,7 +16,7 @@
import InlineButton from '../elements/InlineButton.svelte'; import InlineButton from '../elements/InlineButton.svelte';
import SearchInput from '../elements/SearchInput.svelte'; import SearchInput from '../elements/SearchInput.svelte';
import WidgetsInnerContainer from './WidgetsInnerContainer.svelte'; import WidgetsInnerContainer from './WidgetsInnerContainer.svelte';
import { useDatabaseInfo, useDatabaseStatus } from '../utility/metadataLoaders'; import { useConnectionInfo, useDatabaseInfo, useDatabaseStatus } from '../utility/metadataLoaders';
import SearchBoxWrapper from '../elements/SearchBoxWrapper.svelte'; import SearchBoxWrapper from '../elements/SearchBoxWrapper.svelte';
import AppObjectList from '../appobj/AppObjectList.svelte'; import AppObjectList from '../appobj/AppObjectList.svelte';
import _ from 'lodash'; import _ from 'lodash';
@ -27,6 +27,11 @@
import axiosInstance from '../utility/axiosInstance'; import axiosInstance from '../utility/axiosInstance';
import LoadingInfo from '../elements/LoadingInfo.svelte'; import LoadingInfo from '../elements/LoadingInfo.svelte';
import { getObjectTypeFieldLabel } from '../utility/common'; import { getObjectTypeFieldLabel } from '../utility/common';
import DropDownButton from '../elements/DropDownButton.svelte';
import FontIcon from '../icons/FontIcon.svelte';
import CloseSearchButton from '../elements/CloseSearchButton.svelte';
import { findEngineDriver } from 'dbgate-tools';
import { extensions } from '../stores';
export let conid; export let conid;
export let database; export let database;
@ -36,6 +41,9 @@
$: objects = useDatabaseInfo({ conid, database }); $: objects = useDatabaseInfo({ conid, database });
$: status = useDatabaseStatus({ conid, database }); $: status = useDatabaseStatus({ conid, database });
$: connection = useConnectionInfo({ conid });
$: driver = findEngineDriver($connection, $extensions);
// $: console.log('OBJECTS', $objects); // $: console.log('OBJECTS', $objects);
$: objectList = _.flatten( $: objectList = _.flatten(
@ -54,6 +62,14 @@
const handleRefreshDatabase = () => { const handleRefreshDatabase = () => {
axiosInstance.post('database-connections/refresh', { conid, database }); axiosInstance.post('database-connections/refresh', { conid, database });
}; };
function createAddMenu() {
if (driver?.dialect?.nosql) {
return [{ label: 'New collection', command: 'new.collection' }];
} else {
return [{ label: 'New table', command: 'new.table' }];
}
}
</script> </script>
{#if $status && $status.name == 'error'} {#if $status && $status.name == 'error'}
@ -72,7 +88,11 @@
{:else} {:else}
<SearchBoxWrapper> <SearchBoxWrapper>
<SearchInput placeholder="Search tables or objects" bind:value={filter} /> <SearchInput placeholder="Search tables or objects" bind:value={filter} />
<InlineButton on:click={handleRefreshDatabase}>Refresh</InlineButton> <CloseSearchButton bind:filter />
<DropDownButton icon="img add" menu={createAddMenu} />
<InlineButton on:click={handleRefreshDatabase} title="Refresh database connection and object list">
<FontIcon icon="icon refresh" />
</InlineButton>
</SearchBoxWrapper> </SearchBoxWrapper>
<WidgetsInnerContainer> <WidgetsInnerContainer>
{#if ($status && ($status.name == 'pending' || $status.name == 'checkStructure' || $status.name == 'loadStructure') && $objects) || !$objects} {#if ($status && ($status.name == 'pending' || $status.name == 'checkStructure' || $status.name == 'loadStructure') && $objects) || !$objects}