diff --git a/packages/backend-core/src/db/conversions.js b/packages/backend-core/src/db/conversions.js index 766ec1ad06..50d896322f 100644 --- a/packages/backend-core/src/db/conversions.js +++ b/packages/backend-core/src/db/conversions.js @@ -25,7 +25,7 @@ exports.isDevApp = app => { /** * Convert a development app ID to a deployed app ID. */ -exports.getDeployedAppID = appId => { +exports.getProdAppID = appId => { // if dev, convert it if (appId.startsWith(APP_DEV_PREFIX)) { const id = appId.split(APP_DEV_PREFIX)[1] diff --git a/packages/backend-core/src/db/utils.js b/packages/backend-core/src/db/utils.js index 7190a1221b..2800cf43c2 100644 --- a/packages/backend-core/src/db/utils.js +++ b/packages/backend-core/src/db/utils.js @@ -23,7 +23,7 @@ const { isProdAppID, isDevAppID, getDevelopmentAppID, - getDeployedAppID, + getProdAppID, } = require("./conversions") const UNICODE_MAX = "\ufff0" @@ -43,7 +43,7 @@ exports.isDevApp = isDevApp exports.isProdAppID = isProdAppID exports.isDevAppID = isDevAppID exports.getDevelopmentAppID = getDevelopmentAppID -exports.getDeployedAppID = getDeployedAppID +exports.getProdAppID = getProdAppID /** * If creating DB allDocs/query params with only a single top level ID this can be used, this @@ -197,7 +197,7 @@ exports.getAllDbs = async () => { } let couchUrl = `${exports.getCouchUrl()}/_all_dbs` let tenantId = getTenantId() - if (!env.MULTI_TENANCY || tenantId == DEFAULT_TENANT_ID) { + if (!env.MULTI_TENANCY || tenantId === DEFAULT_TENANT_ID) { // just get all DBs when: // - single tenancy // - default tenant @@ -281,7 +281,7 @@ exports.getAllApps = async ({ dev, all, idsOnly } = {}) => { /** * Utility function for getAllApps but filters to production apps only. */ -exports.getDeployedAppIDs = async () => { +exports.getProdAppIDs = async () => { return (await exports.getAllApps({ idsOnly: true })).filter( id => !exports.isDevAppID(id) ) diff --git a/packages/backend-core/src/tenancy/context.js b/packages/backend-core/src/tenancy/context.js index 3d2c862d71..1c1238278e 100644 --- a/packages/backend-core/src/tenancy/context.js +++ b/packages/backend-core/src/tenancy/context.js @@ -2,7 +2,7 @@ const env = require("../environment") const { Headers } = require("../../constants") const cls = require("./FunctionContext") const { getCouch } = require("../db") -const { getDeployedAppID, getDevelopmentAppID } = require("../db/conversions") +const { getProdAppID, getDevelopmentAppID } = require("../db/conversions") const { isEqual } = require("lodash") // some test cases call functions directly, need to @@ -150,7 +150,7 @@ function getDB(key, opts) { toUseAppId = appId break case ContextKeys.PROD_DB: - toUseAppId = getDeployedAppID(appId) + toUseAppId = getProdAppID(appId) break case ContextKeys.DEV_DB: toUseAppId = getDevelopmentAppID(appId) diff --git a/packages/server/src/api/controllers/application.js b/packages/server/src/api/controllers/application.js index c16c54f1e3..00d3efccb8 100644 --- a/packages/server/src/api/controllers/application.js +++ b/packages/server/src/api/controllers/application.js @@ -28,7 +28,7 @@ const { processObject } = require("@budibase/string-templates") const { getAllApps, isDevAppID, - getDeployedAppID, + getProdAppID, Replication, } = require("@budibase/backend-core/db") const { USERS_TABLE_SCHEMA } = require("../../constants") @@ -44,13 +44,17 @@ const { getTenantId, isMultiTenant } = require("@budibase/backend-core/tenancy") const { syncGlobalUsers } = require("./user") const { app: appCache } = require("@budibase/backend-core/cache") const { cleanupAutomations } = require("../../automations/utils") -const context = require("@budibase/backend-core/context") +const { + getAppDB, + getProdAppDB, + updateAppId, +} = require("@budibase/backend-core/context") const URL_REGEX_SLASH = /\/|\\/g // utility function, need to do away with this async function getLayouts() { - const db = context.getAppDB() + const db = getAppDB() return ( await db.allDocs( getLayoutParams(null, { @@ -61,7 +65,7 @@ async function getLayouts() { } async function getScreens() { - const db = context.getAppDB() + const db = getAppDB() return ( await db.allDocs( getScreenParams(null, { @@ -119,9 +123,9 @@ async function createInstance(template) { const tenantId = isMultiTenant() ? getTenantId() : null const baseAppId = generateAppID(tenantId) const appId = generateDevAppID(baseAppId) - context.updateAppId(appId) + updateAppId(appId) - const db = context.getAppDB() + const db = getAppDB() await db.put({ _id: "_design/database", // view collation information, read before writing any complex views: @@ -197,7 +201,7 @@ exports.fetchAppDefinition = async ctx => { } exports.fetchAppPackage = async ctx => { - const db = context.getAppDB() + const db = getAppDB() const application = await db.get(DocumentTypes.APP_METADATA) const layouts = await getLayouts() let screens = await getScreens() @@ -236,7 +240,7 @@ exports.create = async ctx => { const instance = await createInstance(instanceConfig) const appId = instance._id - const db = context.getAppDB() + const db = getAppDB() let _rev try { // if template there will be an existing doc @@ -301,7 +305,7 @@ exports.update = async ctx => { exports.updateClient = async ctx => { // Get current app version - const db = context.getAppDB() + const db = getAppDB() const application = await db.get(DocumentTypes.APP_METADATA) const currentVersion = application.version @@ -323,7 +327,7 @@ exports.updateClient = async ctx => { exports.revertClient = async ctx => { // Check app can be reverted - const db = context.getAppDB() + const db = getAppDB() const application = await db.get(DocumentTypes.APP_METADATA) if (!application.revertableVersion) { ctx.throw(400, "There is no version to revert to") @@ -345,7 +349,7 @@ exports.revertClient = async ctx => { } exports.delete = async ctx => { - const db = context.getAppDB() + const db = getAppDB() const result = await db.destroy() /* istanbul ignore next */ @@ -370,11 +374,11 @@ exports.sync = async (ctx, next) => { } // replicate prod to dev - const prodAppId = getDeployedAppID(appId) + const prodAppId = getProdAppID(appId) try { // specific case, want to make sure setup is skipped - const prodDb = context.getProdAppDB({ skip_setup: true }) + const prodDb = getProdAppDB({ skip_setup: true }) const info = await prodDb.info() if (info.error) throw info.error } catch (err) { @@ -414,7 +418,7 @@ exports.sync = async (ctx, next) => { } const updateAppPackage = async (appPackage, appId) => { - const db = context.getAppDB() + const db = getAppDB() const application = await db.get(DocumentTypes.APP_METADATA) const newAppPackage = { ...application, ...appPackage } @@ -433,7 +437,7 @@ const updateAppPackage = async (appPackage, appId) => { } const createEmptyAppPackage = async (ctx, app) => { - const db = context.getAppDB() + const db = getAppDB() let screensAndLayouts = [] for (let layout of BASE_LAYOUTS) { diff --git a/packages/server/src/api/controllers/deploy/index.js b/packages/server/src/api/controllers/deploy/index.js index 22c7d5ce3a..4186a192a4 100644 --- a/packages/server/src/api/controllers/deploy/index.js +++ b/packages/server/src/api/controllers/deploy/index.js @@ -1,7 +1,7 @@ const Deployment = require("./Deployment") const { Replication, - getDeployedAppID, + getProdAppID, getDevelopmentAppID, } = require("@budibase/backend-core/db") const { DocumentTypes, getAutomationParams } = require("../../../db/utils") @@ -97,7 +97,7 @@ async function deployApp(deployment) { try { const appId = getAppId() const devAppId = getDevelopmentAppID(appId) - const productionAppId = getDeployedAppID(appId) + const productionAppId = getProdAppID(appId) const replication = new Replication({ source: devAppId, diff --git a/packages/server/src/api/controllers/dev.js b/packages/server/src/api/controllers/dev.js index a27fab9a83..bec9478245 100644 --- a/packages/server/src/api/controllers/dev.js +++ b/packages/server/src/api/controllers/dev.js @@ -3,7 +3,7 @@ const env = require("../../environment") const { checkSlashesInUrl } = require("../../utilities") const { request } = require("../../utilities/workerRequests") const { clearLock } = require("../../utilities/redis") -const { Replication, getDeployedAppID } = require("@budibase/backend-core/db") +const { Replication, getProdAppID } = require("@budibase/backend-core/db") const { DocumentTypes } = require("../../db/utils") const { app: appCache } = require("@budibase/backend-core/cache") const { getProdAppDB, getAppDB } = require("@budibase/backend-core/context") @@ -77,7 +77,7 @@ exports.clearLock = async ctx => { exports.revert = async ctx => { const { appId } = ctx.params - const productionAppId = getDeployedAppID(appId) + const productionAppId = getProdAppID(appId) // App must have been deployed first try { diff --git a/packages/server/src/api/controllers/user.js b/packages/server/src/api/controllers/user.js index 208d3a60a3..ca7ef24162 100644 --- a/packages/server/src/api/controllers/user.js +++ b/packages/server/src/api/controllers/user.js @@ -10,7 +10,7 @@ const { isEqual } = require("lodash") const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles") const { getDevelopmentAppID, - getDeployedAppIDs, + getProdAppIDs, dbExists, } = require("@budibase/backend-core/db") const { UserStatus } = require("@budibase/backend-core/constants") @@ -92,7 +92,7 @@ exports.syncUser = async function (ctx) { let prodAppIds // if they are a builder then get all production app IDs if ((user.builder && user.builder.global) || deleting) { - prodAppIds = await getDeployedAppIDs() + prodAppIds = await getProdAppIDs() } else { prodAppIds = Object.entries(roles) .filter(entry => entry[1] !== BUILTIN_ROLE_IDS.PUBLIC) diff --git a/packages/server/src/api/controllers/webhook.js b/packages/server/src/api/controllers/webhook.js index 7375b3e750..49ab652cbf 100644 --- a/packages/server/src/api/controllers/webhook.js +++ b/packages/server/src/api/controllers/webhook.js @@ -2,7 +2,7 @@ const { generateWebhookID, getWebhookParams } = require("../../db/utils") const toJsonSchema = require("to-json-schema") const validate = require("jsonschema").validate const triggers = require("../../automations/triggers") -const { getDeployedAppID } = require("@budibase/backend-core/db") +const { getProdAppID } = require("@budibase/backend-core/db") const { getAppDB, updateAppId } = require("@budibase/backend-core/context") const AUTOMATION_DESCRIPTION = "Generated from Webhook Schema" @@ -82,8 +82,8 @@ exports.buildSchema = async ctx => { } exports.trigger = async ctx => { - const deployedAppId = getDeployedAppID(ctx.params.instance) - updateAppId(deployedAppId) + const prodAppId = getProdAppID(ctx.params.instance) + updateAppId(prodAppId) try { const db = getAppDB() const webhook = await db.get(ctx.params.id) @@ -98,7 +98,7 @@ exports.trigger = async ctx => { await triggers.externalTrigger(target, { body: ctx.request.body, ...ctx.request.body, - appId: deployedAppId, + appId: prodAppId, }) } ctx.status = 200 diff --git a/packages/server/src/automations/utils.js b/packages/server/src/automations/utils.js index 1d8a03f26d..3ee1f535c7 100644 --- a/packages/server/src/automations/utils.js +++ b/packages/server/src/automations/utils.js @@ -6,7 +6,7 @@ const { queue } = require("./bullboard") const newid = require("../db/newid") const { updateEntityMetadata } = require("../utilities") const { MetadataTypes } = require("../constants") -const { getDeployedAppID } = require("@budibase/backend-core/db") +const { getProdAppID } = require("@budibase/backend-core/db") const { cloneDeep } = require("lodash/fp") const { getAppDB, getAppId } = require("@budibase/backend-core/context") @@ -170,7 +170,7 @@ exports.checkForWebhooks = async ({ oldAuto, newAuto }) => { // the app ID has to be development for this endpoint // it can only be used when building the app // but the trigger endpoint will always be used in production - const prodAppId = getDeployedAppID(appId) + const prodAppId = getProdAppID(appId) newTrigger.inputs = { schemaUrl: `api/webhooks/schema/${appId}/${id}`, triggerUrl: `api/webhooks/trigger/${prodAppId}/${id}`, diff --git a/packages/server/src/utilities/global.js b/packages/server/src/utilities/global.js index 317d80689a..f8ec5ea647 100644 --- a/packages/server/src/utilities/global.js +++ b/packages/server/src/utilities/global.js @@ -3,7 +3,7 @@ const { getGlobalIDFromUserMetadataID, } = require("../db/utils") const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles") -const { getDeployedAppID } = require("@budibase/backend-core/db") +const { getProdAppID } = require("@budibase/backend-core/db") const { getGlobalUserParams } = require("@budibase/backend-core/db") const { user: userCache } = require("@budibase/backend-core/cache") const { @@ -26,7 +26,7 @@ exports.updateAppRole = (user, { appId } = {}) => { return user } // always use the deployed app - user.roleId = user.roles[getDeployedAppID(appId)] + user.roleId = user.roles[getProdAppID(appId)] // if a role wasn't found then either set as admin (builder) or public (everyone else) if (!user.roleId && user.builder && user.builder.global) { user.roleId = BUILTIN_ROLE_IDS.ADMIN diff --git a/packages/server/src/utilities/rowProcessor/index.js b/packages/server/src/utilities/rowProcessor/index.js index ab1a4bf92a..18e0b14de6 100644 --- a/packages/server/src/utilities/rowProcessor/index.js +++ b/packages/server/src/utilities/rowProcessor/index.js @@ -7,7 +7,7 @@ const { deleteFiles } = require("../../utilities/fileSystem/utilities") const { ObjectStoreBuckets } = require("../../constants") const { isProdAppID, - getDeployedAppID, + getProdAppID, dbExists, } = require("@budibase/backend-core/db") const { getAppId } = require("@budibase/backend-core/context") @@ -303,7 +303,7 @@ exports.outputProcessing = async (table, rows, opts = { squash: true }) => { exports.cleanupAttachments = async (table, { row, rows, oldRow, oldTable }) => { const appId = getAppId() if (!isProdAppID(appId)) { - const prodAppId = getDeployedAppID(appId) + const prodAppId = getProdAppID(appId) // if prod exists, then don't allow deleting const exists = await dbExists(prodAppId) if (exists) { diff --git a/packages/server/src/utilities/workerRequests.js b/packages/server/src/utilities/workerRequests.js index a7fa92b295..91db63d2a4 100644 --- a/packages/server/src/utilities/workerRequests.js +++ b/packages/server/src/utilities/workerRequests.js @@ -1,7 +1,7 @@ const fetch = require("node-fetch") const env = require("../environment") const { checkSlashesInUrl } = require("./index") -const { getDeployedAppID } = require("@budibase/backend-core/db") +const { getProdAppID } = require("@budibase/backend-core/db") const { updateAppRole } = require("./global") const { Headers } = require("@budibase/backend-core/constants") const { getTenantId, isTenantIdSet } = require("@budibase/backend-core/tenancy") @@ -76,9 +76,9 @@ exports.getGlobalSelf = async (ctx, appId = null) => { } exports.removeAppFromUserRoles = async (ctx, appId) => { - const deployedAppId = getDeployedAppID(appId) + const prodAppId = getProdAppID(appId) const response = await fetch( - checkSlashesInUrl(env.WORKER_URL + `/api/global/roles/${deployedAppId}`), + checkSlashesInUrl(env.WORKER_URL + `/api/global/roles/${prodAppId}`), request(ctx, { method: "DELETE", }) diff --git a/packages/worker/src/api/controllers/global/roles.js b/packages/worker/src/api/controllers/global/roles.js index 72fd42d452..96de0e4753 100644 --- a/packages/worker/src/api/controllers/global/roles.js +++ b/packages/worker/src/api/controllers/global/roles.js @@ -1,7 +1,7 @@ const { getAllRoles } = require("@budibase/backend-core/roles") const { getAllApps, - getDeployedAppID, + getProdAppID, DocumentTypes, } = require("@budibase/backend-core/db") const { doInAppContext, getAppDB } = require("@budibase/backend-core/context") @@ -18,7 +18,7 @@ exports.fetch = async ctx => { const roles = await Promise.all(promises) const response = {} for (let app of apps) { - const deployedAppId = getDeployedAppID(app.appId) + const deployedAppId = getProdAppID(app.appId) response[deployedAppId] = { roles: roles.shift(), name: app.name,