Flatten JSON schema in apps to allow filtering and display of nested values

This commit is contained in:
Andrew Kingston 2021-12-06 12:37:50 +00:00
parent 2a814abd53
commit 1bb6fb37e5
5 changed files with 31 additions and 20 deletions

View File

@ -4,6 +4,7 @@
import CellRenderer from "./CellRenderer.svelte" import CellRenderer from "./CellRenderer.svelte"
import SelectEditRenderer from "./SelectEditRenderer.svelte" import SelectEditRenderer from "./SelectEditRenderer.svelte"
import { cloneDeep } from "lodash" import { cloneDeep } from "lodash"
import { deepGet } from "../utils/helpers"
/** /**
* The expected schema is our normal couch schemas for our tables. * The expected schema is our normal couch schemas for our tables.
@ -318,7 +319,7 @@
{customRenderers} {customRenderers}
{row} {row}
schema={schema[field]} schema={schema[field]}
value={row[field]} value={deepGet(row, field)}
on:clickrelationship on:clickrelationship
> >
<slot /> <slot />

View File

@ -76,3 +76,6 @@ export { default as clickOutside } from "./Actions/click_outside"
// Stores // Stores
export { notifications, createNotificationStore } from "./Stores/notifications" export { notifications, createNotificationStore } from "./Stores/notifications"
// Utils
export * from "./utils/helpers"

View File

@ -6,3 +6,27 @@ export const generateID = () => {
} }
export const capitalise = s => s.substring(0, 1).toUpperCase() + s.substring(1) export const capitalise = s => s.substring(0, 1).toUpperCase() + s.substring(1)
/**
* Gets a key within an object. The key supports dot syntax for retrieving deep
* fields - e.g. "a.b.c".
* Exact matches of keys with dots in them take precedence over nested keys of
* the same path - e.g. getting "a.b" from { "a.b": "foo", a: { b: "bar" } }
* will return "foo" over "bar".
* @param obj the object
* @param key the key
*/
export const deepGet = (obj, key) => {
if (!obj || !key) {
return null
}
if (obj[key] != null) {
return obj[key]
}
const split = key.split(".")
let value = obj
for (let i = 0; i < split.length; i++) {
value = value?.[split[i]]
}
return value
}

View File

@ -1,4 +1,5 @@
import { NoEmptyFilterStrings } from "../constants/lucene" import { NoEmptyFilterStrings } from "../constants/lucene"
import { deepGet } from "@budibase/bbui"
/** /**
* Removes any fields that contain empty strings that would cause inconsistent * Removes any fields that contain empty strings that would cause inconsistent
@ -205,21 +206,3 @@ export const luceneLimit = (docs, limit) => {
} }
return docs.slice(0, numLimit) return docs.slice(0, numLimit)
} }
/**
* Gets a key within an object. The key supports dot syntax for retrieving deep
* fields - e.g. "a.b.c".
* @param obj the object
* @param key the key
*/
const deepGet = (obj, key) => {
if (!obj || !key) {
return null
}
const split = key.split(".")
let value = obj
for (let i = 0; i < split.length; i++) {
value = value?.[split[i]]
}
return value
}

View File

@ -85,7 +85,7 @@ export const fetchDatasourceSchema = async dataSource => {
for (let i = 0; i < keysToSchema.length; i++) { for (let i = 0; i < keysToSchema.length; i++) {
schema = schema[keysToSchema[i]].schema schema = schema[keysToSchema[i]].schema
} }
return convertJSONSchemaToTableSchema(schema) return convertJSONSchemaToTableSchema(schema, true)
} }
// Tables, views and links can be fetched by table ID // Tables, views and links can be fetched by table ID