Quick fix for REST schema being updated by user, when the request is sent again/query performed again user updates are lost - this means that changes are maintained across runs.
This commit is contained in:
parent
b63f349015
commit
96327eaef1
|
@ -9,7 +9,7 @@ import { quotas } from "@budibase/pro"
|
|||
import { events, context, utils, constants } from "@budibase/backend-core"
|
||||
import sdk from "../../../sdk"
|
||||
import { QueryEvent } from "../../../threads/definitions"
|
||||
import { Query } from "@budibase/types"
|
||||
import { Query, UserCtx } from "@budibase/types"
|
||||
import { ValidQueryNameRegex } from "@budibase/shared-core"
|
||||
|
||||
const Runner = new Thread(ThreadType.QUERY, {
|
||||
|
@ -28,11 +28,11 @@ function enrichQueries(input: any) {
|
|||
return wasArray ? queries : queries[0]
|
||||
}
|
||||
|
||||
export async function fetch(ctx: any) {
|
||||
export async function fetch(ctx: UserCtx) {
|
||||
ctx.body = await sdk.queries.fetch()
|
||||
}
|
||||
|
||||
const _import = async (ctx: any) => {
|
||||
const _import = async (ctx: UserCtx) => {
|
||||
const body = ctx.request.body
|
||||
const data = body.data
|
||||
|
||||
|
@ -73,7 +73,7 @@ const _import = async (ctx: any) => {
|
|||
}
|
||||
export { _import as import }
|
||||
|
||||
export async function save(ctx: any) {
|
||||
export async function save(ctx: UserCtx) {
|
||||
const db = context.getAppDB()
|
||||
const query = ctx.request.body
|
||||
|
||||
|
@ -100,19 +100,19 @@ export async function save(ctx: any) {
|
|||
ctx.message = `Query ${query.name} saved successfully.`
|
||||
}
|
||||
|
||||
export async function find(ctx: any) {
|
||||
export async function find(ctx: UserCtx) {
|
||||
const queryId = ctx.params.queryId
|
||||
ctx.body = await sdk.queries.find(queryId)
|
||||
}
|
||||
|
||||
//Required to discern between OIDC OAuth config entries
|
||||
function getOAuthConfigCookieId(ctx: any) {
|
||||
function getOAuthConfigCookieId(ctx: UserCtx) {
|
||||
if (ctx.user.providerType === constants.Config.OIDC) {
|
||||
return utils.getCookie(ctx, constants.Cookie.OIDC_CONFIG)
|
||||
}
|
||||
}
|
||||
|
||||
function getAuthConfig(ctx: any) {
|
||||
function getAuthConfig(ctx: UserCtx) {
|
||||
const authCookie = utils.getCookie(ctx, constants.Cookie.Auth)
|
||||
let authConfigCtx: any = {}
|
||||
authConfigCtx["configId"] = getOAuthConfigCookieId(ctx)
|
||||
|
@ -120,7 +120,7 @@ function getAuthConfig(ctx: any) {
|
|||
return authConfigCtx
|
||||
}
|
||||
|
||||
export async function preview(ctx: any) {
|
||||
export async function preview(ctx: UserCtx) {
|
||||
const { datasource, envVars } = await sdk.datasources.getWithEnvVars(
|
||||
ctx.request.body.datasourceId
|
||||
)
|
||||
|
@ -129,6 +129,19 @@ export async function preview(ctx: any) {
|
|||
// this stops dynamic variables from calling the same query
|
||||
const { fields, parameters, queryVerb, transformer, queryId, schema } = query
|
||||
|
||||
let existingSchema = schema
|
||||
if (queryId && !existingSchema) {
|
||||
try {
|
||||
const db = context.getAppDB()
|
||||
const existing = (await db.get(queryId)) as Query
|
||||
existingSchema = existing.schema
|
||||
} catch (err) {
|
||||
if (err.status !== 404) {
|
||||
ctx.throw(500, "Unable to retrieve existing query")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const authConfigCtx: any = getAuthConfig(ctx)
|
||||
|
||||
try {
|
||||
|
@ -180,6 +193,14 @@ export async function preview(ctx: any) {
|
|||
schemaFields[key] = fieldType
|
||||
}
|
||||
}
|
||||
// if existing schema, update to include any previous schema keys
|
||||
if (existingSchema) {
|
||||
for (let key of Object.keys(schemaFields)) {
|
||||
if (existingSchema[key]?.type) {
|
||||
schemaFields[key] = existingSchema[key].type
|
||||
}
|
||||
}
|
||||
}
|
||||
// remove configuration before sending event
|
||||
delete datasource.config
|
||||
await events.query.previewed(datasource, query)
|
||||
|
@ -195,7 +216,7 @@ export async function preview(ctx: any) {
|
|||
}
|
||||
|
||||
async function execute(
|
||||
ctx: any,
|
||||
ctx: UserCtx,
|
||||
opts: any = { rowsOnly: false, isAutomation: false }
|
||||
) {
|
||||
const db = context.getAppDB()
|
||||
|
@ -260,12 +281,12 @@ async function execute(
|
|||
}
|
||||
}
|
||||
|
||||
export async function executeV1(ctx: any) {
|
||||
export async function executeV1(ctx: UserCtx) {
|
||||
return execute(ctx, { rowsOnly: true, isAutomation: false })
|
||||
}
|
||||
|
||||
export async function executeV2(
|
||||
ctx: any,
|
||||
ctx: UserCtx,
|
||||
{ isAutomation }: { isAutomation?: boolean } = {}
|
||||
) {
|
||||
return execute(ctx, { rowsOnly: false, isAutomation })
|
||||
|
@ -292,7 +313,7 @@ const removeDynamicVariables = async (queryId: any) => {
|
|||
}
|
||||
}
|
||||
|
||||
export async function destroy(ctx: any) {
|
||||
export async function destroy(ctx: UserCtx) {
|
||||
const db = context.getAppDB()
|
||||
const queryId = ctx.params.queryId
|
||||
await removeDynamicVariables(queryId)
|
||||
|
|
|
@ -11,12 +11,7 @@ export interface QueryEvent {
|
|||
queryId: string
|
||||
environmentVariables?: Record<string, string>
|
||||
ctx?: any
|
||||
schema?: {
|
||||
[key: string]: {
|
||||
name: string
|
||||
type: string
|
||||
}
|
||||
}
|
||||
schema?: Record<string, { name?: string; type: string }>
|
||||
}
|
||||
|
||||
export interface QueryVariable {
|
||||
|
|
|
@ -6,7 +6,7 @@ export interface Query extends Document {
|
|||
parameters: QueryParameter[]
|
||||
fields: RestQueryFields | any
|
||||
transformer: string | null
|
||||
schema: any
|
||||
schema: Record<string, { name?: string; type: string }>
|
||||
readable: boolean
|
||||
queryVerb: string
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue