From ac2c63f9edadd7561c5e6e4ea12fecb49e4e0f7f Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Thu, 28 Sep 2023 16:02:37 +0100 Subject: [PATCH 01/67] use Relationship selector for external datasource relationships --- .../Datasources/CreateEditRelationship.svelte | 103 ++++++++++-------- .../common/RelationshipSelector.svelte | 26 +++-- 2 files changed, 76 insertions(+), 53 deletions(-) diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte index 9c98bdc2e5..8b0cb5b7bb 100644 --- a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte +++ b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte @@ -13,6 +13,8 @@ import { Helpers } from "@budibase/bbui" import { RelationshipErrorChecker } from "./relationshipErrors" import { onMount } from "svelte" + import RelationshipSelector from "components/common/RelationshipSelector.svelte" + import { PrettyRelationshipDefinitions } from "constants/backend" export let save export let datasource @@ -22,16 +24,18 @@ export let selectedFromTable export let close - const relationshipTypes = [ - { - label: "One to Many", - value: RelationshipType.MANY_TO_ONE, + let relationshipMap = { + [RelationshipType.MANY_TO_MANY]: { + part1: PrettyRelationshipDefinitions.MANY, + part2: PrettyRelationshipDefinitions.MANY, }, - { - label: "Many to Many", - value: RelationshipType.MANY_TO_MANY, + [RelationshipType.MANY_TO_ONE]: { + part1: PrettyRelationshipDefinitions.ONE, + part2: PrettyRelationshipDefinitions.MANY, }, - ] + } + let relationshipOpts1 = Object.values(PrettyRelationshipDefinitions) + let relationshipOpts2 = Object.values(PrettyRelationshipDefinitions) let originalFromColumnName = toRelationship.name, originalToColumnName = fromRelationship.name @@ -49,14 +53,32 @@ ) let errors = {} let fromPrimary, fromForeign, fromColumn, toColumn - let fromId, toId, throughId, throughToKey, throughFromKey + $: fromId = null + $: toId = null + + let throughId, throughToKey, throughFromKey let isManyToMany, isManyToOne, relationshipType let hasValidated = false $: tableOptions = plusTables.map(table => ({ label: table.name, value: table._id, + name: table.name, + _id: table._id, })) + + $: { + // Determine the relationship type based on the selected values of both parts + relationshipType = Object.entries(relationshipMap).find( + ([_, parts]) => + parts.part1 === relationshipPart1 && parts.part2 === relationshipPart2 + )?.[0] + + changed(() => { + hasValidated = false + }) + } + $: console.log(relationshipType) $: valid = getErrorCount(errors) === 0 && allRequiredAttributesSet() $: isManyToMany = relationshipType === RelationshipType.MANY_TO_MANY $: isManyToOne = relationshipType === RelationshipType.MANY_TO_ONE @@ -328,6 +350,8 @@ fromPrimary = selectedFromTable?.primary[0] || null } }) + let relationshipPart1 = PrettyRelationshipDefinitions.MANY + let relationshipPart2 = PrettyRelationshipDefinitions.ONE - - changed(() => { - const table = plusTables.find(tbl => tbl._id === e.detail) - fromColumn = table?.name || "" - fromPrimary = table?.primary?.[0] - })} - /> - {/if} + + + changed(() => { + const table = plusTables.find(tbl => tbl._id === e.detail) + fromColumn = table?.name || "" + fromPrimary = table?.primary?.[0] + })} + secondaryTableChanged={e => + changed(() => { + const table = plusTables.find(tbl => tbl._id === e.detail) + toColumn = table.name || "" + fromForeign = null + })} + /> + {#if isManyToOne && fromId} - changed(() => { - const table = plusTables.find(tbl => tbl._id === e.detail) - toColumn = table.name || "" - fromForeign = null - })} - /> {#if isManyToMany} table.name} getOptionValue={table => table._id} bind:value={relationshipTableIdPrimary} + on:change={primaryTableChanged} + bind:error={errors.fromTable} /> @@ -46,20 +52,24 @@ +{#if editableColumn} + +{/if}