From 19fb11e69d920da4a45207d9e30df8d957424309 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 25 Feb 2021 11:05:15 +0000 Subject: [PATCH 1/5] 1-n-data-integrity --- packages/server/src/db/linkedRows/LinkController.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/server/src/db/linkedRows/LinkController.js b/packages/server/src/db/linkedRows/LinkController.js index d202c07d63..96074c0ef2 100644 --- a/packages/server/src/db/linkedRows/LinkController.js +++ b/packages/server/src/db/linkedRows/LinkController.js @@ -132,6 +132,14 @@ class LinkController { const rowField = row[fieldName] const field = table.schema[fieldName] if (field.type === FieldTypes.LINK && rowField != null) { + // if 1:N, ensure that this ID is not already attached to another record + const linkedTable = await this._db.get(field.tableId) + const linkedSchema = linkedTable.schema[field.fieldName] + + if (linkedSchema.relationshipType === "one-to-many") { + + } + // check which links actual pertain to the update in this row const thisFieldLinkDocs = linkDocs.filter( linkDoc => From bcaed4f4e4f6bf20b08b83afcad21f1b02729a01 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 25 Feb 2021 11:55:23 +0000 Subject: [PATCH 2/5] Server side data integrity for 1:N --- .../DataTable/modals/CreateEditRow.svelte | 6 ++++ .../src/db/linkedRows/LinkController.js | 33 ++++++++++++++----- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte index 5929b2db96..790a74df53 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte @@ -29,6 +29,12 @@ // Prevent modal closing if there were errors return false } + + if (rowResponse.status === 500) { + notifier.danger(rowResponse.message) + return false + } + notifier.success("Row saved successfully.") backendUiStore.actions.rows.save(rowResponse) } diff --git a/packages/server/src/db/linkedRows/LinkController.js b/packages/server/src/db/linkedRows/LinkController.js index 96074c0ef2..4535cb0274 100644 --- a/packages/server/src/db/linkedRows/LinkController.js +++ b/packages/server/src/db/linkedRows/LinkController.js @@ -132,14 +132,6 @@ class LinkController { const rowField = row[fieldName] const field = table.schema[fieldName] if (field.type === FieldTypes.LINK && rowField != null) { - // if 1:N, ensure that this ID is not already attached to another record - const linkedTable = await this._db.get(field.tableId) - const linkedSchema = linkedTable.schema[field.fieldName] - - if (linkedSchema.relationshipType === "one-to-many") { - - } - // check which links actual pertain to the update in this row const thisFieldLinkDocs = linkDocs.filter( linkDoc => @@ -151,6 +143,31 @@ class LinkController { ? linkDoc.doc2.rowId : linkDoc.doc1.rowId }) + + // if 1:N, ensure that this ID is not already attached to another record + const linkedTable = await this._db.get(field.tableId) + const linkedSchema = linkedTable.schema[field.fieldName] + + if (linkedSchema.relationshipType === "one-to-many") { + for (let linkId of rowField) { + const links = await getLinkDocuments({ + appId: this._appId, + tableId: field.tableId, + rowId: linkId, + includeDocs: IncludeDocs.INCLUDE, + }) + + // The 1 side of 1:N is already related to something else + // You must remove the existing relationship + if (links.length > 0) { + throw new Error( + `1:N Relationship Error: Record already linked to another.` + ) + } + console.log("ONE TO MANY") + } + } + // iterate through the link IDs in the row field, see if any don't exist already for (let linkId of rowField) { if (linkId && linkId !== "" && linkDocIds.indexOf(linkId) === -1) { From 89350c91d1f4098f6eaa61a34ed543fbbd473afe Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 25 Feb 2021 11:59:31 +0000 Subject: [PATCH 3/5] refactor --- packages/server/src/db/linkedRows/LinkController.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/server/src/db/linkedRows/LinkController.js b/packages/server/src/db/linkedRows/LinkController.js index 4535cb0274..9230c62e73 100644 --- a/packages/server/src/db/linkedRows/LinkController.js +++ b/packages/server/src/db/linkedRows/LinkController.js @@ -148,8 +148,9 @@ class LinkController { const linkedTable = await this._db.get(field.tableId) const linkedSchema = linkedTable.schema[field.fieldName] - if (linkedSchema.relationshipType === "one-to-many") { - for (let linkId of rowField) { + // iterate through the link IDs in the row field, see if any don't exist already + for (let linkId of rowField) { + if (linkedSchema.relationshipType === "one-to-many") { const links = await getLinkDocuments({ appId: this._appId, tableId: field.tableId, @@ -164,12 +165,8 @@ class LinkController { `1:N Relationship Error: Record already linked to another.` ) } - console.log("ONE TO MANY") } - } - // iterate through the link IDs in the row field, see if any don't exist already - for (let linkId of rowField) { if (linkId && linkId !== "" && linkDocIds.indexOf(linkId) === -1) { // first check the doc we're linking to exists try { From 2d75bc750be6230ae066221df34baf924357ab76 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 25 Feb 2021 12:21:24 +0000 Subject: [PATCH 4/5] relationship type constant --- packages/server/src/constants/index.js | 4 ++++ packages/server/src/db/linkedRows/LinkController.js | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/server/src/constants/index.js b/packages/server/src/constants/index.js index ff2e6f19f0..c71e43d1fa 100644 --- a/packages/server/src/constants/index.js +++ b/packages/server/src/constants/index.js @@ -12,6 +12,10 @@ exports.FieldTypes = { AUTO: "auto", } +exports.RelationshipTypes = { + ONE_TO_MANY: "one-to-many", +} + exports.AuthTypes = { APP: "app", BUILDER: "builder", diff --git a/packages/server/src/db/linkedRows/LinkController.js b/packages/server/src/db/linkedRows/LinkController.js index 9230c62e73..a02c9946ef 100644 --- a/packages/server/src/db/linkedRows/LinkController.js +++ b/packages/server/src/db/linkedRows/LinkController.js @@ -2,7 +2,7 @@ const CouchDB = require("../index") const { IncludeDocs, getLinkDocuments } = require("./linkUtils") const { generateLinkID } = require("../utils") const Sentry = require("@sentry/node") -const { FieldTypes } = require("../../constants") +const { FieldTypes, RelationshipTypes } = require("../../constants") /** * Creates a new link document structure which can be put to the database. It is important to @@ -150,7 +150,7 @@ class LinkController { // iterate through the link IDs in the row field, see if any don't exist already for (let linkId of rowField) { - if (linkedSchema.relationshipType === "one-to-many") { + if (linkedSchema.relationshipType === RelationshipTypes.ONE_TO_MANY) { const links = await getLinkDocuments({ appId: this._appId, tableId: field.tableId, From fbbeb19724a8a7598a5c7881c7cf6e520bc10cfb Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 25 Feb 2021 12:23:47 +0000 Subject: [PATCH 5/5] adding many to many constant --- packages/server/src/constants/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/server/src/constants/index.js b/packages/server/src/constants/index.js index c71e43d1fa..90893730be 100644 --- a/packages/server/src/constants/index.js +++ b/packages/server/src/constants/index.js @@ -14,6 +14,7 @@ exports.FieldTypes = { exports.RelationshipTypes = { ONE_TO_MANY: "one-to-many", + MANY_TO_MANY: "many-to-many", } exports.AuthTypes = {