2023-01-16 19:03:56 +00:00
< script >
2023-05-26 15:21:39 +00:00
import { startProgress } from '$lib/progress';
2023-05-31 18:20:39 +00:00
import connections from '$lib/stores/connections';
2023-06-11 07:34:00 +00:00
import { RenameCollection } from '$wails/go/app/App';
2023-02-21 16:47:21 +00:00
import { EnterText } from '$wails/go/ui/UI';
2023-02-15 18:27:51 +00:00
import { EventsOn } from '$wails/runtime/runtime';
2023-06-07 19:52:43 +00:00
import HostView from './host/index.svelte';
2023-06-07 19:30:22 +00:00
import DatabaseView from './database/index.svelte';
import CollectionView from './collection/index.svelte';
2023-02-21 19:26:46 +00:00
import DumpInfo from './dump.svelte';
2023-02-15 18:27:51 +00:00
import HostDetail from './hostdetail.svelte';
import HostTree from './hosttree.svelte';
2023-05-27 19:18:47 +00:00
import sharedState from '$lib/stores/sharedstate';
2023-05-31 18:20:39 +00:00
import Icon from '$components/icon.svelte';
import hosts from '$lib/stores/hosts';
2023-01-16 19:03:56 +00:00
export let activeHostKey = '';
export let activeDbKey = '';
export let activeCollKey = '';
2023-01-22 20:12:56 +00:00
let hostTree;
let showHostDetail = false;
let hostDetailKey = '';
2023-01-28 12:25:14 +00:00
let exportInfo;
2023-05-27 19:18:47 +00:00
$: sharedState.currentHost.set(activeHostKey);
$: sharedState.currentDb.set(activeDbKey);
$: sharedState.currentColl.set(activeCollKey);
2023-05-31 16:24:00 +00:00
export function createHost() {
2023-01-22 20:12:56 +00:00
hostDetailKey = '';
showHostDetail = true;
}
function editHost(hostKey) {
hostDetailKey = hostKey;
showHostDetail = true;
2023-01-16 19:03:56 +00:00
}
2023-05-31 16:24:00 +00:00
export async function createDatabase() {
2023-02-20 20:04:01 +00:00
const name = await EnterText('Create a database', 'Enter the database name. 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.');
if (name) {
$connections[activeHostKey].databases[name] = { collections : {} } ;
}
2023-01-22 20:12:56 +00:00
}
2023-02-21 07:14:52 +00:00
async function renameCollection(oldCollKey) {
const newCollKey = await EnterText('Rename collection', `Enter a new name for collection ${ oldCollKey } .`, oldCollKey);
if (newCollKey && (newCollKey !== oldCollKey)) {
2023-05-26 15:21:39 +00:00
const progress = startProgress(`Renaming collection "${ oldCollKey } " to "${ newCollKey } "…`);
2023-02-21 07:14:52 +00:00
const ok = await RenameCollection(activeHostKey, activeDbKey, oldCollKey, newCollKey);
if (ok) {
activeCollKey = newCollKey;
await hostTree.reload();
}
2023-05-26 15:21:39 +00:00
progress.end();
2023-01-22 20:12:56 +00:00
}
2023-01-16 19:03:56 +00:00
}
2023-05-31 16:24:00 +00:00
export async function createCollection() {
2023-02-20 20:04:01 +00:00
const name = await EnterText('Create a collection', '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.');
if (name) {
$connections[activeHostKey].databases[activeDbKey].collections[name] = {} ;
}
2023-01-16 19:03:56 +00:00
}
2023-01-28 12:25:14 +00:00
function exportCollection(collKey) {
exportInfo = {
2023-02-11 10:22:02 +00:00
type: 'export',
filetype: 'json',
2023-01-28 12:25:14 +00:00
hostKey: activeHostKey,
dbKey: activeDbKey,
collKeys: [ collKey ],
};
}
function dumpCollection(collKey) {
2023-02-11 10:22:02 +00:00
exportInfo = {
type: 'dump',
filetype: 'bson',
2023-01-28 12:25:14 +00:00
hostKey: activeHostKey,
dbKey: activeDbKey,
collKeys: [ collKey ],
};
}
2023-01-23 13:12:14 +00:00
EventsOn('CreateHost', createHost);
2023-02-20 20:04:01 +00:00
EventsOn('CreateDatabase', createDatabase);
EventsOn('CreateCollection', createCollection);
2023-01-16 19:03:56 +00:00
< / script >
2023-01-23 12:17:07 +00:00
< div class = "tree" >
2023-05-31 18:20:39 +00:00
< div class = "tree-buttons" >
< button class = "button-small" on:click = { createHost } >
< Icon name = "+" / > New host
< / button >
< / div >
2023-01-23 12:17:07 +00:00
< HostTree
bind:activeHostKey
bind:activeCollKey
bind:activeDbKey
bind:this={ hostTree }
on:newHost={ createHost }
2023-02-20 20:04:01 +00:00
on:newDatabase={ createDatabase }
on:newCollection={ createCollection }
2023-01-23 12:17:07 +00:00
on:editHost={ e => editHost ( e . detail )}
2023-02-21 07:14:52 +00:00
on:renameCollection={ e => renameCollection ( e . detail )}
2023-01-28 12:25:14 +00:00
on:exportCollection={ e => exportCollection ( e . detail )}
on:dumpCollection={ e => dumpCollection ( e . detail )}
2023-01-23 12:17:07 +00:00
/>
< / div >
2023-01-16 19:03:56 +00:00
2023-06-07 19:30:22 +00:00
{ #if activeCollKey }
< CollectionView
collection={ $connections [ activeHostKey ] ? . databases [ activeDbKey ] ? . collections ? .[ activeCollKey ]}
hostKey={ activeHostKey }
dbKey={ activeDbKey }
collKey={ activeCollKey }
/>
{ :else if activeDbKey }
< DatabaseView
database={ $connections [ activeHostKey ] ? . databases [ activeDbKey ]}
hostKey={ activeHostKey }
dbKey={ activeDbKey }
/>
2023-06-07 19:52:43 +00:00
{ :else if activeHostKey }
< HostView
host={ $connections [ activeHostKey ]}
hostKey={ activeHostKey }
/>
2023-06-07 19:30:22 +00:00
{ /if }
2023-01-16 19:03:56 +00:00
2023-01-22 20:12:56 +00:00
< HostDetail
bind:show={ showHostDetail }
2023-05-31 18:20:39 +00:00
on:reload={ hosts . update }
2023-06-11 07:34:00 +00:00
hostKey={ hostDetailKey }
2023-01-22 20:12:56 +00:00
/>
2023-05-31 18:20:39 +00:00
< DumpInfo bind:info = { exportInfo } / >
2023-01-28 12:25:14 +00:00
2023-01-18 13:05:45 +00:00
< style >
2023-01-23 12:17:07 +00:00
.tree {
padding: 0.5rem;
background-color: #fff;
}
2023-05-31 18:20:39 +00:00
.tree-buttons {
margin-bottom: 1rem;
}
2023-01-18 13:05:45 +00:00
< / style >