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

View File

@ -17,15 +17,15 @@ export function getTableFields(linkField) {
}
export function getFields(fields, { allowLinks } = { allowLinks: true }) {
let fieldNames = fields.filter(
let filteredFields = fields.filter(
field => !BannedSearchTypes.includes(field.type)
)
if (allowLinks) {
const linkFields = fields.filter(field => field.type === "link")
for (let linkField of linkFields) {
// 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 = {
rows: $fetch.rows,
info: $fetch.info,
datasource: dataSource || {},
schema: $fetch.schema,
rowsLength: $fetch.rows.length,

View File

@ -11,11 +11,14 @@
export let size = "M"
const component = getContext("component")
const { builderStore, ActionTypes, getAction } = getContext("sdk")
const { builderStore, ActionTypes, getAction, fetchDatasourceSchema } =
getContext("sdk")
let modal
let tmpFilters = []
let filters = []
let schemaLoaded = false,
schema
$: dataProviderId = dataProvider?.id
$: addExtension = getAction(
@ -26,7 +29,7 @@
dataProviderId,
ActionTypes.RemoveDataProviderQueryExtension
)
$: schema = dataProvider?.schema
$: fetchSchema(dataProvider || {})
$: schemaFields = getSchemaFields(schema, allowedFields)
// 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 = {}
if (!allowedFields?.length) {
clonedSchema = schema
@ -68,6 +84,7 @@
})
</script>
{#if schemaLoaded}
<Button
onClick={openEditor}
icon="Properties"
@ -83,3 +100,4 @@
<FilterModal bind:filters={tmpFilters} {schemaFields} />
</ModalContent>
</Modal>
{/if}

View File

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