Fixing some missues with linked records after testing with the new UI.
This commit is contained in:
parent
5c9eff4cf4
commit
9c6490f0f2
|
@ -55,6 +55,8 @@ exports.save = async function(ctx) {
|
||||||
modelToSave._rev = result.rev
|
modelToSave._rev = result.rev
|
||||||
|
|
||||||
const designDoc = await db.get("_design/database")
|
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 = {
|
||||||
...designDoc.views,
|
...designDoc.views,
|
||||||
[`all_${modelToSave._id}`]: {
|
[`all_${modelToSave._id}`]: {
|
||||||
|
|
|
@ -170,7 +170,7 @@ exports.find = async function(ctx) {
|
||||||
ctx.throw(400, "Supplied modelId does not match the records modelId")
|
ctx.throw(400, "Supplied modelId does not match the records modelId")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.body = await linkRecords.attachLinkInfo(instanceId, record)
|
ctx.body = await linkRecords.attachLinkInfoSingleRecord(instanceId, record)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.destroy = async function(ctx) {
|
exports.destroy = async function(ctx) {
|
||||||
|
@ -245,7 +245,7 @@ exports.fetchLinkedRecords = async function(ctx) {
|
||||||
recordId,
|
recordId,
|
||||||
})
|
})
|
||||||
// now get the docs from the all docs index
|
// 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,
|
include_docs: true,
|
||||||
keys: linkDocIds,
|
keys: linkDocIds,
|
||||||
})
|
})
|
||||||
|
|
|
@ -114,7 +114,7 @@ class LinkController {
|
||||||
const linkDocIds = await this.getLinkDocs(false, fieldName, record._id)
|
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
|
// iterate through the link IDs in the record field, see if any don't exist already
|
||||||
for (let linkId of recordField) {
|
for (let linkId of recordField) {
|
||||||
if (linkDocIds.indexOf(linkId) === -1) {
|
if (linkId && linkId !== "" && linkDocIds.indexOf(linkId) === -1) {
|
||||||
operations.push(
|
operations.push(
|
||||||
new LinkDocument(
|
new LinkDocument(
|
||||||
model._id,
|
model._id,
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const LinkController = require("./LinkController")
|
const LinkController = require("./LinkController")
|
||||||
const CouchDB = require("../index")
|
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
|
* 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) => {
|
exports.attachLinkInfo = async (instanceId, records) => {
|
||||||
let recordPromises = []
|
let recordPromises = []
|
||||||
for (let record of records) {
|
for (let record of records) {
|
||||||
recordPromises.push(exports.attachLinkInfo(instanceId, record))
|
recordPromises.push(exports.attachLinkInfoSingleRecord(instanceId, record))
|
||||||
}
|
}
|
||||||
return await Promise.all(recordPromises)
|
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).
|
* @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).
|
* @returns {Promise<object>} The updated record (this may be the same if no links were found).
|
||||||
*/
|
*/
|
||||||
exports.attachLinkInfo = async (instanceId, record) => {
|
exports.attachLinkInfoSingleRecord = async (instanceId, record) => {
|
||||||
// first check if the record has any link fields
|
// first check if the record has any link fields and set counts to zero
|
||||||
let hasLinkedRecords = false
|
let hasLinkedRecords = false
|
||||||
for (let fieldName of Object.keys(record)) {
|
for (let fieldName of Object.keys(record)) {
|
||||||
let field = record[fieldName]
|
let field = record[fieldName]
|
||||||
if (field != null && field.type === "link") {
|
if (field != null && field.type === "link") {
|
||||||
hasLinkedRecords = true
|
hasLinkedRecords = true
|
||||||
break
|
field.count = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// no linked records, can simply return
|
// no linked records, can simply return
|
||||||
|
@ -140,8 +141,8 @@ exports.attachLinkInfo = async (instanceId, record) => {
|
||||||
for (let linkDoc of linkDocs) {
|
for (let linkDoc of linkDocs) {
|
||||||
// work out which link pertains to this record
|
// work out which link pertains to this record
|
||||||
const doc = linkDoc.doc1.recordId === recordId ? linkDoc.doc1 : linkDoc.doc2
|
const doc = linkDoc.doc1.recordId === recordId ? linkDoc.doc1 : linkDoc.doc2
|
||||||
if (record[doc.fieldName].count == null) {
|
if (record[doc.fieldName] == null || isNaN(record[doc.fieldName].count)) {
|
||||||
record[doc.fieldName].count = 1
|
record[doc.fieldName] = { type: "link", count: 1 }
|
||||||
} else {
|
} else {
|
||||||
record[doc.fieldName].count++
|
record[doc.fieldName].count++
|
||||||
}
|
}
|
||||||
|
@ -189,13 +190,17 @@ exports.getLinkDocuments = async ({
|
||||||
params.include_docs = !!includeDocs
|
params.include_docs = !!includeDocs
|
||||||
try {
|
try {
|
||||||
const response = await db.query("database/by_link", params)
|
const response = await db.query("database/by_link", params)
|
||||||
|
if (includeDocs) {
|
||||||
return response.rows.map(row => row.doc)
|
return response.rows.map(row => row.doc)
|
||||||
|
} else {
|
||||||
|
return response.rows.map(row => row.value)
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// check if the view doesn't exist, it should for all new instances
|
// check if the view doesn't exist, it should for all new instances
|
||||||
if (err != null && err.name === "not_found") {
|
if (err != null && err.name === "not_found") {
|
||||||
await exports.createLinkView(instanceId)
|
await exports.createLinkView(instanceId)
|
||||||
} else {
|
} else {
|
||||||
console.error(err)
|
Sentry.captureException(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue