1
0
Fork 0
mirror of synced 2024-07-08 07:46:10 +12:00

Fixing some missues with linked records after testing with the new UI.

This commit is contained in:
mike12345567 2020-09-30 12:00:56 +01:00
parent 5c9eff4cf4
commit 9c6490f0f2
4 changed files with 18 additions and 11 deletions

View file

@ -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}`]: {

View file

@ -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,
})

View file

@ -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,

View file

@ -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<object>} 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)
}
}
}