diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte index 62cd7e413e..a2d7825fb5 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte @@ -35,7 +35,7 @@ import JSONSchemaModal from "./JSONSchemaModal.svelte" import { ValidColumnNameRegex } from "@budibase/shared-core" import { admin } from "stores/portal" - import { FieldType } from "@budibase/types" + import { FieldSubtype, FieldType } from "@budibase/types" const AUTO_TYPE = "auto" const FORMULA_TYPE = FIELDS.FORMULA.type @@ -44,6 +44,11 @@ const NUMBER_TYPE = FIELDS.NUMBER.type const JSON_TYPE = FIELDS.JSON.type const DATE_TYPE = FIELDS.DATETIME.type + const BB_REFERENCE_TYPE = FieldType.BB_REFERENCE + const BB_USER_REFERENCE_TYPE = composeType( + BB_REFERENCE_TYPE, + FieldSubtype.USER + ) const dispatch = createEventDispatcher() const PROHIBITED_COLUMN_NAMES = ["type", "_id", "_rev", "tableId"] @@ -77,11 +82,15 @@ delete fieldDefinitions.USER } + function composeType(fieldType, subtype) { + return `${fieldType}_${subtype}` + } + // Handling fields with subtypes fieldDefinitions = Object.entries(fieldDefinitions).reduce( (p, [key, field]) => { - if (field.type === FieldType.BB_REFERENCE) { - const composedType = `${field.type}_${field.subtype}` + if (field.type === BB_REFERENCE_TYPE) { + const composedType = composeType(field.type, field.subtype) p[key] = { ...field, type: composedType, @@ -142,6 +151,8 @@ $: initialiseField(field, savingColumn) + $: isBBReference = !!bbRefTypeMapping[editableColumn.type] + $: checkConstraints(editableColumn) $: required = !!editableColumn?.constraints?.presence || primaryDisplay $: uneditable = @@ -303,9 +314,10 @@ // Default relationships many to many if (editableColumn.type === LINK_TYPE) { editableColumn.relationshipType = RelationshipType.MANY_TO_MANY - } - if (editableColumn.type === FORMULA_TYPE) { + } else if (editableColumn.type === FORMULA_TYPE) { editableColumn.formulaType = "dynamic" + } else if (editableColumn.type === BB_USER_REFERENCE_TYPE) { + editableColumn.relationshipType = RelationshipType.ONE_TO_MANY } } @@ -666,6 +678,17 @@ Open schema editor + {:else if isBBReference} + + (editableColumn.relationshipType = e.detail + ? RelationshipType.MANY_TO_MANY + : RelationshipType.ONE_TO_MANY)} + disabled={!isCreating} + thin + text="Allow multiple users" + /> {/if} {#if editableColumn.type === AUTO_TYPE || editableColumn.autocolumn} + import { getContext } from "svelte" + import RelationshipCell from "./RelationshipCell.svelte" + import { FieldSubtype } from "@budibase/types" + + const { API } = getContext("grid") + const { subtype } = $$props.schema + + const schema = { + ...$$props.schema, + // This is not really used, just adding some content to be able to render the relationship cell + tableId: "external", + } + + async function searchFunction(searchParams) { + if (subtype !== FieldSubtype.USER) { + throw `Search for '${subtype}' not implemented` + } + + const results = await API.searchUsers({ + ...searchParams, + }) + return { + ...results, + data: undefined, + rows: results.data, + } + } + + + diff --git a/packages/frontend-core/src/components/grid/cells/RelationshipCell.svelte b/packages/frontend-core/src/components/grid/cells/RelationshipCell.svelte index ac7869bc4b..925c840478 100644 --- a/packages/frontend-core/src/components/grid/cells/RelationshipCell.svelte +++ b/packages/frontend-core/src/components/grid/cells/RelationshipCell.svelte @@ -21,6 +21,8 @@ import { Icon, Input, ProgressCircle, clickOutside } from "@budibase/bbui" import { debounce } from "../../../utils/utils" + const { API, dispatch } = getContext("grid") + export let value export let api export let readonly @@ -30,15 +32,15 @@ export let invertX = false export let invertY = false export let contentLines = 1 + export let searchFunction = API.searchTable + export let primaryDisplay - const { API, dispatch } = getContext("grid") const color = getColor(0) let isOpen = false let searchResults let searchString let lastSearchString - let primaryDisplay let candidateIndex let lastSearchId let searching = false @@ -96,7 +98,7 @@ lastSearchId = Math.random() searching = true const thisSearchId = lastSearchId - const results = await API.searchTable({ + const results = await searchFunction({ paginate: false, tableId: schema.tableId, limit: 20, @@ -259,14 +261,16 @@ on:wheel={e => (focused ? e.stopPropagation() : null)} > {#each value || [] as relationship} - {#if relationship.primaryDisplay} + {#if relationship[primaryDisplay] || relationship.primaryDisplay} showRelationship(relationship._id) : null} > - {readable(relationship.primaryDisplay)} + {readable( + relationship[primaryDisplay] || relationship.primaryDisplay + )} {#if editable} { return TypeComponentMap[column?.schema?.type] || TextCell diff --git a/packages/frontend-core/src/components/grid/lib/utils.js b/packages/frontend-core/src/components/grid/lib/utils.js index feff3da625..9383f69f66 100644 --- a/packages/frontend-core/src/components/grid/lib/utils.js +++ b/packages/frontend-core/src/components/grid/lib/utils.js @@ -19,7 +19,7 @@ const TypeIconMap = { formula: "Calculator", json: "Brackets", bigint: "TagBold", - internal: { + bb_reference: { user: "User", }, }