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'} +