Updating query responses to follow the same form consistently.
This commit is contained in:
parent
c48f71ab76
commit
db7c2c804b
|
@ -1,15 +1,21 @@
|
|||
import { generateQueryID } from "../../../db/utils"
|
||||
import { BaseQueryVerbs, FieldTypes } from "../../../constants"
|
||||
import { BaseQueryVerbs } from "../../../constants"
|
||||
import { Thread, ThreadType } from "../../../threads"
|
||||
import { save as saveDatasource } from "../datasource"
|
||||
import { RestImporter } from "./import"
|
||||
import { invalidateDynamicVariables } from "../../../threads/utils"
|
||||
import env from "../../../environment"
|
||||
import { quotas } from "@budibase/pro"
|
||||
import { events, context, utils, constants } from "@budibase/backend-core"
|
||||
import sdk from "../../../sdk"
|
||||
import { QueryEvent } from "../../../threads/definitions"
|
||||
import { ConfigType, Query, UserCtx, SessionCookie } from "@budibase/types"
|
||||
import {
|
||||
ConfigType,
|
||||
Query,
|
||||
UserCtx,
|
||||
SessionCookie,
|
||||
QuerySchema,
|
||||
FieldType,
|
||||
} from "@budibase/types"
|
||||
import { ValidQueryNameRegex } from "@budibase/shared-core"
|
||||
|
||||
const Runner = new Thread(ThreadType.QUERY, {
|
||||
|
@ -163,28 +169,32 @@ export async function preview(ctx: UserCtx) {
|
|||
}
|
||||
|
||||
const { rows, keys, info, extra } = (await Runner.run(inputs)) as any
|
||||
const schemaFields: any = {}
|
||||
const schemaFields: Record<string, QuerySchema> = {}
|
||||
const makeQuerySchema = (type: FieldType, name: string): QuerySchema => ({
|
||||
type,
|
||||
name,
|
||||
})
|
||||
if (rows?.length > 0) {
|
||||
for (let key of [...new Set(keys)] as string[]) {
|
||||
const field = rows[0][key]
|
||||
let type = typeof field,
|
||||
fieldType = FieldTypes.STRING
|
||||
fieldType = makeQuerySchema(FieldType.STRING, key)
|
||||
if (field)
|
||||
switch (type) {
|
||||
case "boolean":
|
||||
schemaFields[key] = FieldTypes.BOOLEAN
|
||||
schemaFields[key] = makeQuerySchema(FieldType.BOOLEAN, key)
|
||||
break
|
||||
case "object":
|
||||
if (field instanceof Date) {
|
||||
fieldType = FieldTypes.DATETIME
|
||||
fieldType = makeQuerySchema(FieldType.DATETIME, key)
|
||||
} else if (Array.isArray(field)) {
|
||||
fieldType = FieldTypes.ARRAY
|
||||
fieldType = makeQuerySchema(FieldType.ARRAY, key)
|
||||
} else {
|
||||
fieldType = FieldTypes.JSON
|
||||
fieldType = makeQuerySchema(FieldType.JSON, key)
|
||||
}
|
||||
break
|
||||
case "number":
|
||||
fieldType = FieldTypes.NUMBER
|
||||
fieldType = makeQuerySchema(FieldType.NUMBER, key)
|
||||
break
|
||||
}
|
||||
schemaFields[key] = fieldType
|
||||
|
|
|
@ -3,6 +3,27 @@ import { processStringSync } from "@budibase/string-templates"
|
|||
import { context } from "@budibase/backend-core"
|
||||
import { getQueryParams, isProdAppID } from "../../../db/utils"
|
||||
import { BaseQueryVerbs } from "../../../constants"
|
||||
import { Query, QuerySchema } from "@budibase/types"
|
||||
|
||||
function updateSchema(query: Query) {
|
||||
if (!query.schema) {
|
||||
return query
|
||||
}
|
||||
const schema: Record<string, QuerySchema> = {}
|
||||
for (let key of Object.keys(query.schema)) {
|
||||
if (typeof query.schema[key] === "string") {
|
||||
schema[key] = { type: query.schema[key] as string, name: key }
|
||||
} else {
|
||||
schema[key] = query.schema[key] as QuerySchema
|
||||
}
|
||||
}
|
||||
query.schema = schema
|
||||
return query
|
||||
}
|
||||
|
||||
function updateSchemas(queries: Query[]) {
|
||||
return queries.map(query => updateSchema(query))
|
||||
}
|
||||
|
||||
// simple function to append "readable" to all read queries
|
||||
function enrichQueries(input: any) {
|
||||
|
@ -25,7 +46,7 @@ export async function find(queryId: string) {
|
|||
delete query.fields
|
||||
delete query.parameters
|
||||
}
|
||||
return query
|
||||
return updateSchema(query)
|
||||
}
|
||||
|
||||
export async function fetch(opts: { enrich: boolean } = { enrich: true }) {
|
||||
|
@ -37,12 +58,11 @@ export async function fetch(opts: { enrich: boolean } = { enrich: true }) {
|
|||
})
|
||||
)
|
||||
|
||||
const queries = body.rows.map((row: any) => row.doc)
|
||||
let queries = body.rows.map((row: any) => row.doc)
|
||||
if (opts.enrich) {
|
||||
return enrichQueries(queries)
|
||||
} else {
|
||||
return queries
|
||||
queries = await enrichQueries(queries)
|
||||
}
|
||||
return updateSchemas(queries)
|
||||
}
|
||||
|
||||
export async function enrichContext(
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { QuerySchema } from "@budibase/types"
|
||||
|
||||
export type WorkerCallback = (error: any, response?: any) => void
|
||||
|
||||
export interface QueryEvent {
|
||||
|
@ -11,7 +13,7 @@ export interface QueryEvent {
|
|||
queryId: string
|
||||
environmentVariables?: Record<string, string>
|
||||
ctx?: any
|
||||
schema?: Record<string, { name?: string; type: string }>
|
||||
schema?: Record<string, QuerySchema | string>
|
||||
}
|
||||
|
||||
export interface QueryVariable {
|
||||
|
|
|
@ -1,12 +1,17 @@
|
|||
import { Document } from "../document"
|
||||
|
||||
export interface QuerySchema {
|
||||
name?: string
|
||||
type: string
|
||||
}
|
||||
|
||||
export interface Query extends Document {
|
||||
datasourceId: string
|
||||
name: string
|
||||
parameters: QueryParameter[]
|
||||
fields: RestQueryFields | any
|
||||
transformer: string | null
|
||||
schema: Record<string, { name?: string; type: string }>
|
||||
schema: Record<string, QuerySchema | string>
|
||||
readable: boolean
|
||||
queryVerb: string
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue