From 9c6490f0f24d56d7e950c2c218c379f82d78b03b Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 30 Sep 2020 12:00:56 +0100 Subject: [PATCH] Fixing some missues with linked records after testing with the new UI. --- packages/server/src/api/controllers/model.js | 2 ++ packages/server/src/api/controllers/record.js | 4 ++-- .../src/db/linkedRecords/LinkController.js | 2 +- packages/server/src/db/linkedRecords/index.js | 21 ++++++++++++------- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/server/src/api/controllers/model.js b/packages/server/src/api/controllers/model.js index f4934583c3..dabad28456 100644 --- a/packages/server/src/api/controllers/model.js +++ b/packages/server/src/api/controllers/model.js @@ -55,6 +55,8 @@ exports.save = async function(ctx) { modelToSave._rev = result.rev const designDoc = await db.get("_design/database") + /** TODO: should we include the doc type here - currently it is possible for anything + with a modelId in it to be returned */ designDoc.views = { ...designDoc.views, [`all_${modelToSave._id}`]: { diff --git a/packages/server/src/api/controllers/record.js b/packages/server/src/api/controllers/record.js index 7348356a82..a0711b0dca 100644 --- a/packages/server/src/api/controllers/record.js +++ b/packages/server/src/api/controllers/record.js @@ -170,7 +170,7 @@ exports.find = async function(ctx) { ctx.throw(400, "Supplied modelId does not match the records modelId") return } - ctx.body = await linkRecords.attachLinkInfo(instanceId, record) + ctx.body = await linkRecords.attachLinkInfoSingleRecord(instanceId, record) } exports.destroy = async function(ctx) { @@ -245,7 +245,7 @@ exports.fetchLinkedRecords = async function(ctx) { recordId, }) // now get the docs from the all docs index - const response = await db.query(`database/_all_docs`, { + const response = await db.allDocs({ include_docs: true, keys: linkDocIds, }) diff --git a/packages/server/src/db/linkedRecords/LinkController.js b/packages/server/src/db/linkedRecords/LinkController.js index 94986c8dbc..ee31e61501 100644 --- a/packages/server/src/db/linkedRecords/LinkController.js +++ b/packages/server/src/db/linkedRecords/LinkController.js @@ -114,7 +114,7 @@ class LinkController { const linkDocIds = await this.getLinkDocs(false, fieldName, record._id) // iterate through the link IDs in the record field, see if any don't exist already for (let linkId of recordField) { - if (linkDocIds.indexOf(linkId) === -1) { + if (linkId && linkId !== "" && linkDocIds.indexOf(linkId) === -1) { operations.push( new LinkDocument( model._id, diff --git a/packages/server/src/db/linkedRecords/index.js b/packages/server/src/db/linkedRecords/index.js index 3258df6d15..15e40f032f 100644 --- a/packages/server/src/db/linkedRecords/index.js +++ b/packages/server/src/db/linkedRecords/index.js @@ -1,5 +1,6 @@ const LinkController = require("./LinkController") const CouchDB = require("../index") +const Sentry = require("@sentry/node") /** * This functionality makes sure that when records with links are created, updated or deleted they are processed @@ -100,7 +101,7 @@ exports.updateLinks = async ({ exports.attachLinkInfo = async (instanceId, records) => { let recordPromises = [] for (let record of records) { - recordPromises.push(exports.attachLinkInfo(instanceId, record)) + recordPromises.push(exports.attachLinkInfoSingleRecord(instanceId, record)) } return await Promise.all(recordPromises) } @@ -111,14 +112,14 @@ exports.attachLinkInfo = async (instanceId, records) => { * @param {object} record The record itself which is to be updated with info (if applicable). * @returns {Promise} The updated record (this may be the same if no links were found). */ -exports.attachLinkInfo = async (instanceId, record) => { - // first check if the record has any link fields +exports.attachLinkInfoSingleRecord = async (instanceId, record) => { + // first check if the record has any link fields and set counts to zero let hasLinkedRecords = false for (let fieldName of Object.keys(record)) { let field = record[fieldName] if (field != null && field.type === "link") { hasLinkedRecords = true - break + field.count = 0 } } // no linked records, can simply return @@ -140,8 +141,8 @@ exports.attachLinkInfo = async (instanceId, record) => { for (let linkDoc of linkDocs) { // work out which link pertains to this record const doc = linkDoc.doc1.recordId === recordId ? linkDoc.doc1 : linkDoc.doc2 - if (record[doc.fieldName].count == null) { - record[doc.fieldName].count = 1 + if (record[doc.fieldName] == null || isNaN(record[doc.fieldName].count)) { + record[doc.fieldName] = { type: "link", count: 1 } } else { record[doc.fieldName].count++ } @@ -189,13 +190,17 @@ exports.getLinkDocuments = async ({ params.include_docs = !!includeDocs try { const response = await db.query("database/by_link", params) - return response.rows.map(row => row.doc) + if (includeDocs) { + return response.rows.map(row => row.doc) + } else { + return response.rows.map(row => row.value) + } } catch (err) { // check if the view doesn't exist, it should for all new instances if (err != null && err.name === "not_found") { await exports.createLinkView(instanceId) } else { - console.error(err) + Sentry.captureException(err) } } }