Bypass couch db when using tenancy middleware

This commit is contained in:
Rory Powell 2022-04-26 17:13:45 +01:00
parent e2bef2a323
commit 716eab5d10
2 changed files with 16 additions and 2 deletions

View File

@ -71,7 +71,9 @@ exports.doInTenant = (tenantId, task) => {
// set the tenant id // set the tenant id
if (!opts.existing) { if (!opts.existing) {
cls.setOnContext(ContextKeys.TENANT_ID, tenantId) cls.setOnContext(ContextKeys.TENANT_ID, tenantId)
exports.setGlobalDB(tenantId) if (env.USE_COUCH) {
exports.setGlobalDB(tenantId)
}
} }
try { try {
@ -80,7 +82,9 @@ exports.doInTenant = (tenantId, task) => {
} finally { } finally {
const using = cls.getFromContext(ContextKeys.IN_USE) const using = cls.getFromContext(ContextKeys.IN_USE)
if (!using || using <= 1) { 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 // clear from context now that database is closed/task is finished
cls.setOnContext(ContextKeys.TENANT_ID, null) cls.setOnContext(ContextKeys.TENANT_ID, null)
cls.setOnContext(ContextKeys.GLOBAL_DB, null) cls.setOnContext(ContextKeys.GLOBAL_DB, null)
@ -167,6 +171,7 @@ exports.doInAppContext = (appId, task) => {
exports.updateTenantId = tenantId => { exports.updateTenantId = tenantId => {
cls.setOnContext(ContextKeys.TENANT_ID, tenantId) cls.setOnContext(ContextKeys.TENANT_ID, tenantId)
exports.setGlobalDB(tenantId)
} }
exports.updateAppId = async appId => { exports.updateAppId = async appId => {

View File

@ -30,9 +30,18 @@ module.exports = {
COOKIE_DOMAIN: process.env.COOKIE_DOMAIN, COOKIE_DOMAIN: process.env.COOKIE_DOMAIN,
PLATFORM_URL: process.env.PLATFORM_URL, PLATFORM_URL: process.env.PLATFORM_URL,
TENANT_FEATURE_FLAGS: process.env.TENANT_FEATURE_FLAGS, TENANT_FEATURE_FLAGS: process.env.TENANT_FEATURE_FLAGS,
USE_COUCH: process.env.USE_COUCH || true,
isTest, isTest,
_set(key, value) { _set(key, value) {
process.env[key] = value process.env[key] = value
module.exports[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
}
}