Fix relational data in client apps and use count for link fields in client apps

This commit is contained in:
Andrew Kingston 2020-10-12 14:21:15 +01:00
parent 959aa3ff60
commit 3c8502187f
4 changed files with 15 additions and 13 deletions

View File

@ -20,6 +20,7 @@
let sort = {} let sort = {}
let sorted = [] let sorted = []
let schema = {} let schema = {}
let store = _bb.store
$: cssVariables = { $: cssVariables = {
backgroundColor, backgroundColor,
@ -39,7 +40,7 @@
onMount(async () => { onMount(async () => {
if (!isEmpty(datasource)) { if (!isEmpty(datasource)) {
data = await fetchData(datasource, _bb) data = await fetchData(datasource, $store)
if (data && data.length) { if (data && data.length) {
await fetchModel(data[0].modelId) await fetchModel(data[0].modelId)
headers = Object.keys(schema).filter(shouldDisplayField) headers = Object.keys(schema).filter(shouldDisplayField)
@ -99,7 +100,7 @@
{#if schema[header].type === 'attachment'} {#if schema[header].type === 'attachment'}
<AttachmentList files={row[header]} /> <AttachmentList files={row[header]} />
{:else if schema[header].type === 'link'} {:else if schema[header].type === 'link'}
<td>{row[header]} related row(s)</td> <td>{row[header].length} related row(s)</td>
{:else} {:else}
<td>{row[header] == null ? '' : row[header]}</td> <td>{row[header] == null ? '' : row[header]}</td>
{/if} {/if}

View File

@ -7,10 +7,11 @@
export let datasource = [] export let datasource = []
let target let target
let store = _bb.store
onMount(async () => { onMount(async () => {
if (!isEmpty(datasource)) { if (!isEmpty(datasource)) {
const data = await fetchData(datasource, _bb) const data = await fetchData(datasource, $store)
_bb.attachChildren(target, { _bb.attachChildren(target, {
hydrate: false, hydrate: false,
context: data, context: data,

View File

@ -52,7 +52,9 @@
const modelObj = await fetchModel(record.modelId) const modelObj = await fetchModel(record.modelId)
for (let key of Object.keys(modelObj.schema)) { for (let key of Object.keys(modelObj.schema)) {
if (modelObj.schema[key].type === "link") { if (modelObj.schema[key].type === "link") {
record[key] = Array.isArray(record[key]) ? record[key].length : 0 record[`${key}_count`] = Array.isArray(record[key])
? record[key].length
: 0
} }
} }

View File

@ -1,10 +1,10 @@
import api from "./api" import api from "./api"
export default async function fetchData(datasource, _bb) { export default async function fetchData(datasource, store) {
const { type, name } = datasource const { type, name } = datasource
if (name) { if (name) {
let records let records = []
if (type === "model") { if (type === "model") {
records = await fetchModelData() records = await fetchModelData()
} else if (type === "view") { } else if (type === "view") {
@ -20,7 +20,9 @@ export default async function fetchData(datasource, _bb) {
records.forEach(record => { records.forEach(record => {
for (let key of keys) { for (let key of keys) {
if (model.schema[key].type === "link") { if (model.schema[key].type === "link") {
record[key] = Array.isArray(record[key]) ? record[key].length : 0 record[`${key}_count`] = Array.isArray(record[key])
? record[key].length
: 0
} }
} }
}) })
@ -62,14 +64,10 @@ export default async function fetchData(datasource, _bb) {
} }
async function fetchLinkedRecordsData() { async function fetchLinkedRecordsData() {
if ( if (!store || !store.data || !store.data._id) {
!_bb.store.state ||
!_bb.store.state.data ||
!_bb.store.state.data._id
) {
return [] return []
} }
const QUERY_URL = `/api/${_bb.store.state.data.modelId}/${_bb.store.state.data._id}/enrich` const QUERY_URL = `/api/${store.data.modelId}/${store.data._id}/enrich`
const response = await api.get(QUERY_URL) const response = await api.get(QUERY_URL)
const record = await response.json() const record = await response.json()
return record[datasource.fieldName] return record[datasource.fieldName]