|
|
|
@ -3,6 +3,7 @@ const { IncludeDocs, getLinkDocuments } = require("./linkUtils") |
|
|
|
const { generateLinkID } = require("../utils") |
|
|
|
const Sentry = require("@sentry/node") |
|
|
|
const { FieldTypes, RelationshipTypes } = require("../../constants") |
|
|
|
const { isEqual } = require("lodash") |
|
|
|
|
|
|
|
/** |
|
|
|
* Creates a new link document structure which can be put to the database. It is important to |
|
|
|
@ -113,6 +114,25 @@ class LinkController { |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Makes sure the passed in table schema contains valid relationship structures. |
|
|
|
*/ |
|
|
|
validateTable(table) { |
|
|
|
const usedAlready = [] |
|
|
|
for (let schema of Object.values(table.schema)) { |
|
|
|
if (schema.type !== FieldTypes.LINK) { |
|
|
|
continue |
|
|
|
} |
|
|
|
const unique = schema.tableId + schema.fieldName |
|
|
|
if (usedAlready.indexOf(unique) !== -1) { |
|
|
|
throw new Error( |
|
|
|
"Cannot re-use the linked column name for a linked table." |
|
|
|
) |
|
|
|
} |
|
|
|
usedAlready.push(unique) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// all operations here will assume that the table
|
|
|
|
// this operation is related to has linked rows
|
|
|
|
/** |
|
|
|
@ -268,6 +288,8 @@ class LinkController { |
|
|
|
*/ |
|
|
|
async tableSaved() { |
|
|
|
const table = await this.table() |
|
|
|
// validate the table first
|
|
|
|
this.validateTable(table) |
|
|
|
const schema = table.schema |
|
|
|
for (let fieldName of Object.keys(schema)) { |
|
|
|
const field = schema[fieldName] |
|
|
|
@ -291,6 +313,11 @@ class LinkController { |
|
|
|
if (field.autocolumn) { |
|
|
|
linkConfig.autocolumn = field.autocolumn |
|
|
|
} |
|
|
|
// check the linked table to make sure we aren't overwriting an existing column
|
|
|
|
const existingSchema = linkedTable.schema[field.fieldName] |
|
|
|
if (existingSchema != null && !isEqual(existingSchema, linkConfig)) { |
|
|
|
throw new Error("Cannot overwrite existing column.") |
|
|
|
} |
|
|
|
// create the link field in the other table
|
|
|
|
linkedTable.schema[field.fieldName] = linkConfig |
|
|
|
const response = await this._db.put(linkedTable) |
|
|
|
|