1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-18 21:17:59 +00:00

Updates to update screen

- Code example for update
- Nice default operators
- Fixed memory leak
This commit is contained in:
Romein van Buren 2023-01-18 13:38:31 +01:00
parent 07a17669c5
commit af72f76350
Signed by: romein
GPG Key ID: 0EFF8478ADDF6C49
2 changed files with 43 additions and 22 deletions

View File

@ -35,16 +35,26 @@
$bit: 'Bit', $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() { async function submitQuery() {
// form = { query: '{}', parameters: [ {} ] }; updatedCount = await UpdateItems(collection.hostKey, collection.dbKey, collection.key, JSON.stringify(form));
const result = await UpdateItems(collection.hostKey, collection.dbKey, collection.key, JSON.stringify(form));
console.log(result);
} }
function removeParam(index) { function removeParam(index) {
@ -56,13 +66,21 @@
} }
function addParameter(index) { 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') { if (typeof index !== 'number') {
form.parameters = [ ...form.parameters, {} ]; form.parameters = [ ...form.parameters, newItem ];
} }
else { else {
form.parameters = [ form.parameters = [
...form.parameters.slice(0, index), ...form.parameters.slice(0, index),
{}, newItem,
...form.parameters.slice(index), ...form.parameters.slice(index),
]; ];
} }
@ -86,6 +104,16 @@
<input type="checkbox" bind:checked={form.many} /> <input type="checkbox" bind:checked={form.many} />
</span> </span>
</label> </label>
{#key updatedCount}
{#if typeof updatedCount === 'number'}
<div class="flash-green">
Updated {updatedCount} item{updatedCount === 1 ? '' : 's'}
</div>
{/if}
{/key}
<button class="btn" type="submit">Update</button>
</div> </div>
<label class="field"> <label class="field">
@ -114,7 +142,7 @@
<input type="text" class="code" bind:value={param.value} placeholder={'{}'} use:input={{ json: true }} /> <input type="text" class="code" bind:value={param.value} placeholder={'{}'} use:input={{ json: true }} />
</label> </label>
<button class="btn" on:click={() => addParameter()} type="button"> <button class="btn" disabled={form.parameters.length >= allOperators.length} on:click={() => addParameter()} type="button">
<Icon name="+" /> <Icon name="+" />
</button> </button>
<button class="btn" disabled={form.parameters.length < 2} on:click={() => removeParam(index)} type="button"> <button class="btn" disabled={form.parameters.length < 2} on:click={() => removeParam(index)} type="button">
@ -123,11 +151,6 @@
</fieldset> </fieldset>
{/each} {/each}
</fieldset> </fieldset>
<div class="result">
<div></div>
<button class="btn" type="submit">Update</button>
</div>
</form> </form>
<style> <style>
@ -140,6 +163,10 @@
.options { .options {
display: flex; display: flex;
gap: 0.5rem; gap: 0.5rem;
align-items: center;
}
.options button {
margin-left: auto;
} }
.parameters { .parameters {
@ -152,9 +179,4 @@
grid-template: 1fr / auto 1fr auto auto; grid-template: 1fr / auto 1fr auto auto;
gap: 0.5rem; gap: 0.5rem;
} }
.result {
display: flex;
justify-content: space-between;
}
</style> </style>

View File

@ -56,7 +56,8 @@ func (a *App) UpdateItems(hostKey, dbKey, collKey string, formJson string) int64
for _, param := range form.Parameters { for _, param := range form.Parameters {
var unmarshalled bson.M 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 update[param.Type] = unmarshalled
} else { } else {
fmt.Println(err.Error()) fmt.Println(err.Error())
@ -72,8 +73,6 @@ func (a *App) UpdateItems(hostKey, dbKey, collKey string, formJson string) int64
var result *mongo.UpdateResult var result *mongo.UpdateResult
options := mongoOptions.Update().SetUpsert(form.Upsert) options := mongoOptions.Update().SetUpsert(form.Upsert)
fmt.Println(query, update)
if form.Many { if form.Many {
result, err = client.Database(dbKey).Collection(collKey).UpdateMany(ctx, query, update, options) result, err = client.Database(dbKey).Collection(collKey).UpdateMany(ctx, query, update, options)
} else { } else {