diff --git a/packages/standard-components/src/RecordDetail.svelte b/packages/standard-components/src/RecordDetail.svelte index 95723782bd..aa9d39c0cc 100644 --- a/packages/standard-components/src/RecordDetail.svelte +++ b/packages/standard-components/src/RecordDetail.svelte @@ -8,6 +8,12 @@ let store = _bb.store let target + async function fetchModel(id) { + const FETCH_MODEL_URL = `/api/models/${id}` + const response = await _bb.api.get(FETCH_MODEL_URL) + return await response.json() + } + async function fetchFirstRecord() { const FETCH_RECORDS_URL = `/api/views/all_${model}` const response = await _bb.api.get(FETCH_RECORDS_URL) @@ -34,6 +40,14 @@ } if (record) { + // Fetch model schema so we can check for linked records + const model = await fetchModel(record.modelId) + for (let key of Object.keys(model.schema)) { + if (model.schema[key].type === "link") { + record[key] = Array.isArray(record[key]) ? record[key].length : 0 + } + } + _bb.attachChildren(target, { hydrate: false, context: record, diff --git a/packages/standard-components/src/fetchData.js b/packages/standard-components/src/fetchData.js index 7a1c4b5f7c..a0c50c233a 100644 --- a/packages/standard-components/src/fetchData.js +++ b/packages/standard-components/src/fetchData.js @@ -4,7 +4,28 @@ export default async function fetchData(datasource) { const { isModel, name } = datasource if (name) { - return isModel ? await fetchModelData() : await fetchViewData() + const records = isModel ? await fetchModelData() : await fetchViewData() + + // Fetch model schema so we can check for linked records + if (records && records.length) { + const model = await fetchModel(records[0].modelId) + const keys = Object.keys(model.schema) + records.forEach(record => { + for (let key of keys) { + if (model.schema[key].type === "link") { + record[key] = Array.isArray(record[key]) ? record[key].length : 0 + } + } + }) + } + + return records + } + + async function fetchModel(id) { + const FETCH_MODEL_URL = `/api/models/${id}` + const response = await api.get(FETCH_MODEL_URL) + return await response.json() } async function fetchModelData() {