Browse Source

Server side data integrity for 1:N

pull/1189/head
Martin McKeaveney 5 years ago
parent
commit
5bc8b5da4c
  1. 6
      packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte
  2. 33
      packages/server/src/db/linkedRows/LinkController.js

6
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)
}

33
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) {

Loading…
Cancel
Save