From 90ffdc6a8be5abd4632d5f04b215e377dc1a1efc Mon Sep 17 00:00:00 2001 From: Romein van Buren Date: Wed, 11 Jan 2023 20:41:15 +0100 Subject: [PATCH] Many small tweaks --- frontend/src/app.svelte | 76 ++++----- frontend/src/components/code-example.svelte | 1 - frontend/src/components/grid.svelte | 3 +- .../src/organisms/addressbar/index.svelte | 16 +- .../organisms/collection-detail/find.svelte | 157 ++++++++++-------- .../organisms/collection-detail/index.svelte | 57 +++---- .../organisms/collection-detail/insert.svelte | 4 - .../organisms/collection-detail/stats.svelte | 19 +++ frontend/wailsjs/go/main/App.d.ts | 3 +- frontend/wailsjs/go/models.ts | 19 +++ main.go | 39 +++-- 11 files changed, 223 insertions(+), 171 deletions(-) create mode 100644 frontend/src/organisms/collection-detail/stats.svelte create mode 100755 frontend/wailsjs/go/models.ts diff --git a/frontend/src/app.svelte b/frontend/src/app.svelte index 8bed635..3e5a450 100644 --- a/frontend/src/app.svelte +++ b/frontend/src/app.svelte @@ -11,7 +11,7 @@ let activeHostKey = ''; let activeDbKey = ''; let activeCollKey = ''; - let addressBarModalOpen = false; + let addressBarModalOpen = true; $: host = hosts[activeHostKey]; $: connection = connections[activeHostKey]; @@ -61,60 +61,46 @@
openConnection(e.detail)} bind:modalOpen={addressBarModalOpen} /> -
- {#if host && connection} -
- ({ - id, - collCount: Object.keys(connection.databases[id].collections || {}).length, - children: connection.databases[id].collections || [], - }))} - bind:activeKey={activeDbKey} - bind:activeChildKey={activeCollKey} - on:select={e => openDatabase(e.detail)} - on:selectChild={e => openCollection(e.detail)} - /> -
+ {#if host && connection} +
+ ({ + id, + collCount: Object.keys(connection.databases[id].collections || {}).length, + children: connection.databases[id].collections || [], + }))} + bind:activeKey={activeDbKey} + bind:activeChildKey={activeCollKey} + on:select={e => openDatabase(e.detail)} + on:selectChild={e => openCollection(e.detail)} + /> +
-
- -
- {/if} -
+
+ +
+ {/if}
diff --git a/frontend/src/components/code-example.svelte b/frontend/src/components/code-example.svelte index 16513ed..06eb5ea 100644 --- a/frontend/src/components/code-example.svelte +++ b/frontend/src/components/code-example.svelte @@ -13,7 +13,6 @@ border-radius: 10px; padding: 0.5rem; opacity: 0.6; - margin-bottom: 0.5rem; } strong { diff --git a/frontend/src/components/grid.svelte b/frontend/src/components/grid.svelte index ea26748..848dc8e 100644 --- a/frontend/src/components/grid.svelte +++ b/frontend/src/components/grid.svelte @@ -107,8 +107,7 @@ .grid { background-color: #fff; width: 100%; - overflow: scroll; - max-height: 400px; /* fixme */ + height: 100%; } .grid.contained { border: 1px solid #ccc; diff --git a/frontend/src/organisms/addressbar/index.svelte b/frontend/src/organisms/addressbar/index.svelte index 975c647..1279886 100644 --- a/frontend/src/organisms/addressbar/index.svelte +++ b/frontend/src/organisms/addressbar/index.svelte @@ -18,8 +18,13 @@
-
modalOpen = true}> - {host?.uri || 'No host selected'} +
modalOpen = true}> + {#if host?.uri} + {@const split = host.uri.split('://').map(s => s.split('/')).flat()} + {split[0]}://{split[1]}{split.slice(2).join('/')} + {:else} + no host selected + {/if}
@@ -48,14 +53,15 @@ display: flex; justify-content: space-between; align-items: center; - margin: 1rem; padding: 0.5rem 0.5rem 0.5rem 1rem; + height: 3rem; border: 1px solid #ccc; border-radius: 10px; } - .address.empty { - font-style: italic; + .address .protocol, + .address .path, + .address .empty { opacity: 0.6; } diff --git a/frontend/src/organisms/collection-detail/find.svelte b/frontend/src/organisms/collection-detail/find.svelte index 4189902..322e51d 100644 --- a/frontend/src/organisms/collection-detail/find.svelte +++ b/frontend/src/organisms/collection-detail/find.svelte @@ -24,20 +24,19 @@ limit: 30, }; - let result = []; + let result = {}; + let submittedForm = {}; let queryField; let activeKey = ''; $: code = `db.${collection.key}.find(${form.query || '{}'}${form.fields && form.fields !== '{}' ? `, ${form.fields}` : ''}).sort(${form.sort})${form.skip ? `.skip(${form.skip})` : ''}${form.limit ? `.limit(${form.limit})` : ''};`; - $: if (collection) { - result = []; - } - async function submitQuery() { activeKey = ''; result = await PerformFind(collection.hostKey, collection.dbKey, collection.key, JSON.stringify(form)); - queryField?.focus(); - queryField?.select(); + if (result) { + submittedForm = JSON.parse(JSON.stringify(form)); + } + resetFocus(); } function prev() { @@ -58,92 +57,104 @@ alert('yet to be implemented'); } - onMount(() => { + function resetFocus() { queryField?.focus(); queryField?.select(); - }); + } + + onMount(resetFocus); -
-
- +
+ +
+ - -
- -
- - - - - - - -
- - - - -
- -
-
- {#if result} - Results: {result.length} - {/if} +
-
- - - + +
+ + + + + + + +
+ + + + +
+
+ {#key result} + + {/key} +
+ +
+
+ {#if result} + Results: {result.total || 0} + {/if} +
+
+ + + +
diff --git a/frontend/src/organisms/collection-detail/insert.svelte b/frontend/src/organisms/collection-detail/insert.svelte index 3b77e58..ba0dc97 100644 --- a/frontend/src/organisms/collection-detail/insert.svelte +++ b/frontend/src/organisms/collection-detail/insert.svelte @@ -6,10 +6,6 @@ let input = ''; let insertedIds; - $: if (collection) { - insertedIds = undefined; - } - async function insert() { insertedIds = await PerformInsert(collection.hostKey, collection.dbKey, collection.key, input); } diff --git a/frontend/src/organisms/collection-detail/stats.svelte b/frontend/src/organisms/collection-detail/stats.svelte new file mode 100644 index 0000000..208e513 --- /dev/null +++ b/frontend/src/organisms/collection-detail/stats.svelte @@ -0,0 +1,19 @@ + + +
+ + +
+ + diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/main/App.d.ts index 424c7e8..e0bbea2 100755 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/main/App.d.ts @@ -2,6 +2,7 @@ // This file is automatically generated. DO NOT EDIT import {map[string]main} from '../models'; import {primitive} from '../models'; +import {main} from '../models'; export function Hosts():Promise; @@ -11,6 +12,6 @@ export function OpenConnection(arg1:string):Promise>; export function OpenDatabase(arg1:string,arg2:string):Promise>; -export function PerformFind(arg1:string,arg2:string,arg3:string,arg4:string):Promise; +export function PerformFind(arg1:string,arg2:string,arg3:string,arg4:string):Promise; export function PerformInsert(arg1:string,arg2:string,arg3:string,arg4:string):Promise; diff --git a/frontend/wailsjs/go/models.ts b/frontend/wailsjs/go/models.ts new file mode 100755 index 0000000..7a37ffd --- /dev/null +++ b/frontend/wailsjs/go/models.ts @@ -0,0 +1,19 @@ +export namespace main { + + export class findResult { + total: number; + results: any; + + static createFrom(source: any = {}) { + return new findResult(source); + } + + constructor(source: any = {}) { + if ('string' === typeof source) source = JSON.parse(source); + this.total = source["total"]; + this.results = source["results"]; + } + } + +} + diff --git a/main.go b/main.go index 6f9bef1..37071d1 100644 --- a/main.go +++ b/main.go @@ -141,7 +141,13 @@ func (a *App) OpenCollection(hostKey, dbKey, collKey string) (result bson.M) { return result } -func (a *App) PerformFind(hostKey, dbKey, collKey string, formJson string) interface{} { +type findResult struct { + Total int64 `json:"total"` + Results interface{} `json:"results"` +} + +func (a *App) PerformFind(hostKey, dbKey, collKey string, formJson string) findResult { + var out findResult var form struct { Fields string `json:"fields"` Limit int64 `json:"limit"` @@ -158,13 +164,13 @@ func (a *App) PerformFind(hostKey, dbKey, collKey string, formJson string) inter Title: "Couldn't parse form", Message: err.Error(), }) - return nil + return out } client, ctx, close, err := a.connectToHost(hostKey) if err != nil { fmt.Println(err.Error()) - return nil + return out } defer close() @@ -180,7 +186,7 @@ func (a *App) PerformFind(hostKey, dbKey, collKey string, formJson string) inter Title: "Invalid query", Message: err.Error(), }) - return nil + return out } err = json.Unmarshal([]byte(form.Fields), &projection) @@ -191,7 +197,7 @@ func (a *App) PerformFind(hostKey, dbKey, collKey string, formJson string) inter Title: "Invalid projection", Message: err.Error(), }) - return nil + return out } err = json.Unmarshal([]byte(form.Sort), &sort) @@ -202,7 +208,7 @@ func (a *App) PerformFind(hostKey, dbKey, collKey string, formJson string) inter Title: "Invalid sort", Message: err.Error(), }) - return nil + return out } opt := mongoOptions.FindOptions{ @@ -212,7 +218,18 @@ func (a *App) PerformFind(hostKey, dbKey, collKey string, formJson string) inter Sort: sort, } - cur, err := client.Database(dbKey).Collection(collKey).Find(ctx, bson.D{}, &opt) + total, err := client.Database(dbKey).Collection(collKey).CountDocuments(ctx, query, nil) + if err != nil { + fmt.Println(err.Error()) + runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ + Type: runtime.ErrorDialog, + Title: "Encountered an error while counting documents", + Message: err.Error(), + }) + return out + } + + cur, err := client.Database(dbKey).Collection(collKey).Find(ctx, query, &opt) if err != nil { fmt.Println(err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ @@ -220,7 +237,7 @@ func (a *App) PerformFind(hostKey, dbKey, collKey string, formJson string) inter Title: "Encountered an error while performing query", Message: err.Error(), }) - return nil + return out } defer cur.Close(ctx) @@ -234,10 +251,12 @@ func (a *App) PerformFind(hostKey, dbKey, collKey string, formJson string) inter Title: "Encountered an error while performing query", Message: err.Error(), }) - return nil + return out } - return results + out.Results = results + out.Total = total + return out } func (a *App) PerformInsert(hostKey, dbKey, collKey, jsonData string) interface{} {