1
0
Fork 0
mirror of synced 2024-10-03 10:36:59 +13:00

Delete and invalidate linked dynamic variables when a query is deleted

This commit is contained in:
Rory Powell 2022-01-05 11:54:59 -05:00
parent e115a561c2
commit 8210ed7ee4
3 changed files with 24 additions and 2 deletions

View file

@ -66,7 +66,7 @@ export class OpenAPI2 extends OpenAPISource {
getInfo = async (): Promise<ImportInfo> => {
const name = this.document.info.title || "Swagger Import"
return {
name
name,
}
}

View file

@ -8,6 +8,7 @@ const { BaseQueryVerbs } = require("../../../constants")
const { Thread, ThreadType } = require("../../../threads")
const { save: saveDatasource } = require("../datasource")
const { RestImporter } = require("./import")
const { invalidateDynamicVariables } = require("../../../threads/utils")
const Runner = new Thread(ThreadType.QUERY, { timeoutMs: 10000 })
@ -166,8 +167,28 @@ exports.executeV2 = async function (ctx) {
return execute(ctx, { rowsOnly: false })
}
const removeDynamicVariables = async (db, queryId) => {
const query = await db.get(queryId)
const datasource = await db.get(query.datasourceId)
const dynamicVariables = datasource.config.dynamicVariables
if (dynamicVariables) {
// invalidate the deleted variables
const variablesToDelete = dynamicVariables.filter(
dv => dv.queryId === queryId
)
await invalidateDynamicVariables(variablesToDelete)
// delete dynamic variables from the datasource
const newVariables = dynamicVariables.filter(dv => dv.queryId !== queryId)
datasource.config.dynamicVariables = newVariables
await db.put(datasource)
}
}
exports.destroy = async function (ctx) {
const db = new CouchDB(ctx.appId)
await removeDynamicVariables(db, ctx.params.queryId)
await db.remove(ctx.params.queryId, ctx.params.revId)
ctx.message = `Query deleted.`
ctx.status = 200

View file

@ -42,10 +42,11 @@ exports.checkCacheForDynamicVariable = async (queryId, variable) => {
}
exports.invalidateDynamicVariables = async cachedVars => {
const cache = await getClient()
let promises = []
for (let variable of cachedVars) {
promises.push(
client.delete(makeVariableKey(variable.queryId, variable.name))
cache.delete(makeVariableKey(variable.queryId, variable.name))
)
}
await Promise.all(promises)