Browse Source

Having the server send out _id and primaryDisplay in an object for relationships, also accepting objects and coercing them on way in.

pull/1190/head
mike12345567 5 years ago
parent
commit
d34a9e12e2
  1. 5
      packages/server/src/api/routes/tests/row.spec.js
  2. 9
      packages/server/src/db/linkedRows/index.js
  3. 3
      packages/server/src/utilities/rowProcessor.js

5
packages/server/src/api/routes/tests/row.spec.js

@ -282,12 +282,13 @@ describe("/rows", () => {
const secondRow = (await createRow({
name: "Test 2",
description: "og desc",
link: [firstRow._id],
link: [{_id: firstRow._id}],
tableId: table._id,
})).body
const enriched = await outputProcessing(appId, table, [secondRow])
expect(enriched[0].link.length).toBe(1)
expect(enriched[0].link[0]).toBe("Test Contact")
expect(enriched[0].link[0]._id).toBe(firstRow._id)
expect(enriched[0].link[0].primaryDisplay).toBe("Test Contact")
})
})

9
packages/server/src/db/linkedRows/index.js

@ -175,11 +175,12 @@ exports.attachLinkedPrimaryDisplay = async (appId, table, rows) => {
if (!linkedRow || !linkedTable) {
continue
}
// need to handle an edge case where relationship just wasn't found
const value = linkedRow[linkedTable.primaryDisplay] || linkedRow._id
if (value) {
row[link.fieldName].push(value)
const obj = { _id: linkedRow._id }
// if we know the display column, add it
if (linkedRow[linkedTable.primaryDisplay] != null) {
obj.primaryDisplay = linkedRow[linkedTable.primaryDisplay]
}
row[link.fieldName].push(obj)
}
}
return rows

3
packages/server/src/utilities/rowProcessor.js

@ -15,6 +15,9 @@ const TYPE_TRANSFORM_MAP = {
[null]: [],
[undefined]: undefined,
parse: link => {
if (Array.isArray(link) && typeof link[0] === "object") {
return link.map(el => (el && el._id ? el._id : el))
}
if (typeof link === "string") {
return [link]
}

Loading…
Cancel
Save