diff --git a/packages/server/src/api/controllers/row/internal.js b/packages/server/src/api/controllers/row/internal.js index d429c14cc7..43ee3dd235 100644 --- a/packages/server/src/api/controllers/row/internal.js +++ b/packages/server/src/api/controllers/row/internal.js @@ -5,7 +5,6 @@ const { generateRowID, DocumentTypes, InternalTables, - generateMemoryViewID, } = require("../../../db/utils") const userController = require("../user") const { @@ -20,7 +19,12 @@ const { fullSearch, paginatedSearch } = require("./internalSearch") const { getGlobalUsersFromMetadata } = require("../../../utilities/global") const inMemoryViews = require("../../../db/inMemoryView") const env = require("../../../environment") -const { migrateToInMemoryView } = require("../view/utils") +const { + migrateToInMemoryView, + migrateToDesignView, + getFromDesignDoc, + getFromMemoryDoc, +} = require("../view/utils") const CALCULATION_TYPES = { SUM: "sum", @@ -74,33 +78,24 @@ async function getRawTableData(ctx, db, tableId) { } async function getView(db, viewName) { - let viewInfo - async function getFromDesignDoc() { - const designDoc = await db.get("_design/database") - viewInfo = designDoc.views[viewName] - return viewInfo - } - let migrate = false - if (env.SELF_HOSTED) { - viewInfo = await getFromDesignDoc() - } else { - try { - viewInfo = await db.get(generateMemoryViewID(viewName)) - if (viewInfo) { - viewInfo = viewInfo.view - } - } catch (err) { - // check if it can be retrieved from design doc (needs migrated) - if (err.status !== 404) { - viewInfo = null - } else { - viewInfo = await getFromDesignDoc() - migrate = !!viewInfo - } + let mainGetter = env.SELF_HOSTED ? getFromDesignDoc : getFromMemoryDoc + let secondaryGetter = env.SELF_HOSTED ? getFromMemoryDoc : getFromDesignDoc + let migration = env.SELF_HOSTED ? migrateToDesignView : migrateToInMemoryView + let viewInfo, + migrate = false + try { + viewInfo = await mainGetter(db, viewName) + } catch (err) { + // check if it can be retrieved from design doc (needs migrated) + if (err.status !== 404) { + viewInfo = null + } else { + viewInfo = await secondaryGetter(db, viewName) + migrate = !!viewInfo } } if (migrate) { - await migrateToInMemoryView(db, viewName) + await migration(db, viewName) } if (!viewInfo) { throw "View does not exist." diff --git a/packages/server/src/api/controllers/view/utils.js b/packages/server/src/api/controllers/view/utils.js index c93604177f..1f3b980882 100644 --- a/packages/server/src/api/controllers/view/utils.js +++ b/packages/server/src/api/controllers/view/utils.js @@ -107,3 +107,30 @@ exports.migrateToInMemoryView = async (db, viewName) => { await db.put(designDoc) await exports.saveView(db, null, viewName, view) } + +exports.migrateToDesignView = async (db, viewName) => { + let view = await db.get(generateMemoryViewID(viewName)) + const designDoc = await db.get("_design/database") + designDoc.views[viewName] = view.view + await db.put(designDoc) + await db.remove(view._id, view._rev) +} + +exports.getFromDesignDoc = async (db, viewName) => { + const designDoc = await db.get("_design/database") + let view = designDoc.views[viewName] + if (view == null) { + throw { status: 404, message: "Unable to get view" } + } + return view +} + +exports.getFromMemoryDoc = async (db, viewName) => { + let view = await db.get(generateMemoryViewID(viewName)) + if (view) { + view = view.view + } else { + throw { status: 404, message: "Unable to get view" } + } + return view +}