Adding dynamic filter capabilities, also updating search field select in builder to make sure it removes banned search field types.

This commit is contained in:
mike12345567 2022-01-17 16:48:49 +00:00
parent 54ce8d8386
commit cde801d99b
5 changed files with 45 additions and 31 deletions

View File

@ -7,7 +7,7 @@
import { currentAsset } from "builderStore" import { currentAsset } from "builderStore"
import { tables } from "stores/backend" import { tables } from "stores/backend"
import { createEventDispatcher } from "svelte" import { createEventDispatcher } from "svelte"
import { getTableFields } from "helpers/searchFields" import { getFields } from "helpers/searchFields"
export let componentInstance = {} export let componentInstance = {}
export let value = "" export let value = ""
@ -20,19 +20,14 @@
$: boundValue = getSelectedOption(value, options) $: boundValue = getSelectedOption(value, options)
function getOptions(ds, dsSchema) { function getOptions(ds, dsSchema) {
let base = Object.keys(dsSchema) let base = Object.values(dsSchema)
if (!ds?.tableId) { if (!ds?.tableId) {
return base return base
} }
const currentTable = $tables.list.find(table => table._id === ds.tableId) const currentTable = $tables.list.find(table => table._id === ds.tableId)
if (currentTable && currentTable.sql) { return getFields(base, { allowLinks: currentTable.sql }).map(
for (let column of Object.values(currentTable.schema)) { field => field.name
if (column.type === "link") { )
base = base.concat(getTableFields(column).map(field => field.name))
}
}
}
return base
} }
function getSelectedOption(selectedOptions, allOptions) { function getSelectedOption(selectedOptions, allOptions) {

View File

@ -17,15 +17,15 @@ export function getTableFields(linkField) {
} }
export function getFields(fields, { allowLinks } = { allowLinks: true }) { export function getFields(fields, { allowLinks } = { allowLinks: true }) {
let fieldNames = fields.filter( let filteredFields = fields.filter(
field => !BannedSearchTypes.includes(field.type) field => !BannedSearchTypes.includes(field.type)
) )
if (allowLinks) { if (allowLinks) {
const linkFields = fields.filter(field => field.type === "link") const linkFields = fields.filter(field => field.type === "link")
for (let linkField of linkFields) { for (let linkField of linkFields) {
// only allow one depth of SQL relationship filtering // only allow one depth of SQL relationship filtering
fieldNames = fieldNames.concat(getTableFields(linkField)) filteredFields = filteredFields.concat(getTableFields(linkField))
} }
} }
return fieldNames return filteredFields
} }

View File

@ -67,6 +67,7 @@
$: dataContext = { $: dataContext = {
rows: $fetch.rows, rows: $fetch.rows,
info: $fetch.info, info: $fetch.info,
datasource: dataSource || {},
schema: $fetch.schema, schema: $fetch.schema,
rowsLength: $fetch.rows.length, rowsLength: $fetch.rows.length,

View File

@ -11,11 +11,14 @@
export let size = "M" export let size = "M"
const component = getContext("component") const component = getContext("component")
const { builderStore, ActionTypes, getAction } = getContext("sdk") const { builderStore, ActionTypes, getAction, fetchDatasourceSchema } =
getContext("sdk")
let modal let modal
let tmpFilters = [] let tmpFilters = []
let filters = [] let filters = []
let schemaLoaded = false,
schema
$: dataProviderId = dataProvider?.id $: dataProviderId = dataProvider?.id
$: addExtension = getAction( $: addExtension = getAction(
@ -26,7 +29,7 @@
dataProviderId, dataProviderId,
ActionTypes.RemoveDataProviderQueryExtension ActionTypes.RemoveDataProviderQueryExtension
) )
$: schema = dataProvider?.schema $: fetchSchema(dataProvider || {})
$: schemaFields = getSchemaFields(schema, allowedFields) $: schemaFields = getSchemaFields(schema, allowedFields)
// Add query extension to data provider // Add query extension to data provider
@ -39,7 +42,20 @@
} }
} }
const getSchemaFields = (schema, allowedFields) => { async function fetchSchema(dataProvider) {
const datasource = dataProvider?.datasource
if (datasource) {
schema = await fetchDatasourceSchema(datasource, {
enrichRelationships: true,
})
}
schemaLoaded = true
}
function getSchemaFields(schema, allowedFields) {
if (!schemaLoaded) {
return {}
}
let clonedSchema = {} let clonedSchema = {}
if (!allowedFields?.length) { if (!allowedFields?.length) {
clonedSchema = schema clonedSchema = schema
@ -68,18 +84,20 @@
}) })
</script> </script>
<Button {#if schemaLoaded}
onClick={openEditor} <Button
icon="Properties" onClick={openEditor}
text="Filter" icon="Properties"
{size} text="Filter"
type="secondary" {size}
quiet type="secondary"
active={filters?.length > 0} quiet
/> active={filters?.length > 0}
/>
<Modal bind:this={modal}> <Modal bind:this={modal}>
<ModalContent title="Edit filters" size="XL" onConfirm={updateQuery}> <ModalContent title="Edit filters" size="XL" onConfirm={updateQuery}>
<FilterModal bind:filters={tmpFilters} {schemaFields} /> <FilterModal bind:filters={tmpFilters} {schemaFields} />
</ModalContent> </ModalContent>
</Modal> </Modal>
{/if}

View File

@ -58,7 +58,7 @@ export const fetchDatasourceSchema = async (
schema = { ...schema, ...jsonAdditions } schema = { ...schema, ...jsonAdditions }
// Check for any relationship fields if required // Check for any relationship fields if required
if (options?.enrichRelationships) { if (options?.enrichRelationships && definition.sql) {
let relationshipAdditions = {} let relationshipAdditions = {}
for (let fieldKey of Object.keys(schema)) { for (let fieldKey of Object.keys(schema)) {
const fieldSchema = schema[fieldKey] const fieldSchema = schema[fieldKey]