mirror of
https://github.com/garraflavatra/rolens.git
synced 2025-07-19 22:18:03 +00:00
Index creation
This commit is contained in:
@ -1,10 +1,13 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
@ -42,6 +45,75 @@ func (a *App) GetIndexes(hostKey, dbKey, collKey string) []bson.M {
|
||||
return results
|
||||
}
|
||||
|
||||
func (a *App) CreateIndex(hostKey, dbKey, collKey, jsonData string) string {
|
||||
client, ctx, close, err := a.connectToHost(hostKey)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return ""
|
||||
}
|
||||
defer close()
|
||||
|
||||
type modelItem struct {
|
||||
Key string `json:"key"`
|
||||
Sort interface{} `json:"sort"`
|
||||
}
|
||||
|
||||
var form struct {
|
||||
Name string `json:"name"`
|
||||
Background bool `json:"background"`
|
||||
Unique bool `json:"unique"`
|
||||
DropDuplicates bool `json:"dropDuplicates"`
|
||||
Sparse bool `json:"sparse"`
|
||||
Model []modelItem `json:"model"`
|
||||
}
|
||||
|
||||
err = json.Unmarshal([]byte(jsonData), &form)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
||||
Type: runtime.ErrorDialog,
|
||||
Title: "Couldn't parse JSON",
|
||||
Message: err.Error(),
|
||||
})
|
||||
return ""
|
||||
}
|
||||
|
||||
if form.Model == nil {
|
||||
form.Model = make([]modelItem, 0)
|
||||
}
|
||||
|
||||
var keys bson.D
|
||||
for _, v := range form.Model {
|
||||
asFloat, canBeFloat := v.Sort.(float64)
|
||||
if canBeFloat {
|
||||
v.Sort = int8(math.Floor(asFloat))
|
||||
}
|
||||
|
||||
keys = append(keys, bson.E{
|
||||
Key: v.Key,
|
||||
Value: v.Sort,
|
||||
})
|
||||
}
|
||||
|
||||
indexModel := mongo.IndexModel{
|
||||
Keys: keys,
|
||||
Options: options.Index().SetUnique(form.Unique).SetSparse(form.Sparse),
|
||||
}
|
||||
|
||||
name, err := client.Database(dbKey).Collection(collKey).Indexes().CreateOne(ctx, indexModel)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
||||
Type: runtime.ErrorDialog,
|
||||
Title: "Error while creating index",
|
||||
Message: err.Error(),
|
||||
})
|
||||
return ""
|
||||
}
|
||||
|
||||
return name
|
||||
}
|
||||
|
||||
func (a *App) DropIndex(hostKey, dbKey, collKey, indexName string) bool {
|
||||
client, ctx, close, err := a.connectToHost(hostKey)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user