diff --git a/packages/server/src/api/controllers/row/external.js b/packages/server/src/api/controllers/row/external.js index 581f620634..6c2ee1c7ae 100644 --- a/packages/server/src/api/controllers/row/external.js +++ b/packages/server/src/api/controllers/row/external.js @@ -70,9 +70,10 @@ async function handleRequest( makeExternalQuery(appId, { endpoint: { ...json.endpoint, - entityId: processObjectSync(tableName, row), + entityId: tableName, }, - body: toInsert, + // if we're doing many relationships then we're writing, only one response + body: processObjectSync(toInsert, response[0]), }) ) } diff --git a/packages/server/src/api/controllers/row/externalUtils.js b/packages/server/src/api/controllers/row/externalUtils.js index b73d4ecdeb..35b79eaf67 100644 --- a/packages/server/src/api/controllers/row/externalUtils.js +++ b/packages/server/src/api/controllers/row/externalUtils.js @@ -13,34 +13,39 @@ exports.inputProcessing = (row, table, allTables) => { let newRow = {}, manyRelationships = [] for (let [key, field] of Object.entries(table.schema)) { - // currently excludes empty strings - if (!row[key]) { + // if set already, or not set just skip it + if (!row[key] || newRow[key]) { continue } - const isLink = field.type === FieldTypes.LINK - if (isLink && !field.through) { + // if its not a link then just copy it over + if (field.type !== FieldTypes.LINK) { + newRow[key] = row[key] + continue + } + const { tableName: linkTableName } = breakExternalTableId(field.tableId) + // table has to exist for many to many + if (!allTables[linkTableName]) { + continue + } + const linkTable = allTables[linkTableName] + if (!field.through) { // we don't really support composite keys for relationships, this is why [0] is used - newRow[key] = breakRowIdField(row[key][0])[0] - } else if (isLink && field.through) { - const { tableName: linkTableName } = breakExternalTableId(field.tableId) - // table has to exist for many to many - if (!allTables[linkTableName]) { - continue - } - const linkTable = allTables[linkTableName] + newRow[field.foreignKey || linkTable.primary] = breakRowIdField(row[key][0])[0] + } else { row[key].map(relationship => { // we don't really support composite keys for relationships, this is why [0] is used manyRelationships.push({ tableId: field.through, [linkTable.primary]: breakRowIdField(relationship)[0], // leave the ID for enrichment later - [table.primary]: `{{ _id }}`, + [table.primary]: `{{ ${table.primary} }}`, }) }) - } else { - newRow[key] = row[key] } } + // we return the relationships that may need to be created in the through table + // we do this so that if the ID is generated by the DB it can be inserted + // after the fact return { row: newRow, manyRelationships } } diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index 40ff2de5b6..e867630744 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -176,52 +176,52 @@ module PostgresModule { } // TODO: hack for testing - if (tableName === "persons") { - tables[tableName].primaryDisplay = "firstname" - } - if (tableName === "products") { - tables[tableName].primaryDisplay = "productname" - } - if (tableName === "tasks") { - tables[tableName].primaryDisplay = "taskname" - } - if (tableName === "products") { - tables[tableName].schema["tasks"] = { - name: "tasks", - type: "link", - tableId: buildExternalTableId(datasourceId, "tasks"), - relationshipType: "many-to-many", - through: buildExternalTableId(datasourceId, "products_tasks"), - fieldName: "taskid", - } - } - if (tableName === "persons") { - tables[tableName].schema["tasks"] = { - name: "tasks", - type: "link", - tableId: buildExternalTableId(datasourceId, "tasks"), - relationshipType: "many-to-one", - fieldName: "personid", - } - } - if (tableName === "tasks") { - tables[tableName].schema["products"] = { - name: "products", - type: "link", - tableId: buildExternalTableId(datasourceId, "products"), - relationshipType: "many-to-many", - through: buildExternalTableId(datasourceId, "products_tasks"), - fieldName: "productid", - } - tables[tableName].schema["people"] = { - name: "people", - type: "link", - tableId: buildExternalTableId(datasourceId, "persons"), - relationshipType: "one-to-many", - fieldName: "personid", - foreignKey: "personid", - } - } + // if (tableName === "persons") { + // tables[tableName].primaryDisplay = "firstname" + // } + // if (tableName === "products") { + // tables[tableName].primaryDisplay = "productname" + // } + // if (tableName === "tasks") { + // tables[tableName].primaryDisplay = "taskname" + // } + // if (tableName === "products") { + // tables[tableName].schema["tasks"] = { + // name: "tasks", + // type: "link", + // tableId: buildExternalTableId(datasourceId, "tasks"), + // relationshipType: "many-to-many", + // through: buildExternalTableId(datasourceId, "products_tasks"), + // fieldName: "taskid", + // } + // } + // if (tableName === "persons") { + // tables[tableName].schema["tasks"] = { + // name: "tasks", + // type: "link", + // tableId: buildExternalTableId(datasourceId, "tasks"), + // relationshipType: "many-to-one", + // fieldName: "personid", + // } + // } + // if (tableName === "tasks") { + // tables[tableName].schema["products"] = { + // name: "products", + // type: "link", + // tableId: buildExternalTableId(datasourceId, "products"), + // relationshipType: "many-to-many", + // through: buildExternalTableId(datasourceId, "products_tasks"), + // fieldName: "productid", + // } + // tables[tableName].schema["people"] = { + // name: "people", + // type: "link", + // tableId: buildExternalTableId(datasourceId, "persons"), + // relationshipType: "one-to-many", + // fieldName: "personid", + // foreignKey: "personid", + // } + // } } this.tables = tables }