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