Starting work off towards routing of screens in backend, getting view ready and a bit of cleanup to make internal views easier to create.

This commit is contained in:
mike12345567 2020-11-10 17:22:41 +00:00
parent 9d7ac6835c
commit 5b26fce1ea
5 changed files with 57 additions and 4 deletions

View File

@ -5,6 +5,7 @@ const { join } = require("../../../utilities/centralPath")
const os = require("os") const os = require("os")
const exporters = require("./exporters") const exporters = require("./exporters")
const { fetchView } = require("../row") const { fetchView } = require("../row")
const { ViewNames } = require("../../../db/utils")
const controller = { const controller = {
fetch: async ctx => { fetch: async ctx => {
@ -13,8 +14,8 @@ const controller = {
const response = [] const response = []
for (let name of Object.keys(designDoc.views)) { for (let name of Object.keys(designDoc.views)) {
// Only return custom views // Only return custom views, not built ins
if (name === "by_link") { if (Object.values(ViewNames).indexOf(name) !== -1) {
continue continue
} }
response.push({ response.push({

View File

@ -1,5 +1,6 @@
const CouchDB = require("../index") const CouchDB = require("../index")
const Sentry = require("@sentry/node") const Sentry = require("@sentry/node")
const { ViewNames, getQueryIndex } = require("../utils")
/** /**
* Only needed so that boolean parameters are being used for includeDocs * Only needed so that boolean parameters are being used for includeDocs
@ -40,7 +41,7 @@ exports.createLinkView = async appId => {
} }
designDoc.views = { designDoc.views = {
...designDoc.views, ...designDoc.views,
by_link: view, [ViewNames.LINK]: view,
} }
await db.put(designDoc) await db.put(designDoc)
} }
@ -76,7 +77,7 @@ exports.getLinkDocuments = async function({
} }
params.include_docs = !!includeDocs params.include_docs = !!includeDocs
try { try {
const response = await db.query("database/by_link", params) const response = await db.query(getQueryIndex(ViewNames.LINK), params)
if (includeDocs) { if (includeDocs) {
return response.rows.map(row => row.doc) return response.rows.map(row => row.doc)
} else { } else {

View File

@ -17,10 +17,20 @@ const DocumentTypes = {
SCREEN: "screen", SCREEN: "screen",
} }
const ViewNames = {
LINK: "by_link",
ROUTING: "screen_routes",
}
exports.ViewNames = ViewNames
exports.DocumentTypes = DocumentTypes exports.DocumentTypes = DocumentTypes
exports.SEPARATOR = SEPARATOR exports.SEPARATOR = SEPARATOR
exports.UNICODE_MAX = UNICODE_MAX exports.UNICODE_MAX = UNICODE_MAX
exports.getQueryIndex = viewName => {
return `database/${viewName}`
}
/** /**
* If creating DB allDocs/query params with only a single top level ID this can be used, this * If creating DB allDocs/query params with only a single top level ID this can be used, this
* is usually the case as most of our docs are top level e.g. tables, automations, users and so on. * is usually the case as most of our docs are top level e.g. tables, automations, users and so on.

View File

@ -0,0 +1,19 @@
const CouchDB = require("../db")
const { createRoutingView } = require("./routingUtils")
const { ViewNames, getQueryIndex } = require("../db/utils")
exports.getRoutingInfo = async appId => {
const db = new CouchDB(appId)
try {
const allRouting = await db.query(getQueryIndex(ViewNames.ROUTING))
return allRouting.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 createRoutingView(appId)
return exports.getRoutingInfo(appId)
} else {
throw err
}
}
}

View File

@ -0,0 +1,22 @@
const CouchDB = require("../db")
const { DocumentTypes, SEPARATOR, ViewNames } = require("../db/utils")
const SCREEN_PREFIX = DocumentTypes.SCREEN + SEPARATOR
exports.createRoutingView = async appId => {
const db = new CouchDB(appId)
const designDoc = await db.get("_design/database")
const view = {
map: function(doc) {
if (doc._id.startsWith(SCREEN_PREFIX)) {
emit(doc._id, {
routing: doc.routing,
})
}
}.toString(),
}
designDoc.views = {
...designDoc.views,
[ViewNames.ROUTING]: view,
}
await db.put(designDoc)
}