2020-12-19 07:19:43 +13:00
|
|
|
<script>
|
2021-03-18 00:40:24 +13:00
|
|
|
import { goto } from "@roxi/routify"
|
2020-12-19 07:19:43 +13:00
|
|
|
import {
|
|
|
|
Select,
|
|
|
|
Button,
|
2021-02-19 05:58:10 +13:00
|
|
|
Body,
|
2020-12-19 07:19:43 +13:00
|
|
|
Label,
|
|
|
|
Input,
|
|
|
|
Heading,
|
|
|
|
Spacer,
|
|
|
|
Switcher,
|
|
|
|
} from "@budibase/bbui"
|
2021-04-09 22:02:53 +12:00
|
|
|
import { notifications } from "@budibase/bbui"
|
2020-12-19 07:19:43 +13:00
|
|
|
import api from "builderStore/api"
|
|
|
|
import IntegrationQueryEditor from "components/integration/index.svelte"
|
2021-01-08 02:13:46 +13:00
|
|
|
import ExternalDataSourceTable from "components/backend/DataTable/ExternalDataSourceTable.svelte"
|
2021-02-19 05:58:10 +13:00
|
|
|
import ParameterBuilder from "components/integration/QueryParameterBuilder.svelte"
|
2021-04-01 22:29:47 +13:00
|
|
|
import { datasources, integrations, queries } from "stores/backend"
|
2020-12-19 07:19:43 +13:00
|
|
|
|
|
|
|
const PREVIEW_HEADINGS = [
|
|
|
|
{
|
2021-01-08 02:13:46 +13:00
|
|
|
title: "JSON",
|
|
|
|
key: "JSON",
|
2020-12-19 07:19:43 +13:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Schema",
|
|
|
|
key: "SCHEMA",
|
|
|
|
},
|
2021-01-08 02:13:46 +13:00
|
|
|
{
|
|
|
|
title: "Preview",
|
|
|
|
key: "PREVIEW",
|
|
|
|
},
|
2020-12-19 07:19:43 +13:00
|
|
|
]
|
|
|
|
|
|
|
|
export let query
|
|
|
|
export let fields = []
|
|
|
|
|
2021-01-08 02:13:46 +13:00
|
|
|
let tab = "JSON"
|
|
|
|
let parameters
|
2021-01-16 06:29:46 +13:00
|
|
|
let data = []
|
2020-12-19 07:19:43 +13:00
|
|
|
|
2021-04-01 22:29:47 +13:00
|
|
|
$: datasource = $datasources.list.find(ds => ds._id === query.datasourceId)
|
2021-01-07 01:28:51 +13:00
|
|
|
|
2020-12-19 07:19:43 +13:00
|
|
|
$: query.schema = fields.reduce(
|
|
|
|
(acc, next) => ({
|
|
|
|
...acc,
|
|
|
|
[next.name]: {
|
|
|
|
name: next.name,
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
|
2021-01-15 03:22:24 +13:00
|
|
|
$: datasourceType = datasource?.source
|
2021-01-16 02:42:55 +13:00
|
|
|
|
2021-03-23 01:23:36 +13:00
|
|
|
$: integrationInfo = $integrations[datasourceType]
|
2021-02-19 05:58:10 +13:00
|
|
|
$: queryConfig = integrationInfo?.query
|
2021-01-08 02:13:46 +13:00
|
|
|
|
2021-02-19 07:55:08 +13:00
|
|
|
$: shouldShowQueryConfig = queryConfig && query.queryVerb
|
2021-01-14 03:11:53 +13:00
|
|
|
|
2020-12-19 07:19:43 +13:00
|
|
|
function newField() {
|
|
|
|
fields = [...fields, {}]
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteField(idx) {
|
|
|
|
fields.splice(idx, 1)
|
|
|
|
fields = fields
|
|
|
|
}
|
|
|
|
|
|
|
|
async function previewQuery() {
|
|
|
|
try {
|
2021-01-07 01:28:51 +13:00
|
|
|
const response = await api.post(`/api/queries/preview`, {
|
2021-01-14 03:11:53 +13:00
|
|
|
fields: query.fields,
|
2021-01-13 05:49:11 +13:00
|
|
|
queryVerb: query.queryVerb,
|
2021-01-09 01:06:37 +13:00
|
|
|
parameters: query.parameters.reduce(
|
|
|
|
(acc, next) => ({
|
|
|
|
...acc,
|
|
|
|
[next.name]: next.default,
|
|
|
|
}),
|
|
|
|
{}
|
|
|
|
),
|
2021-01-07 01:28:51 +13:00
|
|
|
datasourceId: datasource._id,
|
2020-12-19 07:19:43 +13:00
|
|
|
})
|
|
|
|
const json = await response.json()
|
2021-01-08 02:13:46 +13:00
|
|
|
|
|
|
|
if (response.status !== 200) throw new Error(json.message)
|
|
|
|
|
2021-02-23 06:41:02 +13:00
|
|
|
data = json.rows || []
|
2020-12-19 07:19:43 +13:00
|
|
|
|
2021-01-16 06:29:46 +13:00
|
|
|
if (data.length === 0) {
|
2021-04-09 22:02:53 +12:00
|
|
|
notifications.info(
|
2021-01-19 04:40:26 +13:00
|
|
|
"Query results empty. Please execute a query with results to create your schema."
|
|
|
|
)
|
2021-01-16 06:29:46 +13:00
|
|
|
return
|
2021-01-19 04:40:26 +13:00
|
|
|
}
|
2021-01-16 06:29:46 +13:00
|
|
|
|
2021-04-09 22:02:53 +12:00
|
|
|
notifications.success("Query executed successfully.")
|
2021-01-16 06:29:46 +13:00
|
|
|
|
2021-02-23 06:41:02 +13:00
|
|
|
// Assume all the fields are strings and create a basic schema from the
|
|
|
|
// unique fields returned by the server
|
|
|
|
fields = json.schemaFields.map(field => ({
|
2020-12-19 07:19:43 +13:00
|
|
|
name: field,
|
|
|
|
type: "STRING",
|
|
|
|
}))
|
|
|
|
} catch (err) {
|
2021-04-09 22:02:53 +12:00
|
|
|
notifications.error(`Query Error: ${err.message}`)
|
2020-12-19 07:19:43 +13:00
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 02:13:46 +13:00
|
|
|
async function saveQuery() {
|
|
|
|
try {
|
2021-04-01 22:29:47 +13:00
|
|
|
const { _id } = await queries.save(query.datasourceId, query)
|
2021-04-09 22:02:53 +12:00
|
|
|
notifications.success(`Query saved successfully.`)
|
2021-01-15 03:22:24 +13:00
|
|
|
$goto(`../../${_id}`)
|
2021-01-08 02:13:46 +13:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2021-04-09 22:02:53 +12:00
|
|
|
notifications.error(`Error creating query. ${err.message}`)
|
2021-01-08 02:13:46 +13:00
|
|
|
}
|
|
|
|
}
|
2020-12-19 07:19:43 +13:00
|
|
|
</script>
|
|
|
|
|
2021-02-19 05:58:10 +13:00
|
|
|
<section class="config">
|
2021-02-20 03:31:07 +13:00
|
|
|
<Heading medium lh>Query {integrationInfo?.friendlyName}</Heading>
|
2021-02-19 05:58:10 +13:00
|
|
|
<hr />
|
2021-02-22 01:04:36 +13:00
|
|
|
<Spacer extraLarge />
|
2021-02-20 03:31:07 +13:00
|
|
|
<Heading small lh>Config</Heading>
|
2021-02-19 05:58:10 +13:00
|
|
|
<Body small grey>Provide a name for your query and select its function.</Body>
|
2021-02-22 01:04:36 +13:00
|
|
|
<Spacer large />
|
2021-02-19 05:58:10 +13:00
|
|
|
<div class="config-field">
|
|
|
|
<Label small>Query Name</Label>
|
2021-02-19 07:55:08 +13:00
|
|
|
<Input thin outline bind:value={query.name} />
|
2021-01-23 05:49:22 +13:00
|
|
|
</div>
|
2021-02-22 01:04:36 +13:00
|
|
|
<Spacer extraLarge />
|
2021-02-19 05:58:10 +13:00
|
|
|
{#if queryConfig}
|
|
|
|
<div class="config-field">
|
|
|
|
<Label small>Function</Label>
|
|
|
|
<Select primary outline thin bind:value={query.queryVerb}>
|
|
|
|
{#each Object.keys(queryConfig) as queryVerb}
|
2021-02-20 03:31:07 +13:00
|
|
|
<option value={queryVerb}>
|
|
|
|
{queryConfig[queryVerb]?.displayName || queryVerb}
|
|
|
|
</option>
|
2021-02-19 05:58:10 +13:00
|
|
|
{/each}
|
|
|
|
</Select>
|
2021-01-23 05:49:22 +13:00
|
|
|
</div>
|
2021-02-22 01:04:36 +13:00
|
|
|
<Spacer extraLarge />
|
2021-02-19 05:58:10 +13:00
|
|
|
<hr />
|
2021-02-22 01:04:36 +13:00
|
|
|
<Spacer extraLarge />
|
|
|
|
<Spacer small />
|
2021-02-19 05:58:10 +13:00
|
|
|
<ParameterBuilder bind:parameters={query.parameters} bindable={false} />
|
|
|
|
<hr />
|
2021-01-14 03:11:53 +13:00
|
|
|
{/if}
|
2021-02-19 05:58:10 +13:00
|
|
|
</section>
|
2020-12-19 07:19:43 +13:00
|
|
|
|
2021-01-14 03:11:53 +13:00
|
|
|
{#if shouldShowQueryConfig}
|
2021-01-13 05:49:11 +13:00
|
|
|
<section>
|
2021-02-22 01:04:36 +13:00
|
|
|
<Spacer extraLarge />
|
|
|
|
<Spacer small />
|
2021-01-13 05:49:11 +13:00
|
|
|
<div class="config">
|
2021-02-20 03:31:07 +13:00
|
|
|
<Heading small lh>Fields</Heading>
|
|
|
|
<Body small grey>Fill in the fields specific to this query.</Body>
|
2021-02-19 05:58:10 +13:00
|
|
|
<Spacer medium />
|
2021-02-22 01:04:36 +13:00
|
|
|
<Spacer extraLarge />
|
2021-01-14 03:11:53 +13:00
|
|
|
<IntegrationQueryEditor
|
2021-02-19 07:55:08 +13:00
|
|
|
{datasource}
|
2021-01-14 03:11:53 +13:00
|
|
|
{query}
|
2021-02-19 07:55:08 +13:00
|
|
|
schema={queryConfig[query.queryVerb]}
|
2021-01-16 02:42:55 +13:00
|
|
|
bind:parameters />
|
2021-02-22 01:04:36 +13:00
|
|
|
<Spacer extraLarge />
|
2021-02-19 05:58:10 +13:00
|
|
|
<hr />
|
2021-02-22 01:04:36 +13:00
|
|
|
<Spacer extraLarge />
|
2021-02-22 22:01:40 +13:00
|
|
|
<Spacer medium />
|
2021-01-13 05:49:11 +13:00
|
|
|
<div class="viewer-controls">
|
2021-02-22 22:01:40 +13:00
|
|
|
<Heading small lh>Results</Heading>
|
2021-02-20 01:07:37 +13:00
|
|
|
<div class="button-container">
|
2021-02-19 07:55:08 +13:00
|
|
|
<Button
|
|
|
|
secondary
|
|
|
|
thin
|
|
|
|
disabled={data.length === 0 || !query.name}
|
|
|
|
on:click={saveQuery}>
|
|
|
|
Save Query
|
|
|
|
</Button>
|
2021-02-20 01:07:37 +13:00
|
|
|
<Spacer medium />
|
|
|
|
<Button thin primary on:click={previewQuery}>Run Query</Button>
|
2021-02-19 07:55:08 +13:00
|
|
|
</div>
|
2021-01-13 05:49:11 +13:00
|
|
|
</div>
|
2021-02-19 05:58:10 +13:00
|
|
|
<Body small grey>
|
|
|
|
Below, you can preview the results from your query and change the
|
|
|
|
schema.
|
|
|
|
</Body>
|
2021-02-19 07:55:08 +13:00
|
|
|
|
2021-02-22 01:04:36 +13:00
|
|
|
<Spacer extraLarge />
|
2021-02-22 22:01:40 +13:00
|
|
|
<Spacer medium />
|
2021-01-08 02:13:46 +13:00
|
|
|
|
2021-01-13 05:49:11 +13:00
|
|
|
<section class="viewer">
|
|
|
|
{#if data}
|
|
|
|
<Switcher headings={PREVIEW_HEADINGS} bind:value={tab}>
|
|
|
|
{#if tab === 'JSON'}
|
2021-02-23 04:53:49 +13:00
|
|
|
<pre
|
|
|
|
class="preview">
|
|
|
|
<!-- prettier-ignore -->
|
2021-02-23 03:01:02 +13:00
|
|
|
{#if !data[0]}
|
2021-02-23 01:23:46 +13:00
|
|
|
|
2021-02-23 03:01:02 +13:00
|
|
|
Please run your query to fetch some data.
|
2021-02-23 01:28:43 +13:00
|
|
|
|
2021-02-19 05:58:10 +13:00
|
|
|
{:else}
|
|
|
|
{JSON.stringify(data[0], undefined, 2)}
|
|
|
|
{/if}
|
2021-02-16 07:41:56 +13:00
|
|
|
</pre>
|
2021-01-13 05:49:11 +13:00
|
|
|
{:else if tab === 'PREVIEW'}
|
|
|
|
<ExternalDataSourceTable {query} {data} />
|
|
|
|
{:else if tab === 'SCHEMA'}
|
|
|
|
{#each fields as field, idx}
|
2021-01-23 05:49:22 +13:00
|
|
|
<Spacer small />
|
2021-01-13 05:49:11 +13:00
|
|
|
<div class="field">
|
2021-01-28 06:29:30 +13:00
|
|
|
<Input
|
|
|
|
outline
|
|
|
|
placeholder="Field Name"
|
|
|
|
type={'text'}
|
|
|
|
bind:value={field.name} />
|
2021-01-23 05:49:22 +13:00
|
|
|
<Select thin border bind:value={field.type}>
|
|
|
|
<option value={''}>Select a field type</option>
|
2021-01-13 05:49:11 +13:00
|
|
|
<option value={'STRING'}>Text</option>
|
|
|
|
<option value={'NUMBER'}>Number</option>
|
|
|
|
<option value={'BOOLEAN'}>Boolean</option>
|
|
|
|
<option value={'DATETIME'}>Datetime</option>
|
|
|
|
</Select>
|
|
|
|
<i
|
|
|
|
class="ri-close-circle-line delete"
|
|
|
|
on:click={() => deleteField(idx)} />
|
|
|
|
</div>
|
|
|
|
{/each}
|
2021-01-28 06:29:30 +13:00
|
|
|
<Spacer small />
|
|
|
|
<Button thin secondary on:click={newField}>Add Field</Button>
|
2021-01-13 05:49:11 +13:00
|
|
|
{/if}
|
|
|
|
</Switcher>
|
|
|
|
{/if}
|
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
{/if}
|
2021-02-22 22:01:40 +13:00
|
|
|
<Spacer extraLarge />
|
|
|
|
<Spacer extraLarge />
|
2020-12-19 07:19:43 +13:00
|
|
|
|
|
|
|
<style>
|
2021-02-19 05:58:10 +13:00
|
|
|
.config-field {
|
|
|
|
display: grid;
|
|
|
|
grid-template-columns: 20% 1fr;
|
|
|
|
grid-gap: var(--spacing-l);
|
2021-01-23 05:49:22 +13:00
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
|
2020-12-19 07:19:43 +13:00
|
|
|
.field {
|
|
|
|
display: grid;
|
2021-02-19 05:58:10 +13:00
|
|
|
grid-template-columns: 1fr 1fr 5%;
|
2021-01-23 05:49:22 +13:00
|
|
|
gap: var(--spacing-l);
|
2020-12-19 07:19:43 +13:00
|
|
|
}
|
|
|
|
|
2021-02-20 01:07:37 +13:00
|
|
|
.button-container {
|
|
|
|
display: flex;
|
2021-01-19 06:01:41 +13:00
|
|
|
}
|
|
|
|
|
2021-02-19 07:55:08 +13:00
|
|
|
hr {
|
|
|
|
margin-top: var(--layout-m);
|
2021-02-22 01:04:36 +13:00
|
|
|
border: 1px solid var(--grey-2);
|
2021-01-19 06:01:41 +13:00
|
|
|
}
|
|
|
|
|
2020-12-19 07:19:43 +13:00
|
|
|
.config {
|
|
|
|
margin-bottom: var(--spacing-s);
|
|
|
|
}
|
|
|
|
|
|
|
|
.delete {
|
|
|
|
align-self: center;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
|
2021-02-22 22:01:40 +13:00
|
|
|
.viewer {
|
|
|
|
min-height: 200px;
|
|
|
|
}
|
|
|
|
|
2020-12-19 07:19:43 +13:00
|
|
|
.preview {
|
2021-01-08 02:13:46 +13:00
|
|
|
height: 100%;
|
2021-02-22 22:01:40 +13:00
|
|
|
min-height: 120px;
|
2021-01-08 02:13:46 +13:00
|
|
|
overflow-y: auto;
|
2020-12-19 07:19:43 +13:00
|
|
|
overflow-wrap: break-word;
|
|
|
|
white-space: pre-wrap;
|
2021-02-22 22:01:40 +13:00
|
|
|
background-color: var(--grey-1);
|
|
|
|
padding: var(--spacing-m);
|
|
|
|
border-radius: 8px;
|
|
|
|
color: var(--grey-6);
|
2020-12-19 07:19:43 +13:00
|
|
|
}
|
2021-01-08 02:13:46 +13:00
|
|
|
|
2021-01-23 05:49:22 +13:00
|
|
|
.viewer-controls {
|
2021-01-13 05:49:11 +13:00
|
|
|
display: flex;
|
2021-01-23 05:49:22 +13:00
|
|
|
flex-direction: row;
|
2021-02-19 07:55:08 +13:00
|
|
|
justify-content: space-between;
|
2021-01-23 05:49:22 +13:00
|
|
|
gap: var(--spacing-m);
|
|
|
|
min-width: 150px;
|
2021-02-20 03:31:07 +13:00
|
|
|
align-items: center;
|
2021-02-05 23:32:10 +13:00
|
|
|
}
|
2020-12-19 07:19:43 +13:00
|
|
|
</style>
|