1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2024-12-01 13:20:54 +00:00

Create databases and collections

This commit is contained in:
Romein van Buren 2023-01-15 16:49:08 +01:00
parent de02a6fd8f
commit c9a035ed59
Signed by: romein
GPG Key ID: 0EFF8478ADDF6C49
4 changed files with 91 additions and 19 deletions

View File

@ -1,24 +1,38 @@
<script> <script>
import { onMount } from 'svelte'; import { onMount, tick } from 'svelte';
import { DropCollection, DropDatabase, Hosts, OpenCollection, OpenConnection, OpenDatabase } from '../wailsjs/go/app/App'; import { DropCollection, DropDatabase, Hosts, OpenCollection, OpenConnection, OpenDatabase } from '../wailsjs/go/app/App';
import AddressBar from './organisms/addressbar/index.svelte'; import AddressBar from './organisms/addressbar/index.svelte';
import Grid from './components/grid.svelte'; import Grid from './components/grid.svelte';
import CollectionDetail from './organisms/collection-detail/index.svelte'; import CollectionDetail from './organisms/collection-detail/index.svelte';
import { busy, contextMenu } from './stores'; import { busy, contextMenu } from './stores';
import ContextMenu from './components/contextmenu.svelte'; import ContextMenu from './components/contextmenu.svelte';
import Modal from './components/modal.svelte';
import { input } from './actions';
const connections = {}; const connections = {};
let hosts = {}; let hosts = {};
let activeHostKey = ''; let activeHostKey = '';
let activeDbKey = ''; let activeDbKey = '';
let activeCollKey = ''; let activeCollKey = '';
let addressBarModalOpen = true; let addressBarModalOpen = true;
let newDb;
let newDbInput;
let newColl;
let newCollInput;
$: host = hosts[activeHostKey]; $: host = hosts[activeHostKey];
$: connection = connections[activeHostKey]; $: connection = connections[activeHostKey];
$: database = connection?.databases[activeDbKey]; $: database = connection?.databases[activeDbKey];
$: collection = database?.collections?.[activeCollKey]; $: collection = database?.collections?.[activeCollKey];
$: if (newDb) {
tick().then(() => newDbInput.focus());
}
async function openConnection(hostKey) { async function openConnection(hostKey) {
busy.start(); busy.start();
const databases = await OpenConnection(hostKey); const databases = await OpenConnection(hostKey);
@ -36,6 +50,13 @@
busy.end(); busy.end();
} }
function createDatabase() {
busy.start();
connections[activeHostKey].databases[newDb.name] = { collections: {} };
newDb = undefined;
busy.end();
}
async function openDatabase(dbKey) { async function openDatabase(dbKey) {
busy.start(); busy.start();
const collections = await OpenDatabase(activeHostKey, dbKey); const collections = await OpenDatabase(activeHostKey, dbKey);
@ -50,7 +71,14 @@
async function dropDatabase(dbKey) { async function dropDatabase(dbKey) {
busy.start(); busy.start();
await DropDatabase(activeHostKey, dbKey); await DropDatabase(activeHostKey, dbKey);
await openConnection(activeHostKey); await reload();
busy.end();
}
function createCollection() {
busy.start();
connections[activeHostKey].databases[activeDbKey].collections[newColl.name] = {};
newColl = undefined;
busy.end(); busy.end();
} }
@ -64,8 +92,7 @@
async function dropCollection(dbKey, collKey) { async function dropCollection(dbKey, collKey) {
busy.start(); busy.start();
await DropCollection(activeHostKey, dbKey, collKey); await DropCollection(activeHostKey, dbKey, collKey);
await openConnection(activeHostKey); await reload();
await openDatabase(dbKey);
busy.end(); busy.end();
} }
@ -88,16 +115,26 @@
columns={[ { key: 'id' }, { key: 'collCount', right: true } ]} columns={[ { key: 'id' }, { key: 'collCount', right: true } ]}
items={Object.keys(connection.databases).map(dbKey => ({ items={Object.keys(connection.databases).map(dbKey => ({
id: dbKey, id: dbKey,
collCount: Object.keys(connection.databases[dbKey].collections || {}).length, collCount: Object.keys(connection.databases[dbKey].collections || {}).length || '',
children: Object.keys(connection.databases[dbKey].collections).map(collKey => ({ children: Object.keys(connection.databases[dbKey].collections).map(collKey => ({
id: collKey, id: collKey,
menu: [ { label: `Drop ${collKey}`, fn: () => dropCollection(dbKey, collKey) } ], menu: [ { label: `Drop ${collKey}`, fn: () => dropCollection(dbKey, collKey) } ],
})) || [], })).sort((a, b) => a.id.localeCompare(b)) || [],
menu: [ { label: `Drop ${dbKey}`, fn: () => dropDatabase(dbKey) } ], menu: [ { label: `Drop ${dbKey}`, fn: () => dropDatabase(dbKey) } ],
}))} }))}
actions={[ actions={[
{ icon: 'reload', fn: reload }, { icon: 'reload', fn: reload },
{ icon: '+' }, { icon: '+', fn: evt => {
if (activeDbKey) {
contextMenu.show(evt, [
{ label: 'New database', fn: () => newDb = {} },
{ label: 'New collection', fn: () => newColl = {} },
]);
}
else {
newDb = {};
}
} },
{ icon: '-' }, { icon: '-' },
]} ]}
bind:activeKey={activeDbKey} bind:activeKey={activeDbKey}
@ -118,6 +155,32 @@
{/if} {/if}
</main> </main>
{#if newDb}
<Modal bind:show={newDb}>
<p><strong>Create a database</strong></p>
<p>Note: databases in MongoDB do not exist until they have a collection and an item. Your new database will not persist on the server; fill it to have it created.</p>
<form on:submit|preventDefault={createDatabase}>
<label class="field">
<input type="text" spellcheck="false" bind:value={newDb.name} use:input placeholder="New collection name" bind:this={newDbInput} />
</label>
<button class="btn create" type="submit">Create database</button>
</form>
</Modal>
{/if}
{#if newColl}
<Modal bind:show={newColl}>
<p><strong>Create a collections</strong></p>
<p>Note: collections in MongoDB do not exist until they have at least one item. Your new collection will not persist on the server; fill it to have it created.</p>
<form on:submit|preventDefault={createCollection}>
<label class="field">
<input type="text" spellcheck="false" bind:value={newColl.name} use:input placeholder="New collection name" bind:this={newCollInput} />
</label>
<button class="btn create" type="submit">Create collection</button>
</form>
</Modal>
{/if}
{#key $contextMenu} {#key $contextMenu}
<ContextMenu {...$contextMenu} on:close={contextMenu.hide} /> <ContextMenu {...$contextMenu} on:close={contextMenu.hide} />
{/key} {/key}
@ -137,4 +200,8 @@
.databaselist { .databaselist {
overflow: scroll; overflow: scroll;
} }
.btn.create {
margin-top: 0.5rem;
}
</style> </style>

View File

@ -11,7 +11,7 @@
} }
function click(fn) { function click(fn) {
fn(); fn?.();
close(); close();
} }
</script> </script>
@ -54,6 +54,8 @@
button { button {
padding: 5px; padding: 5px;
border-radius: 5px; border-radius: 5px;
width: 100%;
text-align: left;
} }
button:hover { button:hover {
background-color: #00008b; background-color: #00008b;

View File

@ -18,19 +18,19 @@
{#if show} {#if show}
<div class="modal outer" on:mousedown|self={() => show = false} transition:fade> <div class="modal outer" on:mousedown|self={() => show = false} transition:fade>
<div class="inner" transition:fly={{ y: 100 }}> <div class="inner" transition:fly={{ y: 100 }}>
<header> {#if title}
{#if title} <header>
<div class="title">{title}</div> <div class="title">{title}</div>
{/if} <button class="btn close" on:click={() => show = false} title="close">
<button class="btn close" on:click={() => show = false} title="close"> <Icon name="x" />
<Icon name="x" /> </button>
</button> </header>
</header> {/if}
<div class="slot content" class:padded={contentPadding}> <slot /> </div> <div class="slot content" class:padded={contentPadding}> <slot /> </div>
{#if $$slots.footerLeft || $$slots.footerRight}
<footer> {#if $$slots.footer}
<slot name="footer" /> <footer> <slot name="footer" /> </footer>
</footer>
{/if} {/if}
</div> </div>
</div> </div>

View File

@ -26,6 +26,9 @@ body {
p { p {
margin: 0 0 0.7rem 0; margin: 0 0 0.7rem 0;
} }
p strong {
font-weight: 700;
}
.loading { .loading {
cursor: wait; cursor: wait;