From 5a4398f690a688baac0223cfd80d10f199cb4425 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 28 Jun 2021 17:42:39 +0100 Subject: [PATCH] Some work towards supporting many SQL relationships. --- .../DataTable/modals/CreateEditColumn.svelte | 2 +- .../src/api/controllers/row/external.js | 28 +++++++++++-------- packages/string-templates/src/index.cjs | 7 +---- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte index 306c1cc90a..35722481db 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte @@ -53,7 +53,7 @@ let deletion $: tableOptions = $tables.list.filter( - table => table._id !== $tables.draft._id + table => table._id !== $tables.draft._id && table.type !== "external" ) $: required = !!field?.constraints?.presence || primaryDisplay $: uneditable = diff --git a/packages/server/src/api/controllers/row/external.js b/packages/server/src/api/controllers/row/external.js index a5180a1436..879e576746 100644 --- a/packages/server/src/api/controllers/row/external.js +++ b/packages/server/src/api/controllers/row/external.js @@ -3,7 +3,6 @@ const { DataSourceOperation, SortDirection, FieldTypes, - RelationshipTypes, } = require("../../../constants") const { getAllExternalTables } = require("../table/utils") const { @@ -137,22 +136,29 @@ function buildFilters(id, filters, table) { } } -function buildRelationships(table) { +function buildRelationships(table, allTables) { const relationships = [] for (let [fieldName, field] of Object.entries(table.schema)) { if (field.type !== FieldTypes.LINK) { continue } - // TODO: through field - if (field.relationshipType === RelationshipTypes.MANY_TO_MANY) { + const { tableName: linkTableName } = breakExternalTableId(field.tableId) + const linkTable = allTables.find(table => table._id === field.tableId) + // no table to link to, this is not a valid relationships + if (!linkTable) { continue } - const broken = breakExternalTableId(field.tableId) - relationships.push({ - from: fieldName, - to: field.fieldName, - tableName: broken.tableName, - }) + const definition = { + from: fieldName || table.primary, + to: field.fieldName || linkTable.primary, + tableName: linkTableName, + through: undefined, + } + if (field.through) { + const { tableName: throughTableName } = breakExternalTableId(field.through) + definition.through = throughTableName + } + relationships.push(definition) } return relationships } @@ -171,7 +177,7 @@ async function handleRequest( } // clean up row on ingress using schema filters = buildFilters(id, filters, table) - const relationships = buildRelationships(table) + const relationships = buildRelationships(table, tables) row = inputProcessing(row, table) if ( operation === DataSourceOperation.DELETE && diff --git a/packages/string-templates/src/index.cjs b/packages/string-templates/src/index.cjs index 7dbd788819..573ea76f12 100644 --- a/packages/string-templates/src/index.cjs +++ b/packages/string-templates/src/index.cjs @@ -1,12 +1,7 @@ const handlebars = require("handlebars") const { registerAll } = require("./helpers/index") const processors = require("./processors") -const { cloneDeep } = require("lodash/fp") -const { - removeNull, - updateContext, - removeHandlebarsStatements, -} = require("./utilities") +const { removeHandlebarsStatements } = require("./utilities") const manifest = require("../manifest.json") const hbsInstance = handlebars.create()