From af72f763503b23f136dac092f3446d315ed23a4e Mon Sep 17 00:00:00 2001 From: Romein van Buren Date: Wed, 18 Jan 2023 13:38:31 +0100 Subject: [PATCH] Updates to update screen - Code example for update - Nice default operators - Fixed memory leak --- .../connection/collection/update.svelte | 60 +++++++++++++------ internal/app/collection_update.go | 5 +- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/frontend/src/organisms/connection/collection/update.svelte b/frontend/src/organisms/connection/collection/update.svelte index e26c87d..85afc5d 100644 --- a/frontend/src/organisms/connection/collection/update.svelte +++ b/frontend/src/organisms/connection/collection/update.svelte @@ -35,16 +35,26 @@ $bit: 'Bit', }, }; + const allOperators = Object.values(atomicUpdateOperators).map(Object.keys).flat(); - const form = { query: '{}', parameters: [ {} ] }; + const form = { query: '{}', parameters: [ { type: '$set' } ] }; + let updatedCount; + $: code = buildCode(form); - $: code = `db.${collection.key}.${form.many ? 'updateMany' : 'updateOne'}()`; + function buildCode(form) { + const method = form.many ? 'updateMany' : 'updateOne'; + + let operation = '{ ' + form.parameters.filter(p => p.type).map(p => `${p.type}: ${p.value || '{}'}`).join(', ') + ' }'; + if (operation === '{ }') { + operation = ''; + } + + const code = `db.${collection.key}.${method}(${form.query || '{}'}${operation ? ', ' + operation : ''});`; + return code; + } async function submitQuery() { - // form = { query: '{}', parameters: [ {} ] }; - - const result = await UpdateItems(collection.hostKey, collection.dbKey, collection.key, JSON.stringify(form)); - console.log(result); + updatedCount = await UpdateItems(collection.hostKey, collection.dbKey, collection.key, JSON.stringify(form)); } function removeParam(index) { @@ -56,13 +66,21 @@ } function addParameter(index) { + const usedOperators = form.parameters.map(p => p.type); + const operator = allOperators.find(o => !usedOperators.includes(o)); + + if (!operator) { + return; + } + + const newItem = { type: operator }; if (typeof index !== 'number') { - form.parameters = [ ...form.parameters, {} ]; + form.parameters = [ ...form.parameters, newItem ]; } else { form.parameters = [ ...form.parameters.slice(0, index), - {}, + newItem, ...form.parameters.slice(index), ]; } @@ -86,6 +104,16 @@ + + {#key updatedCount} + {#if typeof updatedCount === 'number'} +
+ Updated {updatedCount} item{updatedCount === 1 ? '' : 's'} +
+ {/if} + {/key} + + - - diff --git a/internal/app/collection_update.go b/internal/app/collection_update.go index 707eafa..a83158e 100644 --- a/internal/app/collection_update.go +++ b/internal/app/collection_update.go @@ -56,7 +56,8 @@ func (a *App) UpdateItems(hostKey, dbKey, collKey string, formJson string) int64 for _, param := range form.Parameters { var unmarshalled bson.M - if json.Unmarshal([]byte(param.Value), &unmarshalled) == nil { + err = json.Unmarshal([]byte(param.Value), &unmarshalled) + if err == nil { update[param.Type] = unmarshalled } else { fmt.Println(err.Error()) @@ -72,8 +73,6 @@ func (a *App) UpdateItems(hostKey, dbKey, collKey string, formJson string) int64 var result *mongo.UpdateResult options := mongoOptions.Update().SetUpsert(form.Upsert) - fmt.Println(query, update) - if form.Many { result, err = client.Database(dbKey).Collection(collKey).UpdateMany(ctx, query, update, options) } else {