From 514f2b0cef2ffb80c33722e53820a5c27e8dc5ee Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 11 Oct 2023 13:48:28 +0200 Subject: [PATCH] Types --- .../api/controllers/row/ExternalRequest.ts | 23 +++++++------------ .../src/api/controllers/table/external.ts | 20 +++++++++------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index 0fea102f7b..cf8f7b5be4 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -568,18 +568,14 @@ export class ExternalRequest { // need to specify where to put this back into column: fieldName, } - if ((field as ManyToManyRelationshipFieldMetadata).through) { + if (isManyToMany(field)) { const { tableName: throughTableName } = breakExternalTableId( - (field as ManyToManyRelationshipFieldMetadata).through + field.through ) definition.through = throughTableName // don't support composite keys for relationships - definition.from = - (field as ManyToManyRelationshipFieldMetadata).throughTo || - table.primary[0] - definition.to = - (field as ManyToManyRelationshipFieldMetadata).throughFrom || - linkTable.primary[0] + definition.from = field.throughTo || table.primary[0] + definition.to = field.throughFrom || linkTable.primary[0] definition.fromPrimary = table.primary[0] definition.toPrimary = linkTable.primary[0] } @@ -603,7 +599,7 @@ export class ExternalRequest { const primaryKey = table.primary[0] // make a new request to get the row with all its relationships // we need this to work out if any relationships need removed - for (let field of Object.values(table.schema)) { + for (const field of Object.values(table.schema)) { if ( field.type !== FieldTypes.LINK || !field.fieldName || @@ -612,16 +608,13 @@ export class ExternalRequest { continue } const isMany = field.relationshipType === RelationshipType.MANY_TO_MANY - const tableId = isMany - ? (field as ManyToManyRelationshipFieldMetadata).through - : field.tableId + const tableId = isMany ? field.through : field.tableId const { tableName: relatedTableName } = breakExternalTableId(tableId) // @ts-ignore const linkPrimaryKey = this.tables[relatedTableName].primary[0] - const manyKey = - (field as ManyToManyRelationshipFieldMetadata).throughTo || primaryKey + const lookupField = isMany ? primaryKey : (field as any).foreignKey - const fieldName = isMany ? manyKey : field.fieldName + const fieldName = isMany ? field.throughTo || primaryKey : field.fieldName if (!lookupField || !row[lookupField]) { continue } diff --git a/packages/server/src/api/controllers/table/external.ts b/packages/server/src/api/controllers/table/external.ts index dac39a4a99..e84c23b5ca 100644 --- a/packages/server/src/api/controllers/table/external.ts +++ b/packages/server/src/api/controllers/table/external.ts @@ -81,7 +81,9 @@ function cleanupRelationships( const relatedTable = Object.values(tables).find( table => table._id === schemaTableId ) - const foreignKey = (schema as any).foreignKey + const foreignKey = + schema.relationshipType !== RelationshipType.MANY_TO_MANY && + schema.foreignKey if (!relatedTable || !foreignKey) { continue } @@ -183,17 +185,19 @@ function generateRelatedSchema( ) { // generate column for other table const relatedSchema = cloneDeep(linkColumn) + const isMany2Many = + linkColumn.relationshipType === RelationshipType.MANY_TO_MANY // swap them from the main link - if ((linkColumn as any).foreignKey) { - relatedSchema.fieldName = (linkColumn as any).foreignKey + if (!isMany2Many && linkColumn.foreignKey) { + relatedSchema.fieldName = linkColumn.foreignKey relatedSchema.foreignKey = linkColumn.fieldName } // is many to many - else { + else if (isMany2Many) { // don't need to copy through, already got it - relatedSchema.fieldName = (linkColumn as any).throughTo - relatedSchema.throughTo = (linkColumn as any).throughFrom - relatedSchema.throughFrom = (linkColumn as any).throughTo + relatedSchema.fieldName = linkColumn.throughTo + relatedSchema.throughTo = linkColumn.throughFrom + relatedSchema.throughFrom = linkColumn.throughTo } relatedSchema.relationshipType = otherRelationshipType( linkColumn.relationshipType @@ -203,7 +207,7 @@ function generateRelatedSchema( table.schema[columnName] = relatedSchema } -function isRelationshipSetup(column: FieldSchema) { +function isRelationshipSetup(column: RelationshipFieldMetadata) { return (column as any).foreignKey || (column as any).through }