Merge pull request #11895 from Budibase/feat/relationship-configuration

Fix issue where one to one relationships could be selected
This commit is contained in:
Peter Clement 2023-09-28 15:35:36 +01:00 committed by GitHub
commit bc9055dd8d
2 changed files with 50 additions and 22 deletions

View File

@ -62,7 +62,9 @@
let relationshipPart1 = PrettyRelationshipDefinitions.Many let relationshipPart1 = PrettyRelationshipDefinitions.Many
let relationshipPart2 = PrettyRelationshipDefinitions.One let relationshipPart2 = PrettyRelationshipDefinitions.One
let relationshipTableIdPrimary = null
let relationshipTableIdSecondary = null let relationshipTableIdSecondary = null
let table = $tables.selected let table = $tables.selected
let confirmDeleteDialog let confirmDeleteDialog
let savingColumn let savingColumn
@ -75,6 +77,8 @@
// Initial value for column name in other table for linked records // Initial value for column name in other table for linked records
fieldName: $tables.selected.name, fieldName: $tables.selected.name,
} }
let relationshipOpts1 = Object.values(PrettyRelationshipDefinitions)
let relationshipOpts2 = Object.values(PrettyRelationshipDefinitions)
$: if (primaryDisplay) { $: if (primaryDisplay) {
editableColumn.constraints.presence = { allowEmpty: false } editableColumn.constraints.presence = { allowEmpty: false }
@ -96,7 +100,25 @@
} }
$: { $: {
// this parses any changes the user has made when creating a new internal relationship
// into what we expect the schema to look like
if (editableColumn.type === LINK_TYPE) { if (editableColumn.type === LINK_TYPE) {
relationshipTableIdPrimary = table._id
if (relationshipPart1 === PrettyRelationshipDefinitions.ONE) {
relationshipOpts2 = relationshipOpts2.filter(
opt => opt !== PrettyRelationshipDefinitions.ONE
)
} else {
relationshipOpts2 = Object.values(PrettyRelationshipDefinitions)
}
if (relationshipPart2 === PrettyRelationshipDefinitions.ONE) {
relationshipOpts1 = relationshipOpts1.filter(
opt => opt !== PrettyRelationshipDefinitions.ONE
)
} else {
relationshipOpts1 = Object.values(PrettyRelationshipDefinitions)
}
// Determine the relationship type based on the selected values of both parts // Determine the relationship type based on the selected values of both parts
editableColumn.relationshipType = Object.entries(relationshipMap).find( editableColumn.relationshipType = Object.entries(relationshipMap).find(
([_, parts]) => ([_, parts]) =>
@ -106,7 +128,6 @@
editableColumn.tableId = relationshipTableIdSecondary editableColumn.tableId = relationshipTableIdSecondary
} }
} }
const initialiseField = (field, savingColumn) => { const initialiseField = (field, savingColumn) => {
isCreating = !field isCreating = !field
@ -121,6 +142,19 @@
if (editableColumn.type === FieldType.BB_REFERENCE) { if (editableColumn.type === FieldType.BB_REFERENCE) {
editableColumn.type = `${editableColumn.type}_${editableColumn.subtype}` editableColumn.type = `${editableColumn.type}_${editableColumn.subtype}`
} }
// Here we are setting the relationship values based on the editableColumn
// This part of the code is used when viewing an existing field hence the check
// for the tableId
if (editableColumn.type === LINK_TYPE && editableColumn.tableId) {
relationshipTableIdPrimary = table._id
relationshipTableIdSecondary = editableColumn.tableId
if (editableColumn.relationshipType in relationshipMap) {
const { part1, part2 } =
relationshipMap[editableColumn.relationshipType]
relationshipPart1 = part1
relationshipPart2 = part2
}
}
} else if (!savingColumn) { } else if (!savingColumn) {
let highestNumber = 0 let highestNumber = 0
Object.keys(table.schema).forEach(columnName => { Object.keys(table.schema).forEach(columnName => {
@ -139,16 +173,6 @@
} }
allowedTypes = getAllowedTypes() allowedTypes = getAllowedTypes()
if (editableColumn.type === LINK_TYPE && editableColumn.tableId) {
relationshipTableIdSecondary = editableColumn.tableId
if (editableColumn.relationshipType in relationshipMap) {
const { part1, part2 } =
relationshipMap[editableColumn.relationshipType]
relationshipPart1 = part1
relationshipPart2 = part2
}
}
} }
$: initialiseField(field, savingColumn) $: initialiseField(field, savingColumn)
@ -209,10 +233,7 @@
$: external = table.type === "external" $: external = table.type === "external"
// in the case of internal tables the sourceId will just be undefined // in the case of internal tables the sourceId will just be undefined
$: tableOptions = $tables.list.filter( $: tableOptions = $tables.list.filter(
opt => opt => opt.type === table.type && table.sourceId === opt.sourceId
opt._id !== $tables.selected._id &&
opt.type === table.type &&
table.sourceId === opt.sourceId
) )
$: typeEnabled = $: typeEnabled =
!originalName || !originalName ||
@ -582,9 +603,11 @@
<RelationshipSelector <RelationshipSelector
bind:relationshipPart1 bind:relationshipPart1
bind:relationshipPart2 bind:relationshipPart2
bind:relationshipTableIdPrimary={table.name} bind:relationshipTableIdPrimary
bind:relationshipTableIdSecondary bind:relationshipTableIdSecondary
bind:editableColumn bind:editableColumn
{relationshipOpts1}
{relationshipOpts2}
{linkEditDisabled} {linkEditDisabled}
{tableOptions} {tableOptions}
{errors} {errors}

View File

@ -1,6 +1,5 @@
<script> <script>
import { Select, Input } from "@budibase/bbui" import { Select, Input } from "@budibase/bbui"
import { PrettyRelationshipDefinitions } from "constants/backend"
export let relationshipPart1 export let relationshipPart1
export let relationshipPart2 export let relationshipPart2
@ -10,6 +9,8 @@
export let linkEditDisabled export let linkEditDisabled
export let tableOptions export let tableOptions
export let errors export let errors
export let relationshipOpts1
export let relationshipOpts2
</script> </script>
<div class="relationship-container"> <div class="relationship-container">
@ -17,15 +18,17 @@
<Select <Select
disabled={linkEditDisabled} disabled={linkEditDisabled}
bind:value={relationshipPart1} bind:value={relationshipPart1}
options={Object.values(PrettyRelationshipDefinitions)} options={relationshipOpts1}
/> />
</div> </div>
<div class="relationship-label">in</div> <div class="relationship-label">in</div>
<div class="relationship-part"> <div class="relationship-part">
<Select <Select
disabled disabled
options={[relationshipTableIdPrimary]} options={tableOptions}
value={relationshipTableIdPrimary} getOptionLabel={table => table.name}
getOptionValue={table => table._id}
bind:value={relationshipTableIdPrimary}
/> />
</div> </div>
</div> </div>
@ -34,7 +37,7 @@
<Select <Select
disabled={linkEditDisabled} disabled={linkEditDisabled}
bind:value={relationshipPart2} bind:value={relationshipPart2}
options={Object.values(PrettyRelationshipDefinitions)} options={relationshipOpts2}
getOptionLabel={option => "To " + option.toLowerCase()} getOptionLabel={option => "To " + option.toLowerCase()}
/> />
</div> </div>
@ -43,7 +46,9 @@
<Select <Select
disabled={linkEditDisabled} disabled={linkEditDisabled}
bind:value={relationshipTableIdSecondary} bind:value={relationshipTableIdSecondary}
options={tableOptions} options={tableOptions.filter(
table => table._id !== relationshipTableIdPrimary
)}
getOptionLabel={table => table.name} getOptionLabel={table => table.name}
getOptionValue={table => table._id} getOptionValue={table => table._id}
/> />