1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-18 13:07:58 +00:00

Truncate collections

This commit is contained in:
Romein van Buren 2023-01-23 13:35:31 +01:00
parent c76e5c9503
commit b35222f86b
Signed by: romein
GPG Key ID: 0EFF8478ADDF6C49
4 changed files with 54 additions and 5 deletions

View File

@ -1,7 +1,7 @@
<script>
import { busy, connections } from '../../stores';
import { createEventDispatcher } from 'svelte';
import { DropCollection, DropDatabase, OpenCollection, OpenConnection, OpenDatabase } from '../../../wailsjs/go/app/App';
import { DropCollection, DropDatabase, OpenCollection, OpenConnection, OpenDatabase, TruncateCollection } from '../../../wailsjs/go/app/App';
import Grid from '../../components/grid.svelte';
import { WindowSetTitle } from '../../../wailsjs/runtime/runtime';
@ -69,6 +69,13 @@
busy.end();
}
async function truncateCollection(dbKey, collKey) {
busy.start();
await TruncateCollection(activeHostKey, dbKey, collKey);
await reload();
busy.end();
}
async function dropCollection(dbKey, collKey) {
busy.start();
await DropCollection(activeHostKey, dbKey, collKey);
@ -94,21 +101,22 @@
name: collKey,
icon: 'list',
menu: [
{ label: `Rename ${collKey} collection`, fn: () => dispatch('renameCollection', collKey) },
{ label: `Drop ${collKey}`, fn: () => dropCollection(dbKey, collKey) },
{ label: 'Rename collection…', fn: () => dispatch('renameCollection', collKey) },
{ label: 'Truncate collection…', fn: () => truncateCollection(dbKey, collKey) },
{ label: 'Drop collection…', fn: () => dropCollection(dbKey, collKey) },
{ separator: true },
{ label: 'New collection…', fn: () => dispatch('newCollection') },
],
})) || [],
menu: [
{ label: `Drop database ${dbKey}`, fn: () => dropDatabase(dbKey) },
{ label: 'Drop database…', fn: () => dropDatabase(dbKey) },
{ separator: true },
{ label: 'New database…', fn: () => dispatch('newDatabase') },
],
})),
menu: [
{ label: 'New database…', fn: () => dispatch('newDatabase') },
{ separator: true },
{ separator: true },
{ label: `Edit host ${hosts[hostKey].name}`, fn: () => dispatch('editHost', hostKey) },
],
}))}

View File

@ -41,6 +41,8 @@ export function RenameCollection(arg1:string,arg2:string,arg3:string,arg4:string
export function Settings():Promise<app.Settings>;
export function TruncateCollection(arg1:string,arg2:string,arg3:string):Promise<boolean>;
export function UpdateHost(arg1:string,arg2:string):Promise<void>;
export function UpdateItems(arg1:string,arg2:string,arg3:string,arg4:string):Promise<number>;

View File

@ -74,6 +74,10 @@ export function Settings() {
return window['go']['app']['App']['Settings']();
}
export function TruncateCollection(arg1, arg2, arg3) {
return window['go']['app']['App']['TruncateCollection'](arg1, arg2, arg3);
}
export function UpdateHost(arg1, arg2) {
return window['go']['app']['App']['UpdateHost'](arg1, arg2);
}

View File

@ -57,6 +57,39 @@ func (a *App) RenameCollection(hostKey, dbKey, collKey, newCollKey string) bool
return true
}
func (a *App) TruncateCollection(hostKey, dbKey, collKey string) bool {
sure, _ := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Confirm",
Message: "Are you sure you want to remove all items from " + collKey + "?",
Buttons: []string{"Yes", "No"},
DefaultButton: "Yes",
CancelButton: "No",
})
if sure != "Yes" {
return false
}
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
fmt.Println(err.Error())
return false
}
_, err = client.Database(dbKey).Collection(collKey).DeleteMany(ctx, bson.D{})
if err != nil {
fmt.Println(err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Could not truncate " + collKey,
Message: err.Error(),
})
return false
}
defer close()
return true
}
func (a *App) DropCollection(hostKey, dbKey, collKey string) bool {
sure, _ := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Confirm",
@ -74,6 +107,7 @@ func (a *App) DropCollection(hostKey, dbKey, collKey string) bool {
fmt.Println(err.Error())
return false
}
err = client.Database(dbKey).Collection(collKey).Drop(ctx)
if err != nil {
fmt.Println(err.Error())
@ -84,6 +118,7 @@ func (a *App) DropCollection(hostKey, dbKey, collKey string) bool {
})
return false
}
defer close()
return true
}