From 569af97664a60aceabd3ad8345c15301c8a2038a Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 1 Jul 2021 21:24:23 +0100 Subject: [PATCH] preventing relationship overrides --- .../CreateEditRelationship.svelte | 113 ++++++------------ .../[selectedDatasource]/index.svelte | 9 +- packages/server/.vscode/launch.json | 9 -- packages/server/src/integrations/postgres.ts | 19 ++- 4 files changed, 58 insertions(+), 92 deletions(-) diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/CreateEditRelationship/CreateEditRelationship.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/CreateEditRelationship/CreateEditRelationship.svelte index a30c08c135..902d8fa4e2 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/CreateEditRelationship/CreateEditRelationship.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/CreateEditRelationship/CreateEditRelationship.svelte @@ -8,10 +8,10 @@ export let from export let plusTables export let relationship = {} + export let close let originalName = relationship.name - $: console.log(relationship) $: tableOptions = plusTables.map(table => ({ label: table.name, value: table._id })) $: valid = relationship.name && relationship.tableId && relationship.relationshipType $: from = plusTables.find(table => table._id === relationship.source) @@ -19,18 +19,6 @@ $: through = plusTables.find(table => table._id === relationship.through) $: linkTable = through || to - $: relationshipOptions = from && to ? [ - { - name: `Many ${from.name} rows → many ${to.name} rows`, - alt: `Many ${from.name} rows → many ${to.name} rows`, - value: RelationshipTypes.MANY_TO_MANY, - }, - { - name: `One ${from.name} row → many ${to.name} rows`, - alt: `One ${from.name} rows → many ${to.name} rows`, - value: RelationshipTypes.ONE_TO_MANY, - } - ] : [] $: relationshipTypes = [ { @@ -51,25 +39,44 @@ // save the relationship on to the datasource async function saveRelationship() { + const manyToMany = relationship.relationshipType === RelationshipTypes.MANY_TO_MANY // source of relationship datasource.entities[from.name].schema[relationship.name] = { type: "link", ...relationship } - // if (originalName !== from.name) { - // delete datasource.entities[from.name].schema[originalName] - // } - // save other side of relationship in the other schema datasource.entities[to.name].schema[relationship.name] = { + name: relationship.name, type: "link", - relationshipType: relationship.relationshipType === RelationshipTypes.MANY_TO_MANY ? RelationshipTypes.MANY_TO_MANY : RelationshipTypes.MANY_TO_ONE, - tableId: to._id + relationshipType: manyToMany ? RelationshipTypes.MANY_TO_MANY : RelationshipTypes.MANY_TO_ONE, + tableId: from._id, + fieldName: relationship.fieldName, + foreignKey: relationship.foreignKey } + // If relationship has been renamed + if (originalName !== relationship.name) { + delete datasource.entities[from.name].schema[originalName] + delete datasource.entities[to.name].schema[originalName] + } + + console.log({ + from: datasource.entities[from.name].schema[relationship.name], + to: datasource.entities[to.name].schema[relationship.name], + }) + await save() await tables.fetch() } + + async function deleteRelationship() { + delete datasource.entities[from.name].schema[relationship.name] + delete datasource.entities[to.name].schema[relationship.name] + await save() + await tables.fetch() + close() + } {/if} - {#if relationship?.relationshipType === RelationshipTypes.ONE_TO_MANY} + {#if relationship?.relationshipType === RelationshipTypes.ONE_TO_MANY && to}