Server side data integrity for 1:N

This commit is contained in:
Martin McKeaveney 2021-02-25 11:55:23 +00:00
parent 19fb11e69d
commit bcaed4f4e4
2 changed files with 31 additions and 8 deletions

View File

@ -29,6 +29,12 @@
// Prevent modal closing if there were errors // Prevent modal closing if there were errors
return false return false
} }
if (rowResponse.status === 500) {
notifier.danger(rowResponse.message)
return false
}
notifier.success("Row saved successfully.") notifier.success("Row saved successfully.")
backendUiStore.actions.rows.save(rowResponse) backendUiStore.actions.rows.save(rowResponse)
} }

View File

@ -132,14 +132,6 @@ class LinkController {
const rowField = row[fieldName] const rowField = row[fieldName]
const field = table.schema[fieldName] const field = table.schema[fieldName]
if (field.type === FieldTypes.LINK && rowField != null) { 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 // check which links actual pertain to the update in this row
const thisFieldLinkDocs = linkDocs.filter( const thisFieldLinkDocs = linkDocs.filter(
linkDoc => linkDoc =>
@ -151,6 +143,31 @@ class LinkController {
? linkDoc.doc2.rowId ? linkDoc.doc2.rowId
: linkDoc.doc1.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 // iterate through the link IDs in the row field, see if any don't exist already
for (let linkId of rowField) { for (let linkId of rowField) {
if (linkId && linkId !== "" && linkDocIds.indexOf(linkId) === -1) { if (linkId && linkId !== "" && linkDocIds.indexOf(linkId) === -1) {