Refactored approach to ensure the request context is set appropriately

This commit is contained in:
Dean 2022-05-26 10:03:29 +01:00
parent 550f6f4d61
commit e9554562bb
2 changed files with 17 additions and 16 deletions

View File

@ -298,9 +298,6 @@ function getContextDB(key, opts) {
break break
} }
//If the appId is specified, override context
toUseAppId = opts?.appId ? opts?.appId : toUseAppId
db = dangerousGetDB(toUseAppId, opts) db = dangerousGetDB(toUseAppId, opts)
try { try {
cls.setOnContext(key, db) cls.setOnContext(key, db)

View File

@ -49,6 +49,7 @@ const {
getAppDB, getAppDB,
getProdAppDB, getProdAppDB,
updateAppId, updateAppId,
doInAppContext,
} = require("@budibase/backend-core/context") } = require("@budibase/backend-core/context")
import { getUniqueRows } from "../../utilities/usageQuota/rows" import { getUniqueRows } from "../../utilities/usageQuota/rows"
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
@ -340,6 +341,7 @@ export const update = async (ctx: any) => {
const data = await updateAppPackage(ctx.request.body, ctx.params.appId) const data = await updateAppPackage(ctx.request.body, ctx.params.appId)
ctx.status = 200 ctx.status = 200
ctx.body = data ctx.body = data
console.log(JSON.stringify(data))
} }
export const updateClient = async (ctx: any) => { export const updateClient = async (ctx: any) => {
@ -502,22 +504,24 @@ export const sync = async (ctx: any, next: any) => {
} }
const updateAppPackage = async (appPackage: any, appId: any) => { const updateAppPackage = async (appPackage: any, appId: any) => {
const db = getAppDB({ appId }) return doInAppContext(appId, async () => {
const application = await db.get(DocumentTypes.APP_METADATA) const db = getAppDB()
const application = await db.get(DocumentTypes.APP_METADATA)
const newAppPackage = { ...application, ...appPackage } const newAppPackage = { ...application, ...appPackage }
if (appPackage._rev !== application._rev) { if (appPackage._rev !== application._rev) {
newAppPackage._rev = application._rev newAppPackage._rev = application._rev
} }
// the locked by property is attached by server but generated from // the locked by property is attached by server but generated from
// Redis, shouldn't ever store it // Redis, shouldn't ever store it
delete newAppPackage.lockedBy delete newAppPackage.lockedBy
const response = await db.put(newAppPackage) const response = await db.put(newAppPackage)
// remove any cached metadata, so that it will be updated // remove any cached metadata, so that it will be updated
await appCache.invalidateAppMetadata(appId) await appCache.invalidateAppMetadata(appId)
return response return response
})
} }
const createEmptyAppPackage = async (ctx: any, app: any) => { const createEmptyAppPackage = async (ctx: any, app: any) => {