2023-01-16 19:03:56 +00:00
< script >
2023-01-22 20:12:56 +00:00
import { onMount } from 'svelte';
import { Hosts , RenameCollection } from '../../../wailsjs/go/app/App';
2023-01-16 19:03:56 +00:00
import { input } from '../../actions';
import Modal from '../../components/modal.svelte';
2023-01-22 20:12:56 +00:00
import HostTree from './hosttree.svelte';
2023-01-16 19:03:56 +00:00
import { busy , connections } from '../../stores';
import CollectionDetail from './collection/index.svelte';
2023-01-22 20:12:56 +00:00
import HostDetail from './hostdetail.svelte';
import Icon from '../../components/icon.svelte';
2023-01-23 13:12:14 +00:00
import { EventsOn } from '../../../wailsjs/runtime/runtime';
2023-02-11 10:22:02 +00:00
import Export from './export/export.svelte';
2023-02-05 08:39:52 +00:00
import Hint from '../../components/hint.svelte';
2023-01-16 19:03:56 +00:00
export let hosts = {} ;
export let activeHostKey = '';
export let activeDbKey = '';
export let activeCollKey = '';
2023-01-22 20:12:56 +00:00
let hostTree;
2023-01-16 19:03:56 +00:00
let newDb;
let newColl;
2023-01-22 20:12:56 +00:00
let showHostDetail = false;
let hostDetailKey = '';
let collToRename = '';
let newCollKey = '';
2023-01-28 12:25:14 +00:00
let exportInfo;
2023-01-22 20:12:56 +00:00
async function getHosts() {
hosts = await Hosts();
}
function createHost() {
hostDetailKey = '';
showHostDetail = true;
}
function editHost(hostKey) {
hostDetailKey = hostKey;
showHostDetail = true;
2023-01-16 19:03:56 +00:00
}
2023-02-05 08:39:52 +00:00
function createDatabase() {
2023-01-16 19:03:56 +00:00
$connections[activeHostKey].databases[newDb.name] = { collections : {} } ;
newDb = undefined;
2023-01-22 20:12:56 +00:00
}
function openEditCollModal(collKey) {
newCollKey = collKey;
collToRename = collKey;
}
async function renameCollection() {
busy.start();
const ok = await RenameCollection(activeHostKey, activeDbKey, collToRename, newCollKey);
if (ok) {
activeCollKey = newCollKey;
collToRename = '';
newCollKey = '';
await hostTree.reload();
}
2023-01-16 19:03:56 +00:00
busy.end();
}
2023-02-05 08:39:52 +00:00
function createCollection() {
2023-01-16 19:03:56 +00:00
$connections[activeHostKey].databases[activeDbKey].collections[newColl.name] = {} ;
newColl = undefined;
}
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);
EventsOn('CreateDatabase', () => newDb = {} );
EventsOn('CreateCollection', () => newColl = {} );
2023-01-22 20:12:56 +00:00
onMount(getHosts);
2023-01-16 19:03:56 +00:00
< / script >
2023-01-23 12:17:07 +00:00
< div class = "tree" >
< HostTree
{ hosts }
bind:activeHostKey
bind:activeCollKey
bind:activeDbKey
bind:this={ hostTree }
on:newHost={ createHost }
on:newDatabase={() => newDb = {}}
on:newCollection={() => newColl = {}}
on:editHost={ e => editHost ( e . detail )}
on:renameCollection={ e => openEditCollModal ( 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
< CollectionDetail
collection={ $connections [ activeHostKey ] ? . databases [ activeDbKey ] ? . collections ? .[ activeCollKey ]}
hostKey={ activeHostKey }
dbKey={ activeDbKey }
collectionKey={ activeCollKey }
/>
2023-01-22 20:12:56 +00:00
< HostDetail
bind:show={ showHostDetail }
on:reload={ getHosts }
hostKey={ activeHostKey }
{ hosts }
/>
2023-02-11 10:22:02 +00:00
< Export bind:info = { exportInfo } { hosts } / >
2023-01-28 12:25:14 +00:00
2023-01-16 19:03:56 +00:00
{ #if newDb }
< Modal bind:show = { newDb } >
< p > < strong > Create a database< / strong > < / p >
2023-02-05 08:39:52 +00:00
< Hint >
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.
< / Hint >
2023-01-16 19:03:56 +00:00
< form on:submit | preventDefault = { createDatabase } >
< label class = "field" >
2023-01-22 20:12:56 +00:00
< input type = "text" spellcheck = "false" bind:value = { newDb . name } use:input= {{ autofocus : true }} placeholder = "New collection name" />
2023-01-16 19:03:56 +00:00
< / label >
2023-01-18 13:05:45 +00:00
< p class = "modal-actions" >
2023-01-16 19:07:56 +00:00
< button class = "btn create" type = "submit" disabled = { ! newDb . name ? . trim ()} > Create database </ button >
2023-01-18 13:05:45 +00:00
< button class = "btn secondary" type = "button" on:click = {() => newDb = undefined } > Cancel</button >
2023-01-16 19:07:56 +00:00
< / p >
2023-01-16 19:03:56 +00:00
< / form >
< / Modal >
{ /if }
{ #if newColl }
< Modal bind:show = { newColl } >
2023-01-22 20:12:56 +00:00
< p > < strong > Create a collection< / strong > < / p >
2023-02-05 08:39:52 +00:00
< Hint >
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.
< / Hint >
2023-01-16 19:03:56 +00:00
< form on:submit | preventDefault = { createCollection } >
< label class = "field" >
2023-01-22 20:12:56 +00:00
< input type = "text" spellcheck = "false" bind:value = { newColl . name } use:input= {{ autofocus : true }} placeholder = "New collection name" />
2023-01-16 19:03:56 +00:00
< / label >
2023-01-18 13:05:45 +00:00
< p class = "modal-actions" >
2023-01-16 19:07:56 +00:00
< button class = "btn create" type = "submit" disabled = { ! newColl . name ? . trim ()} > Create collection </ button >
2023-01-18 13:05:45 +00:00
< button class = "btn secondary" type = "button" on:click = {() => newColl = undefined } > Cancel</button >
2023-01-16 19:07:56 +00:00
< / p >
2023-01-16 19:03:56 +00:00
< / form >
< / Modal >
{ /if }
2023-01-18 13:05:45 +00:00
2023-01-22 20:12:56 +00:00
{ #if collToRename }
< Modal bind:show = { collToRename } width="400px" >
< form class = "rename" on:submit | preventDefault = { renameCollection } >
< div > Renaming collection < strong > { collToRename } </ strong ></ div >
< Icon name = "arr-d" / >
2023-02-05 08:39:52 +00:00
< label class = "field" >
< input type = "text" bind:value = { newCollKey } use:input= {{ autofocus : true }} spellcheck = "false" />
< / label >
< div class = "cancelorsave" >
2023-01-22 20:12:56 +00:00
< button class = "btn secondary" type = "button" on:click = {() => collToRename = '' } > Cancel</button >
< button class = "btn" type = "submit" > Save< / button >
< / div >
< / form >
< / Modal >
{ /if }
2023-01-18 13:05:45 +00:00
< style >
.modal-actions {
display: flex;
justify-content: space-between;
}
2023-01-22 20:12:56 +00:00
2023-01-23 12:17:07 +00:00
.tree {
padding: 0.5rem;
background-color: #fff;
}
2023-01-22 20:12:56 +00:00
.rename {
text-align: center;
display: flex;
flex-direction: column;
gap: 0.5rem;
align-items: center;
}
2023-02-05 08:39:52 +00:00
.rename .field {
width: 100%;
}
2023-01-22 20:12:56 +00:00
.rename input {
text-align: center;
2023-02-05 08:39:52 +00:00
width: 100%;
2023-01-22 20:12:56 +00:00
}
.rename strong {
font-weight: 700;
}
2023-02-05 08:39:52 +00:00
.rename .cancelorsave {
display: flex;
2023-01-22 20:12:56 +00:00
gap: 0.5rem;
2023-02-05 08:39:52 +00:00
justify-content: space-between;
2023-01-22 20:12:56 +00:00
width: 100%;
}
2023-01-18 13:05:45 +00:00
< / style >