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

141 lines
4.6 KiB
Svelte
Raw Normal View History

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';
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);
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();
}
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,
2023-01-21 10:37:52 +01:00
icon: 'db',
2023-01-16 20:03:56 +01:00
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,
2023-01-21 10:37:52 +01:00
icon: 'list',
2023-01-16 20:03:56 +01:00
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}