work in progress, enriching in the display names.

This commit is contained in:
mike12345567 2021-02-17 18:04:21 +00:00
parent 0f323d8aa9
commit 5fab1529ec
5 changed files with 62 additions and 18 deletions

View File

@ -137,6 +137,7 @@ exports.addPermission = async (
exports.createLinkedTable = async (request, appId) => { exports.createLinkedTable = async (request, appId) => {
// get the ID to link to // get the ID to link to
const table = await exports.createTable(request, appId) const table = await exports.createTable(request, appId)
table.displayName = "name"
table.schema.link = { table.schema.link = {
type: "link", type: "link",
fieldName: "link", fieldName: "link",

View File

@ -287,7 +287,7 @@ describe("/rows", () => {
})).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]._id).toBe(firstRow._id) expect(enriched[0].link[0]).toBe("Test Contact")
}) })
}) })

View File

@ -7,6 +7,8 @@ const {
} = require("./linkUtils") } = require("./linkUtils")
const { flatten } = require("lodash") const { flatten } = require("lodash")
const CouchDB = require("../../db") const CouchDB = require("../../db")
const { getMultiIDParams } = require("../../db/utils")
const { FieldTypes } = require("../../constants")
/** /**
* This functionality makes sure that when rows with links are created, updated or deleted they are processed * This functionality makes sure that when rows with links are created, updated or deleted they are processed
@ -28,10 +30,16 @@ exports.IncludeDocs = IncludeDocs
exports.getLinkDocuments = getLinkDocuments exports.getLinkDocuments = getLinkDocuments
exports.createLinkView = createLinkView exports.createLinkView = createLinkView
function getLinkedTableIDs(table) {
return Object.values(table.schema)
.filter(column => column.type === FieldTypes.LINK)
.map(column => column.tableId)
}
async function getLinksForRows(appId, rows) { async function getLinksForRows(appId, rows) {
let tableIds = [...new Set(rows.map(el => el.tableId))] const tableIds = [...new Set(rows.map(el => el.tableId))]
// start by getting all the link values for performance reasons // start by getting all the link values for performance reasons
let responses = flatten( const responses = flatten(
await Promise.all( await Promise.all(
tableIds.map(tableId => tableIds.map(tableId =>
getLinkDocuments({ getLinkDocuments({
@ -117,7 +125,7 @@ exports.updateLinks = async function({
* @returns {Promise<object>} The updated row (this may be the same if no links were found). If an array was input * @returns {Promise<object>} The updated row (this may be the same if no links were found). If an array was input
* then an array will be output, object input -> object output. * then an array will be output, object input -> object output.
*/ */
exports.attachLinkInfo = async (appId, rows) => { exports.attachLinkIDs = async (appId, rows) => {
// handle a single row as well as multiple // handle a single row as well as multiple
let wasArray = true let wasArray = true
if (!(rows instanceof Array)) { if (!(rows instanceof Array)) {
@ -143,25 +151,37 @@ exports.attachLinkInfo = async (appId, rows) => {
return wasArray ? rows : rows[0] return wasArray ? rows : rows[0]
} }
exports.attachLinkedRows = async (appId, rows) => { /**
* Given information about the table we can extract the display name from the linked rows, this
* is what we do for showing the display name of each linked row when in a table format.
* @param {string} appId The app in which the tables/rows/links exist.
* @param {object} table The table from which the rows originated.
* @param {array<object>} rows The rows which are to be enriched with the linked display names/IDs.
* @returns {Promise<Array>} The enriched rows after having display names/IDs attached to the linked fields.
*/
exports.attachLinkedDisplayName = async (appId, table, rows) => {
const linkedTableIds = getLinkedTableIDs(table)
if (linkedTableIds.length === 0) {
return rows
}
let wasArray = true let wasArray = true
if (!(rows instanceof Array)) { if (!(rows instanceof Array)) {
rows = [rows] rows = [rows]
wasArray = false wasArray = false
} }
const db = new CouchDB(appId) const db = new CouchDB(appId)
const linkedTables = (await db.find(getMultiIDParams(linkedTableIds))).docs
const links = (await getLinksForRows(appId, rows)).filter(link => const links = (await getLinksForRows(appId, rows)).filter(link =>
rows.some(row => row._id === link.thisId) rows.some(row => row._id === link.thisId)
) )
const linkedRows = ( const fields = [
await db.find({ "_id",
selector: { ...linkedTables
_id: { .filter(table => table.displayName != null)
$in: links.map(link => link.id), .map(table => table.displayName),
}, ]
}, const linkedRowIds = links.map(link => link.id)
}) const linked = (await db.find(getMultiIDParams(linkedRowIds, fields))).docs
).docs
for (let row of rows) { for (let row of rows) {
links links
.filter(link => link.thisId === row._id) .filter(link => link.thisId === row._id)
@ -169,9 +189,15 @@ exports.attachLinkedRows = async (appId, rows) => {
if (row[link.fieldName] == null) { if (row[link.fieldName] == null) {
row[link.fieldName] = [] row[link.fieldName] = []
} }
const linkedRow = linkedRows.find(row => row._id === link.id) const linkedTableId = table.schema[link.fieldName].tableId
if (linkedRow) { const linkedRow = linked.find(row => row._id === link.id)
row[link.fieldName].push(linkedRow) const linkedTable = linkedTables.find(
table => table._id === linkedTableId
)
if (linkedRow && linkedTable) {
row[link.fieldName].push(
linkedRow[linkedTable.displayName] || linkedRow._id
)
} }
}) })
} }

View File

@ -277,3 +277,20 @@ exports.getQueryParams = (datasourceId = null, otherProps = {}) => {
otherProps otherProps
) )
} }
/**
* This can be used with the db.find functionality to get a list of IDs
*/
exports.getMultiIDParams = (ids, fields = null) => {
let config = {
selector: {
_id: {
$in: ids,
},
},
}
if (fields) {
config.fields = fields
}
return config
}

View File

@ -167,7 +167,7 @@ exports.inputProcessing = async (user, table, row) => {
*/ */
exports.outputProcessing = async (appId, table, rows) => { exports.outputProcessing = async (appId, table, rows) => {
// attach any linked row information // attach any linked row information
const outputRows = await linkRows.attachLinkedRows(appId, rows) const outputRows = await linkRows.attachLinkedDisplayName(appId, table, rows)
// update the attachments URL depending on hosting // update the attachments URL depending on hosting
if (env.CLOUD && env.SELF_HOSTED) { if (env.CLOUD && env.SELF_HOSTED) {
for (let [property, column] of Object.entries(table.schema)) { for (let [property, column] of Object.entries(table.schema)) {