From 716eab5d107ebabea5d0f17fb8d38a21193a328d Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Tue, 26 Apr 2022 17:13:45 +0100 Subject: [PATCH] Bypass couch db when using tenancy middleware --- packages/backend-core/src/context/index.js | 9 +++++++-- packages/backend-core/src/environment.js | 9 +++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/backend-core/src/context/index.js b/packages/backend-core/src/context/index.js index acf8434d3b..b6b6f2380c 100644 --- a/packages/backend-core/src/context/index.js +++ b/packages/backend-core/src/context/index.js @@ -71,7 +71,9 @@ exports.doInTenant = (tenantId, task) => { // set the tenant id if (!opts.existing) { cls.setOnContext(ContextKeys.TENANT_ID, tenantId) - exports.setGlobalDB(tenantId) + if (env.USE_COUCH) { + exports.setGlobalDB(tenantId) + } } try { @@ -80,7 +82,9 @@ exports.doInTenant = (tenantId, task) => { } finally { const using = cls.getFromContext(ContextKeys.IN_USE) if (!using || using <= 1) { - await closeDB(exports.getGlobalDB()) + if (env.USE_COUCH) { + await closeDB(exports.getGlobalDB()) + } // clear from context now that database is closed/task is finished cls.setOnContext(ContextKeys.TENANT_ID, null) cls.setOnContext(ContextKeys.GLOBAL_DB, null) @@ -167,6 +171,7 @@ exports.doInAppContext = (appId, task) => { exports.updateTenantId = tenantId => { cls.setOnContext(ContextKeys.TENANT_ID, tenantId) + exports.setGlobalDB(tenantId) } exports.updateAppId = async appId => { diff --git a/packages/backend-core/src/environment.js b/packages/backend-core/src/environment.js index 527760c1ca..8a92e39469 100644 --- a/packages/backend-core/src/environment.js +++ b/packages/backend-core/src/environment.js @@ -30,9 +30,18 @@ module.exports = { COOKIE_DOMAIN: process.env.COOKIE_DOMAIN, PLATFORM_URL: process.env.PLATFORM_URL, TENANT_FEATURE_FLAGS: process.env.TENANT_FEATURE_FLAGS, + USE_COUCH: process.env.USE_COUCH || true, isTest, _set(key, value) { process.env[key] = value module.exports[key] = value }, } + +// clean up any environment variable edge cases +for (let [key, value] of Object.entries(module.exports)) { + // handle the edge case of "0" to disable an environment variable + if (value === "0") { + module.exports[key] = 0 + } +}