From 15bec7aae858f66447b28b1024f4fa54dad3855f Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 2 Oct 2023 14:51:48 +0200 Subject: [PATCH 1/5] Refresh relationships on column link add/edit/delete --- .../backend/DataTable/modals/CreateEditColumn.svelte | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte index 1c64dfe5ec..c67ce67d57 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte @@ -277,10 +277,7 @@ dispatch("updatecolumns") gridDispatch("close-edit-column") - if ( - saveColumn.type === LINK_TYPE && - saveColumn.relationshipType === RelationshipType.MANY_TO_MANY - ) { + if (saveColumn.type === LINK_TYPE) { // Fetching the new tables tables.fetch() // Fetching the new relationships @@ -312,6 +309,11 @@ confirmDeleteDialog.hide() dispatch("updatecolumns") gridDispatch("close-edit-column") + + if (editableColumn.type === LINK_TYPE) { + // Updating the relationships + datasources.fetch() + } } } catch (error) { notifications.error(`Error deleting column: ${error.message}`) From 46c40f011e3297e16844ce1005bfc6d784794ce1 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 2 Oct 2023 15:00:51 +0200 Subject: [PATCH 2/5] Fix multiple relationship display --- .../_components/panels/Relationships.svelte | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/Relationships.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/Relationships.svelte index 384b87e11d..9b613c57e7 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/Relationships.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/Relationships.svelte @@ -21,15 +21,21 @@ function getRelationships(tables) { const relatedColumns = {} - tables.forEach(({ name: tableName, schema }) => { + tables.forEach(({ name: tableName, schema, _id: tableId }) => { Object.values(schema).forEach(column => { if (column.type !== "link") return - relatedColumns[column._id] ??= {} - relatedColumns[column._id].through = - relatedColumns[column._id].through || column.through + const columnId = + column.through || + (column.main + ? `${tableId}_${column.fieldName}__${column.tableId}_${column.foreignKey}` + : `${column.tableId}_${column.foreignKey}__${tableId}_${column.fieldName}`) - relatedColumns[column._id][column.main ? "from" : "to"] = { + relatedColumns[columnId] ??= {} + relatedColumns[columnId].through = + relatedColumns[columnId].through || column.through + + relatedColumns[columnId][column.main ? "from" : "to"] = { ...column, tableName, } From aa0ec1c7cbc6d5395b6db9fd49082c7e1974d67f Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 2 Oct 2023 15:12:35 +0200 Subject: [PATCH 3/5] Fix validating on relationshipType change --- .../backend/Datasources/CreateEditRelationship.svelte | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte index 9c98bdc2e5..aa07fb630c 100644 --- a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte +++ b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte @@ -57,7 +57,8 @@ label: table.name, value: table._id, })) - $: valid = getErrorCount(errors) === 0 && allRequiredAttributesSet() + $: valid = + getErrorCount(errors) === 0 && allRequiredAttributesSet(relationshipType) $: isManyToMany = relationshipType === RelationshipType.MANY_TO_MANY $: isManyToOne = relationshipType === RelationshipType.MANY_TO_ONE @@ -114,7 +115,7 @@ return Object.entries(errors).filter(entry => !!entry[1]).length } - function allRequiredAttributesSet() { + function allRequiredAttributesSet(relationshipType) { const base = getTable(fromId) && getTable(toId) && fromColumn && toColumn if (relationshipType === RelationshipType.MANY_TO_ONE) { return base && fromPrimary && fromForeign From 3546dfc0cf7b24f0153535f2d0b456917a973518 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 2 Oct 2023 15:18:28 +0200 Subject: [PATCH 4/5] Validate --- .../backend/Datasources/CreateEditRelationship.svelte | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte index aa07fb630c..9e42dfecd9 100644 --- a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte +++ b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte @@ -125,9 +125,10 @@ } function validate() { - if (!allRequiredAttributesSet() && !hasValidated) { + if (!allRequiredAttributesSet(relationshipType) && !hasValidated) { return } + hasValidated = true errorChecker.setType(relationshipType) const fromTable = getTable(fromId), From 2df32b7b28ed76be6fe4bea6b7de6b4721c2303d Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 2 Oct 2023 15:34:26 +0200 Subject: [PATCH 5/5] Use _id if populated --- .../[datasourceId]/_components/panels/Relationships.svelte | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/Relationships.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/Relationships.svelte index 9b613c57e7..1a46ecb540 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/Relationships.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/Relationships.svelte @@ -27,6 +27,7 @@ const columnId = column.through || + column._id || (column.main ? `${tableId}_${column.fieldName}__${column.tableId}_${column.foreignKey}` : `${column.tableId}_${column.foreignKey}__${tableId}_${column.fieldName}`)