Browse Source

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

gh-pages
mike12345567 6 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({ const secondRow = (await createRow({
name: "Test 2", name: "Test 2",
description: "og desc", description: "og desc",
link: [firstRow._id], link: [{_id: firstRow._id}],
tableId: table._id, tableId: table._id,
})).body })).body
const enriched = await outputProcessing(appId, table, [secondRow]) const enriched = await outputProcessing(appId, table, [secondRow])
expect(enriched[0].link.length).toBe(1) 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) { if (!linkedRow || !linkedTable) {
continue continue
} }
// need to handle an edge case where relationship just wasn't found const obj = { _id: linkedRow._id }
const value = linkedRow[linkedTable.primaryDisplay] || linkedRow._id // if we know the display column, add it
if (value) { if (linkedRow[linkedTable.primaryDisplay] != null) {
row[link.fieldName].push(value) obj.primaryDisplay = linkedRow[linkedTable.primaryDisplay]
} }
row[link.fieldName].push(obj)
} }
} }
return rows return rows

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

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

Loading…
Cancel
Save