2023-01-10 17:28:27 +01:00
|
|
|
<script>
|
|
|
|
import { PerformFind } from '../../../wailsjs/go/main/App';
|
|
|
|
import CodeExample from '../../components/code-example.svelte';
|
|
|
|
import { onMount } from 'svelte';
|
|
|
|
import { input } from '../../actions';
|
|
|
|
import ObjectGrid from '../../components/objectgrid.svelte';
|
2023-01-10 20:10:39 +01:00
|
|
|
import Icon from '../../components/icon.svelte';
|
2023-01-10 17:28:27 +01:00
|
|
|
|
|
|
|
export let collection;
|
|
|
|
|
|
|
|
const defaults = {
|
|
|
|
query: '{}',
|
|
|
|
sort: '{ "_id": 1 }',
|
|
|
|
fields: '{}',
|
|
|
|
skip: 0,
|
|
|
|
limit: 30,
|
|
|
|
};
|
|
|
|
|
2023-01-13 16:56:48 +01:00
|
|
|
let form = {
|
2023-01-10 17:28:27 +01:00
|
|
|
query: '{}',
|
|
|
|
sort: '{ "_id": 1 }',
|
|
|
|
fields: '{}',
|
|
|
|
skip: 0,
|
|
|
|
limit: 30,
|
|
|
|
};
|
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
let result = {};
|
|
|
|
let submittedForm = {};
|
2023-01-10 17:28:27 +01:00
|
|
|
let queryField;
|
2023-01-10 20:22:57 +01:00
|
|
|
let activeKey = '';
|
2023-01-10 17:28:27 +01:00
|
|
|
$: code = `db.${collection.key}.find(${form.query || '{}'}${form.fields && form.fields !== '{}' ? `, ${form.fields}` : ''}).sort(${form.sort})${form.skip ? `.skip(${form.skip})` : ''}${form.limit ? `.limit(${form.limit})` : ''};`;
|
|
|
|
|
|
|
|
async function submitQuery() {
|
2023-01-10 20:22:57 +01:00
|
|
|
activeKey = '';
|
2023-01-10 17:28:27 +01:00
|
|
|
result = await PerformFind(collection.hostKey, collection.dbKey, collection.key, JSON.stringify(form));
|
2023-01-11 20:41:15 +01:00
|
|
|
if (result) {
|
|
|
|
submittedForm = JSON.parse(JSON.stringify(form));
|
|
|
|
}
|
|
|
|
resetFocus();
|
2023-01-10 17:28:27 +01:00
|
|
|
}
|
|
|
|
|
2023-01-10 20:22:57 +01:00
|
|
|
function prev() {
|
|
|
|
form.skip -= form.limit;
|
|
|
|
if (form.skip < 0) {
|
|
|
|
form.skip = 0;
|
|
|
|
}
|
|
|
|
submitQuery();
|
|
|
|
}
|
|
|
|
|
|
|
|
function next() {
|
|
|
|
form.skip += form.limit;
|
|
|
|
submitQuery();
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove() {
|
|
|
|
// eslint-disable-next-line no-alert
|
|
|
|
alert('yet to be implemented');
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
function resetFocus() {
|
2023-01-10 17:28:27 +01:00
|
|
|
queryField?.focus();
|
|
|
|
queryField?.select();
|
2023-01-11 20:41:15 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 16:56:48 +01:00
|
|
|
export function performQuery(q) {
|
|
|
|
form = { ...defaults, ...q };
|
|
|
|
console.log(form);
|
|
|
|
submitQuery();
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
onMount(resetFocus);
|
2023-01-10 17:28:27 +01:00
|
|
|
</script>
|
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
<div class="find">
|
|
|
|
<form on:submit|preventDefault={submitQuery}>
|
|
|
|
<div class="form-row one">
|
|
|
|
<label class="field">
|
|
|
|
<span class="label">Query or id</span>
|
|
|
|
<input type="text" class="code" bind:this={queryField} bind:value={form.query} use:input={{ json: true }} placeholder={defaults.query} />
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<label class="field">
|
|
|
|
<span class="label">Sort</span>
|
|
|
|
<input type="text" class="code" bind:value={form.sort} use:input={{ json: true }} placeholder={defaults.sort} />
|
|
|
|
</label>
|
|
|
|
</div>
|
2023-01-10 17:28:27 +01:00
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
<div class="form-row two">
|
|
|
|
<label class="field">
|
|
|
|
<span class="label">Fields</span>
|
|
|
|
<input type="text" class="code" bind:value={form.fields} use:input={{ json: true }} placeholder={defaults.fields} />
|
|
|
|
</label>
|
2023-01-10 17:28:27 +01:00
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
<label class="field">
|
|
|
|
<span class="label">Skip</span>
|
|
|
|
<input type="number" min="0" bind:value={form.skip} use:input placeholder={defaults.skip} />
|
|
|
|
</label>
|
2023-01-10 17:28:27 +01:00
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
<label class="field">
|
|
|
|
<span class="label">Limit</span>
|
|
|
|
<input type="number" min="0" bind:value={form.limit} use:input placeholder={defaults.limit} />
|
|
|
|
</label>
|
2023-01-10 17:28:27 +01:00
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
<button type="submit" class="btn">Run</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
2023-01-10 17:28:27 +01:00
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
<CodeExample {code} />
|
2023-01-10 20:10:39 +01:00
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
<div class="result">
|
|
|
|
<div class="grid">
|
|
|
|
{#key result}
|
|
|
|
<ObjectGrid data={result.results} bind:activeKey />
|
|
|
|
{/key}
|
2023-01-10 20:10:39 +01:00
|
|
|
</div>
|
2023-01-11 20:41:15 +01:00
|
|
|
|
|
|
|
<div class="controls">
|
|
|
|
<div>
|
2023-01-13 16:56:48 +01:00
|
|
|
{#key result}
|
|
|
|
<span class="flash-green">Results: {result.total || 0}</span>
|
|
|
|
{/key}
|
2023-01-11 20:41:15 +01:00
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<button class="btn danger" on:click={remove} disabled={!activeKey}>
|
|
|
|
<Icon name="-" />
|
|
|
|
</button>
|
|
|
|
<button class="btn" on:click={prev} disabled={!submittedForm.limit || (submittedForm.skip <= 0) || !result?.results?.length}>
|
|
|
|
<Icon name="chev-l" />
|
|
|
|
</button>
|
|
|
|
<button class="btn" on:click={next} disabled={!submittedForm.limit || ((result?.results?.length || 0) < submittedForm.limit) || !result?.results?.length}>
|
|
|
|
<Icon name="chev-r" />
|
|
|
|
</button>
|
|
|
|
</div>
|
2023-01-10 20:10:39 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-01-10 17:28:27 +01:00
|
|
|
|
|
|
|
<style>
|
2023-01-11 20:41:15 +01:00
|
|
|
.find {
|
2023-01-10 17:28:27 +01:00
|
|
|
display: grid;
|
|
|
|
gap: 0.5rem;
|
2023-01-11 20:41:15 +01:00
|
|
|
grid-template: auto auto 1fr / 1fr;
|
2023-01-10 17:28:27 +01:00
|
|
|
}
|
2023-01-11 20:41:15 +01:00
|
|
|
|
|
|
|
.form-row {
|
2023-01-10 17:28:27 +01:00
|
|
|
display: grid;
|
|
|
|
gap: 0.5rem;
|
2023-01-11 20:41:15 +01:00
|
|
|
}
|
|
|
|
.form-row.one {
|
|
|
|
grid-template: 1fr / 3fr 2fr;
|
2023-01-10 17:28:27 +01:00
|
|
|
margin-bottom: 0.5rem;
|
|
|
|
}
|
2023-01-11 20:41:15 +01:00
|
|
|
.form-row.two {
|
|
|
|
grid-template: 1fr / 5fr 1fr 1fr 1fr;
|
|
|
|
}
|
2023-01-10 20:10:39 +01:00
|
|
|
|
|
|
|
.result {
|
2023-01-11 20:41:15 +01:00
|
|
|
display: grid;
|
|
|
|
grid-template: 1fr auto / 1fr;
|
2023-01-10 20:10:39 +01:00
|
|
|
gap: 0.5rem;
|
|
|
|
}
|
2023-01-11 20:41:15 +01:00
|
|
|
.result > .grid {
|
|
|
|
overflow: auto;
|
2023-01-10 20:10:39 +01:00
|
|
|
}
|
|
|
|
.result > .controls {
|
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
|
|
|
align-items: center;
|
|
|
|
}
|
2023-01-10 17:28:27 +01:00
|
|
|
</style>
|