From 65078861a29d6aaa59c19e554b95a39e41579981 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 10 Nov 2020 17:22:41 +0000 Subject: [PATCH] Starting work off towards routing of screens in backend, getting view ready and a bit of cleanup to make internal views easier to create. --- .../server/src/api/controllers/view/index.js | 5 +++-- .../server/src/db/linkedRows/linkUtils.js | 5 +++-- packages/server/src/db/utils.js | 10 +++++++++ packages/server/src/routing/index.js | 19 ++++++++++++++++ packages/server/src/routing/routingUtils.js | 22 +++++++++++++++++++ 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 packages/server/src/routing/index.js create mode 100644 packages/server/src/routing/routingUtils.js diff --git a/packages/server/src/api/controllers/view/index.js b/packages/server/src/api/controllers/view/index.js index 57d4862b7e..0b5b18a93c 100644 --- a/packages/server/src/api/controllers/view/index.js +++ b/packages/server/src/api/controllers/view/index.js @@ -5,6 +5,7 @@ const { join } = require("../../../utilities/centralPath") const os = require("os") const exporters = require("./exporters") const { fetchView } = require("../row") +const { ViewNames } = require("../../../db/utils") const controller = { fetch: async ctx => { @@ -13,8 +14,8 @@ const controller = { const response = [] for (let name of Object.keys(designDoc.views)) { - // Only return custom views - if (name === "by_link") { + // Only return custom views, not built ins + if (Object.values(ViewNames).indexOf(name) !== -1) { continue } response.push({ diff --git a/packages/server/src/db/linkedRows/linkUtils.js b/packages/server/src/db/linkedRows/linkUtils.js index dc9d8d3f1e..3a9aff6c33 100644 --- a/packages/server/src/db/linkedRows/linkUtils.js +++ b/packages/server/src/db/linkedRows/linkUtils.js @@ -1,5 +1,6 @@ const CouchDB = require("../index") const Sentry = require("@sentry/node") +const { ViewNames, getQueryIndex } = require("../utils") /** * Only needed so that boolean parameters are being used for includeDocs @@ -40,7 +41,7 @@ exports.createLinkView = async appId => { } designDoc.views = { ...designDoc.views, - by_link: view, + [ViewNames.LINK]: view, } await db.put(designDoc) } @@ -76,7 +77,7 @@ exports.getLinkDocuments = async function({ } params.include_docs = !!includeDocs try { - const response = await db.query("database/by_link", params) + const response = await db.query(getQueryIndex(ViewNames.LINK), params) if (includeDocs) { return response.rows.map(row => row.doc) } else { diff --git a/packages/server/src/db/utils.js b/packages/server/src/db/utils.js index a213dc9066..4edb27a416 100644 --- a/packages/server/src/db/utils.js +++ b/packages/server/src/db/utils.js @@ -17,10 +17,20 @@ const DocumentTypes = { SCREEN: "screen", } +const ViewNames = { + LINK: "by_link", + ROUTING: "screen_routes", +} + +exports.ViewNames = ViewNames exports.DocumentTypes = DocumentTypes exports.SEPARATOR = SEPARATOR 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 * is usually the case as most of our docs are top level e.g. tables, automations, users and so on. diff --git a/packages/server/src/routing/index.js b/packages/server/src/routing/index.js new file mode 100644 index 0000000000..5de85861b4 --- /dev/null +++ b/packages/server/src/routing/index.js @@ -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 + } + } +} diff --git a/packages/server/src/routing/routingUtils.js b/packages/server/src/routing/routingUtils.js new file mode 100644 index 0000000000..7a51079b8a --- /dev/null +++ b/packages/server/src/routing/routingUtils.js @@ -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) +}