2023-01-16 20:03:56 +01:00
|
|
|
<script>
|
2023-02-15 19:27:51 +01:00
|
|
|
import Grid from '$components/grid.svelte';
|
2023-05-26 17:21:39 +02:00
|
|
|
import { startProgress } from '$lib/progress';
|
2023-05-31 20:20:39 +02:00
|
|
|
import connections from '$lib/stores/connections';
|
2023-02-15 19:27:51 +01:00
|
|
|
import { WindowSetTitle } from '$wails/runtime/runtime';
|
2023-01-16 20:03:56 +01:00
|
|
|
import { createEventDispatcher } from 'svelte';
|
2023-05-31 20:20:39 +02:00
|
|
|
import { DropCollection, DropDatabase, OpenCollection, OpenConnection, OpenDatabase, RemoveHost, TruncateCollection } from '../../../wailsjs/go/app/App';
|
|
|
|
import hosts from '$lib/stores/hosts';
|
|
|
|
import { tick } from 'svelte';
|
2023-01-16 20:03:56 +01:00
|
|
|
|
|
|
|
export let activeHostKey = '';
|
|
|
|
export let activeDbKey = '';
|
|
|
|
export let activeCollKey = '';
|
|
|
|
|
|
|
|
const dispatch = createEventDispatcher();
|
2023-01-22 21:12:56 +01:00
|
|
|
let activeGridPath = [];
|
2023-06-07 21:12:09 +02:00
|
|
|
// $: activeGridPath[0] = activeHostKey || undefined;
|
|
|
|
// $: activeGridPath[1] = activeDbKey || undefined;
|
|
|
|
// $: activeGridPath[2] = activeCollKey || undefined;
|
2023-05-31 20:20:39 +02:00
|
|
|
$: host = $hosts[activeHostKey];
|
2023-01-16 20:03:56 +01:00
|
|
|
$: 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) {
|
2023-05-26 17:21:39 +02:00
|
|
|
const progress = startProgress(`Connecting to "${hostKey}"…`);
|
2023-06-07 21:52:43 +02:00
|
|
|
|
2023-06-07 21:12:09 +02:00
|
|
|
activeCollKey = '';
|
|
|
|
activeDbKey = '';
|
|
|
|
activeHostKey = hostKey;
|
2023-06-07 21:52:43 +02:00
|
|
|
|
|
|
|
const { databases, status, systemInfo } = await OpenConnection(hostKey);
|
2023-01-16 20:03:56 +01:00
|
|
|
|
|
|
|
if (databases) {
|
2023-06-07 21:52:43 +02:00
|
|
|
$connections[hostKey] = $connections[hostKey] || {};
|
|
|
|
$connections[hostKey].status = status;
|
|
|
|
$connections[hostKey].systemInfo = systemInfo;
|
|
|
|
|
|
|
|
$connections[hostKey].databases = $connections[hostKey].databases || {};
|
2023-01-16 20:03:56 +01:00
|
|
|
databases.forEach(dbKey => {
|
2023-06-07 21:12:09 +02:00
|
|
|
$connections[hostKey].databases[dbKey] =
|
|
|
|
$connections[hostKey].databases[dbKey] || { collections: {} };
|
2023-01-16 20:03:56 +01:00
|
|
|
});
|
2023-06-07 21:52:43 +02:00
|
|
|
|
2023-01-16 20:03:56 +01:00
|
|
|
activeHostKey = hostKey;
|
|
|
|
dispatch('connected', hostKey);
|
2023-05-31 20:20:39 +02:00
|
|
|
WindowSetTitle(`${$hosts[activeHostKey].name} - Rolens`);
|
2023-01-16 20:03:56 +01:00
|
|
|
}
|
|
|
|
|
2023-05-26 17:21:39 +02:00
|
|
|
progress.end();
|
2023-01-16 20:03:56 +01:00
|
|
|
}
|
|
|
|
|
2023-05-31 20:20:39 +02:00
|
|
|
async function removeHost(hostKey) {
|
|
|
|
activeCollKey = '';
|
|
|
|
activeDbKey = '';
|
|
|
|
activeHostKey = '';
|
2023-06-07 21:12:09 +02:00
|
|
|
|
2023-05-31 20:20:39 +02:00
|
|
|
await tick();
|
|
|
|
await RemoveHost(hostKey);
|
2023-06-07 21:12:09 +02:00
|
|
|
await reload();
|
|
|
|
await hosts.update();
|
2023-05-31 20:20:39 +02:00
|
|
|
}
|
|
|
|
|
2023-01-16 20:03:56 +01:00
|
|
|
async function openDatabase(dbKey) {
|
2023-05-26 17:21:39 +02:00
|
|
|
const progress = startProgress(`Opening database "${dbKey}"…`);
|
2023-06-07 21:30:22 +02:00
|
|
|
const { collections, stats } = await OpenDatabase(activeHostKey, dbKey);
|
2023-06-07 21:12:09 +02:00
|
|
|
activeDbKey = dbKey;
|
|
|
|
activeCollKey = '';
|
2023-06-07 21:30:22 +02:00
|
|
|
$connections[activeHostKey].databases[dbKey].stats = stats;
|
2023-01-16 20:03:56 +01:00
|
|
|
|
|
|
|
for (const collKey of collections || []) {
|
2023-06-07 21:12:09 +02:00
|
|
|
$connections[activeHostKey].databases[dbKey].collections[collKey] =
|
|
|
|
$connections[activeHostKey].databases[dbKey].collections[collKey] ||{};
|
2023-01-16 20:03:56 +01:00
|
|
|
}
|
|
|
|
|
2023-05-26 17:21:39 +02:00
|
|
|
progress.end();
|
2023-01-16 20:03:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function dropDatabase(dbKey) {
|
2023-05-26 17:21:39 +02:00
|
|
|
const progress = startProgress(`Dropping database "${dbKey}"…`);
|
2023-06-07 21:12:09 +02:00
|
|
|
const success = await DropDatabase(activeHostKey, dbKey);
|
|
|
|
if (success) {
|
|
|
|
activeCollKey = '';
|
|
|
|
activeDbKey = '';
|
|
|
|
await reload();
|
|
|
|
}
|
2023-05-26 17:21:39 +02:00
|
|
|
progress.end();
|
2023-01-16 20:03:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function openCollection(collKey) {
|
2023-05-26 17:21:39 +02:00
|
|
|
const progress = startProgress(`Opening collection "${collKey}"…`);
|
2023-01-16 20:03:56 +01:00
|
|
|
const stats = await OpenCollection(activeHostKey, activeDbKey, collKey);
|
2023-06-07 21:12:09 +02:00
|
|
|
activeCollKey = 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;
|
2023-05-26 17:21:39 +02:00
|
|
|
progress.end();
|
2023-01-16 20:03:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-23 13:35:31 +01:00
|
|
|
async function truncateCollection(dbKey, collKey) {
|
2023-05-26 17:21:39 +02:00
|
|
|
const progress = startProgress(`Truncating collection "${collKey}"…`);
|
2023-01-23 13:35:31 +01:00
|
|
|
await TruncateCollection(activeHostKey, dbKey, collKey);
|
|
|
|
await reload();
|
2023-05-26 17:21:39 +02:00
|
|
|
progress.end();
|
2023-01-23 13:35:31 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 20:03:56 +01:00
|
|
|
async function dropCollection(dbKey, collKey) {
|
2023-05-26 17:21:39 +02:00
|
|
|
const progress = startProgress(`Dropping collection "${collKey}"…`);
|
2023-06-07 21:12:09 +02:00
|
|
|
const success = await DropCollection(activeHostKey, dbKey, collKey);
|
|
|
|
if (success) {
|
|
|
|
activeCollKey = '';
|
|
|
|
await reload();
|
|
|
|
}
|
2023-05-26 17:21:39 +02:00
|
|
|
progress.end();
|
2023-01-16 20:03:56 +01:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2023-01-22 21:12:56 +01:00
|
|
|
<Grid
|
|
|
|
striped={false}
|
|
|
|
columns={[ { key: 'name' }, { key: 'count', right: true } ]}
|
2023-06-07 21:12:09 +02:00
|
|
|
bind:activePath={activeGridPath}
|
2023-05-31 20:20:39 +02:00
|
|
|
items={Object.keys($hosts).map(hostKey => ({
|
2023-01-22 21:12:56 +01:00
|
|
|
id: hostKey,
|
2023-05-31 20:20:39 +02:00
|
|
|
name: $hosts[hostKey].name,
|
2023-01-22 21:12:56 +01:00
|
|
|
icon: 'server',
|
|
|
|
children: Object.keys(connection?.databases || {}).sort().map(dbKey => ({
|
2023-01-16 20:03:56 +01:00
|
|
|
id: dbKey,
|
2023-01-22 21:12:56 +01:00
|
|
|
name: dbKey,
|
2023-01-21 10:37:52 +01:00
|
|
|
icon: 'db',
|
2023-01-22 21:12:56 +01:00
|
|
|
count: 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,
|
2023-01-22 21:12:56 +01:00
|
|
|
name: collKey,
|
2023-01-21 10:37:52 +01:00
|
|
|
icon: 'list',
|
2023-01-23 13:17:07 +01:00
|
|
|
menu: [
|
2023-02-16 20:19:04 +01:00
|
|
|
{ label: 'Export collection (JSON, CSV)…', fn: () => dispatch('exportCollection', collKey) },
|
|
|
|
{ label: 'Dump collection (BSON via mongodump)…', fn: () => dispatch('dumpCollection', collKey) },
|
2023-01-28 13:25:14 +01:00
|
|
|
{ separator: true },
|
2023-01-23 13:35:31 +01:00
|
|
|
{ label: 'Rename collection…', fn: () => dispatch('renameCollection', collKey) },
|
|
|
|
{ label: 'Truncate collection…', fn: () => truncateCollection(dbKey, collKey) },
|
|
|
|
{ label: 'Drop collection…', fn: () => dropCollection(dbKey, collKey) },
|
2023-01-23 13:17:07 +01:00
|
|
|
{ separator: true },
|
|
|
|
{ label: 'New collection…', fn: () => dispatch('newCollection') },
|
|
|
|
],
|
2023-01-18 16:31:13 +01:00
|
|
|
})) || [],
|
2023-01-23 13:17:07 +01:00
|
|
|
menu: [
|
2023-01-23 13:35:31 +01:00
|
|
|
{ label: 'Drop database…', fn: () => dropDatabase(dbKey) },
|
2023-01-23 13:17:07 +01:00
|
|
|
{ separator: true },
|
|
|
|
{ label: 'New database…', fn: () => dispatch('newDatabase') },
|
2023-01-29 20:16:31 +01:00
|
|
|
{ label: 'New collection…', fn: () => dispatch('newCollection') },
|
2023-01-23 13:17:07 +01:00
|
|
|
],
|
2023-01-22 21:12:56 +01:00
|
|
|
})),
|
2023-01-23 13:17:07 +01:00
|
|
|
menu: [
|
|
|
|
{ label: 'New database…', fn: () => dispatch('newDatabase') },
|
2023-01-23 13:35:31 +01:00
|
|
|
{ separator: true },
|
2023-05-31 20:20:39 +02:00
|
|
|
{ label: `Edit host ${$hosts[hostKey].name}…`, fn: () => dispatch('editHost', hostKey) },
|
|
|
|
{ label: `Remove host…`, fn: () => removeHost(hostKey) },
|
2023-01-23 13:17:07 +01:00
|
|
|
],
|
2023-01-22 21:12:56 +01:00
|
|
|
}))}
|
|
|
|
on:select={e => {
|
|
|
|
const key = e.detail.itemKey;
|
|
|
|
switch (e.detail?.level) {
|
|
|
|
case 0: return openConnection(key);
|
|
|
|
case 1: return openDatabase(key);
|
|
|
|
case 2: return openCollection(key);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
2023-01-23 13:17:07 +01:00
|
|
|
|
|
|
|
<!--
|
|
|
|
actions={[
|
|
|
|
{ icon: 'reload', fn: reload },
|
|
|
|
{ icon: '+', fn: evt => contextMenu.show(evt, buildMenu(activeHostKey, activeDbKey, activeCollKey, 'new')) },
|
|
|
|
{ icon: 'edit', fn: evt => contextMenu.show(evt, buildMenu(activeHostKey, activeDbKey, activeCollKey, 'edit')), disabled: !activeHostKey },
|
|
|
|
{ icon: '-', fn: evt => contextMenu.show(evt, buildMenu(activeHostKey, activeDbKey, activeCollKey, 'drop')), disabled: !activeDbKey },
|
|
|
|
]}
|
|
|
|
-->
|