2023-01-16 20:03:56 +01:00
|
|
|
<script>
|
|
|
|
import { busy, contextMenu, connections } from '../../stores';
|
|
|
|
import { createEventDispatcher } from 'svelte';
|
|
|
|
import { DropCollection, DropDatabase, OpenCollection, OpenConnection, OpenDatabase } from '../../../wailsjs/go/app/App';
|
|
|
|
import Grid from '../../components/grid.svelte';
|
2023-01-18 13:55:00 +01:00
|
|
|
import { WindowSetTitle } from '../../../wailsjs/runtime';
|
2023-01-16 20:03:56 +01:00
|
|
|
|
|
|
|
export let hosts = {};
|
|
|
|
export let activeHostKey = '';
|
|
|
|
export let activeDbKey = '';
|
|
|
|
export let activeCollKey = '';
|
|
|
|
|
|
|
|
const dispatch = createEventDispatcher();
|
2023-01-18 16:31:13 +01:00
|
|
|
let dbAndCollKeys = [];
|
|
|
|
$: activeDbKey = dbAndCollKeys[0];
|
|
|
|
$: activeCollKey = dbAndCollKeys[1];
|
2023-01-16 20:03:56 +01:00
|
|
|
$: host = hosts[activeHostKey];
|
|
|
|
$: connection = $connections[activeHostKey];
|
|
|
|
$: database = connection?.databases[activeDbKey];
|
|
|
|
$: collection = database?.collections?.[activeCollKey];
|
|
|
|
|
|
|
|
export async function reload() {
|
|
|
|
activeHostKey && await openConnection(activeHostKey);
|
|
|
|
activeDbKey && await openDatabase(activeDbKey);
|
|
|
|
activeCollKey && await openCollection(activeCollKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function openConnection(hostKey) {
|
|
|
|
busy.start();
|
|
|
|
const databases = await OpenConnection(hostKey);
|
|
|
|
|
|
|
|
if (databases) {
|
|
|
|
$connections[hostKey] = { databases: {} };
|
|
|
|
databases.forEach(dbKey => {
|
|
|
|
$connections[hostKey].databases[dbKey] = { collections: {} };
|
|
|
|
});
|
|
|
|
activeHostKey = hostKey;
|
|
|
|
dispatch('connected', hostKey);
|
2023-01-18 13:55:00 +01:00
|
|
|
WindowSetTitle(`${hosts[activeHostKey].name} - Mongodup`);
|
2023-01-16 20:03:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
busy.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function openDatabase(dbKey) {
|
|
|
|
busy.start();
|
|
|
|
const collections = await OpenDatabase(activeHostKey, dbKey);
|
|
|
|
|
|
|
|
for (const collKey of collections || []) {
|
|
|
|
$connections[activeHostKey].databases[dbKey].collections[collKey] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
busy.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function dropDatabase(dbKey) {
|
|
|
|
busy.start();
|
|
|
|
await DropDatabase(activeHostKey, dbKey);
|
|
|
|
await reload();
|
|
|
|
busy.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function openCollection(collKey) {
|
|
|
|
busy.start();
|
|
|
|
const stats = await OpenCollection(activeHostKey, activeDbKey, collKey);
|
2023-01-18 16:31:13 +01:00
|
|
|
$connections[activeHostKey].databases[activeDbKey].collections[collKey] = $connections[activeHostKey].databases[activeDbKey].collections[collKey] || {};
|
2023-01-16 20:03:56 +01:00
|
|
|
$connections[activeHostKey].databases[activeDbKey].collections[collKey].stats = stats;
|
|
|
|
busy.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function dropCollection(dbKey, collKey) {
|
|
|
|
busy.start();
|
|
|
|
await DropCollection(activeHostKey, dbKey, collKey);
|
|
|
|
await reload();
|
|
|
|
busy.end();
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
{#if host && connection}
|
|
|
|
<Grid
|
2023-01-18 14:15:54 +01:00
|
|
|
striped={false}
|
2023-01-16 20:03:56 +01:00
|
|
|
columns={[ { key: 'id' }, { key: 'collCount', right: true } ]}
|
2023-01-18 16:31:13 +01:00
|
|
|
items={Object.keys(connection.databases).sort().map(dbKey => ({
|
2023-01-16 20:03:56 +01:00
|
|
|
id: dbKey,
|
|
|
|
collCount: Object.keys(connection.databases[dbKey].collections || {}).length || '',
|
2023-01-18 16:31:13 +01:00
|
|
|
children: Object.keys(connection.databases[dbKey].collections).sort().map(collKey => ({
|
2023-01-16 20:03:56 +01:00
|
|
|
id: collKey,
|
|
|
|
menu: [
|
2023-01-17 20:53:01 +01:00
|
|
|
{ label: `Drop ${collKey}…`, fn: () => dropCollection(dbKey, collKey) },
|
|
|
|
{ label: `Drop ${dbKey}…`, fn: () => dropDatabase(dbKey) },
|
2023-01-16 20:03:56 +01:00
|
|
|
{ separator: true },
|
2023-01-18 13:59:55 +01:00
|
|
|
{ label: 'New database…', fn: () => dispatch('newDatabase') },
|
|
|
|
{ label: 'New collection…', fn: () => dispatch('newCollection') },
|
2023-01-16 20:03:56 +01:00
|
|
|
],
|
2023-01-18 16:31:13 +01:00
|
|
|
})) || [],
|
2023-01-16 20:03:56 +01:00
|
|
|
menu: [
|
2023-01-17 20:53:01 +01:00
|
|
|
{ label: `Drop ${dbKey}…`, fn: () => dropDatabase(dbKey) },
|
2023-01-16 20:03:56 +01:00
|
|
|
{ separator: true },
|
2023-01-17 20:53:01 +01:00
|
|
|
{ label: 'New database…', fn: () => dispatch('newDatabase') },
|
|
|
|
{ label: 'New collection…', fn: () => dispatch('newCollection') },
|
2023-01-16 20:03:56 +01:00
|
|
|
],
|
|
|
|
}))}
|
|
|
|
actions={[
|
|
|
|
{ icon: 'reload', fn: reload },
|
|
|
|
{ icon: '+', fn: evt => {
|
|
|
|
if (activeDbKey) {
|
|
|
|
contextMenu.show(evt, [
|
2023-01-17 20:53:01 +01:00
|
|
|
{ label: 'New database…', fn: () => dispatch('newDatabase') },
|
|
|
|
{ label: 'New collection…', fn: () => dispatch('newCollection') },
|
2023-01-16 20:03:56 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dispatch('newDatabase');
|
|
|
|
}
|
|
|
|
} },
|
|
|
|
{ icon: '-', fn: evt => {
|
|
|
|
if (activeCollKey) {
|
|
|
|
contextMenu.show(evt, [
|
2023-01-17 20:53:01 +01:00
|
|
|
{ label: 'Drop database…', fn: () => dropDatabase(activeDbKey) },
|
|
|
|
{ label: 'Drop collection…', fn: () => dropCollection(activeDbKey, activeCollKey) },
|
2023-01-16 20:03:56 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dropDatabase(activeDbKey);
|
|
|
|
}
|
|
|
|
}, disabled: !activeDbKey },
|
|
|
|
]}
|
2023-01-18 16:31:13 +01:00
|
|
|
bind:activePath={dbAndCollKeys}
|
|
|
|
on:select={e => {
|
|
|
|
if (e.detail?.level === 0) {
|
|
|
|
openDatabase(e.detail.itemKey);
|
|
|
|
}
|
|
|
|
else if (e.detail?.level === 1) {
|
|
|
|
openCollection(e.detail.itemKey);
|
|
|
|
}
|
|
|
|
}}
|
2023-01-16 20:03:56 +01:00
|
|
|
/>
|
|
|
|
{/if}
|