1
0
Fork 0
mirror of synced 2024-07-02 13:01:09 +12:00

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,8 +71,10 @@ 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)
if (env.USE_COUCH) {
exports.setGlobalDB(tenantId) exports.setGlobalDB(tenantId)
} }
}
try { try {
// invoke the task // invoke the task
@ -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) {
if (env.USE_COUCH) {
await closeDB(exports.getGlobalDB()) 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
}
}