1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-02-01 02:39:26 +00:00

146 lines
5.1 KiB
Svelte
Raw Normal View History

2023-01-16 20:03:56 +01:00
<script>
2023-01-23 13:17:07 +01:00
import { busy, connections } from '../../stores';
2023-01-16 20:03:56 +01:00
import { createEventDispatcher } from 'svelte';
2023-01-23 13:35:31 +01:00
import { DropCollection, DropDatabase, OpenCollection, OpenConnection, OpenDatabase, TruncateCollection } from '../../../wailsjs/go/app/App';
2023-01-16 20:03:56 +01:00
import Grid from '../../components/grid.svelte';
2023-01-22 21:12:56 +01:00
import { WindowSetTitle } from '../../../wailsjs/runtime/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-22 21:12:56 +01:00
let activeGridPath = [];
$: activeHostKey = activeGridPath[0] || activeHostKey;
$: activeDbKey = activeGridPath[1];
$: activeCollKey = activeGridPath[2];
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);
WindowSetTitle(`${hosts[activeHostKey].name} - Rolens`);
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();
}
2023-01-23 13:35:31 +01:00
async function truncateCollection(dbKey, collKey) {
busy.start();
await TruncateCollection(activeHostKey, dbKey, collKey);
await reload();
busy.end();
}
2023-01-16 20:03:56 +01:00
async function dropCollection(dbKey, collKey) {
busy.start();
await DropCollection(activeHostKey, dbKey, collKey);
await reload();
busy.end();
}
</script>
2023-01-22 21:12:56 +01:00
<Grid
striped={false}
columns={[ { key: 'name' }, { key: 'count', right: true } ]}
items={Object.keys(hosts).map(hostKey => ({
id: hostKey,
name: hosts[hostKey].name,
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-01-28 13:25:14 +01:00
{ label: 'Export collection (JSON/CSV, mongoexport)…', fn: () => dispatch('exportCollection', collKey) },
{ label: 'Dump collection (BSON, mongodump)…', fn: () => dispatch('dumpCollection', collKey) },
{ 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-01-23 13:17:07 +01:00
{ label: `Edit host ${hosts[hostKey].name}`, fn: () => dispatch('editHost', hostKey) },
],
2023-01-22 21:12:56 +01:00
}))}
bind:activePath={activeGridPath}
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 },
]}
-->