Migrate QueryAPI
This commit is contained in:
parent
f5f81a5fb0
commit
16e9c5ff4e
|
@ -17,10 +17,12 @@ import {
|
||||||
QueryPreview,
|
QueryPreview,
|
||||||
QuerySchema,
|
QuerySchema,
|
||||||
FieldType,
|
FieldType,
|
||||||
type ExecuteQueryRequest,
|
ExecuteQueryRequest,
|
||||||
type ExecuteQueryResponse,
|
ExecuteQueryResponse,
|
||||||
type Row,
|
Row,
|
||||||
QueryParameter,
|
QueryParameter,
|
||||||
|
PreviewQueryRequest,
|
||||||
|
PreviewQueryResponse,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { ValidQueryNameRegex, utils as JsonUtils } from "@budibase/shared-core"
|
import { ValidQueryNameRegex, utils as JsonUtils } from "@budibase/shared-core"
|
||||||
|
|
||||||
|
@ -134,14 +136,16 @@ function enrichParameters(
|
||||||
return requestParameters
|
return requestParameters
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function preview(ctx: UserCtx) {
|
export async function preview(
|
||||||
|
ctx: UserCtx<PreviewQueryRequest, PreviewQueryResponse>
|
||||||
|
) {
|
||||||
const { datasource, envVars } = await sdk.datasources.getWithEnvVars(
|
const { datasource, envVars } = await sdk.datasources.getWithEnvVars(
|
||||||
ctx.request.body.datasourceId
|
ctx.request.body.datasourceId
|
||||||
)
|
)
|
||||||
const query: QueryPreview = ctx.request.body
|
|
||||||
// preview may not have a queryId as it hasn't been saved, but if it does
|
// preview may not have a queryId as it hasn't been saved, but if it does
|
||||||
// this stops dynamic variables from calling the same query
|
// this stops dynamic variables from calling the same query
|
||||||
const { fields, parameters, queryVerb, transformer, queryId, schema } = query
|
const { fields, parameters, queryVerb, transformer, queryId, schema } =
|
||||||
|
ctx.request.body
|
||||||
|
|
||||||
let existingSchema = schema
|
let existingSchema = schema
|
||||||
if (queryId && !existingSchema) {
|
if (queryId && !existingSchema) {
|
||||||
|
@ -266,9 +270,7 @@ export async function preview(ctx: UserCtx) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const { rows, keys, info, extra } = (await Runner.run(
|
const { rows, keys, info, extra } = await Runner.run<QueryResponse>(inputs)
|
||||||
inputs
|
|
||||||
)) as QueryResponse
|
|
||||||
const { previewSchema, nestedSchemaFields } = getSchemaFields(rows, keys)
|
const { previewSchema, nestedSchemaFields } = getSchemaFields(rows, keys)
|
||||||
|
|
||||||
// if existing schema, update to include any previous schema keys
|
// if existing schema, update to include any previous schema keys
|
||||||
|
@ -281,7 +283,7 @@ export async function preview(ctx: UserCtx) {
|
||||||
}
|
}
|
||||||
// remove configuration before sending event
|
// remove configuration before sending event
|
||||||
delete datasource.config
|
delete datasource.config
|
||||||
await events.query.previewed(datasource, query)
|
await events.query.previewed(datasource, ctx.request.body)
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
rows,
|
rows,
|
||||||
nestedSchemaFields,
|
nestedSchemaFields,
|
||||||
|
@ -295,7 +297,7 @@ export async function preview(ctx: UserCtx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function execute(
|
async function execute(
|
||||||
ctx: UserCtx<ExecuteQueryRequest, ExecuteQueryResponse | Row[]>,
|
ctx: UserCtx<ExecuteQueryRequest, ExecuteQueryResponse | any[]>,
|
||||||
opts: any = { rowsOnly: false, isAutomation: false }
|
opts: any = { rowsOnly: false, isAutomation: false }
|
||||||
) {
|
) {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
|
|
|
@ -1,60 +1,29 @@
|
||||||
import TestConfiguration from "../TestConfiguration"
|
|
||||||
import {
|
import {
|
||||||
Query,
|
Query,
|
||||||
QueryPreview,
|
ExecuteQueryRequest,
|
||||||
type ExecuteQueryRequest,
|
ExecuteQueryResponse,
|
||||||
type ExecuteQueryResponse,
|
PreviewQueryRequest,
|
||||||
|
PreviewQueryResponse,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { TestAPI } from "./base"
|
import { TestAPI } from "./base"
|
||||||
|
|
||||||
export class QueryAPI extends TestAPI {
|
export class QueryAPI extends TestAPI {
|
||||||
constructor(config: TestConfiguration) {
|
|
||||||
super(config)
|
|
||||||
}
|
|
||||||
|
|
||||||
create = async (body: Query): Promise<Query> => {
|
create = async (body: Query): Promise<Query> => {
|
||||||
const res = await this.request
|
return await this._post<Query>(`/api/queries`, { body })
|
||||||
.post(`/api/queries`)
|
|
||||||
.set(this.config.defaultHeaders())
|
|
||||||
.send(body)
|
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
|
|
||||||
if (res.status !== 200) {
|
|
||||||
throw new Error(JSON.stringify(res.body))
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.body as Query
|
|
||||||
}
|
}
|
||||||
|
|
||||||
execute = async (
|
execute = async (
|
||||||
queryId: string,
|
queryId: string,
|
||||||
body?: ExecuteQueryRequest
|
body?: ExecuteQueryRequest
|
||||||
): Promise<ExecuteQueryResponse> => {
|
): Promise<ExecuteQueryResponse> => {
|
||||||
const res = await this.request
|
return await this._post<ExecuteQueryResponse>(`/api/queries/${queryId}`, {
|
||||||
.post(`/api/v2/queries/${queryId}`)
|
body,
|
||||||
.set(this.config.defaultHeaders())
|
})
|
||||||
.send(body)
|
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
|
|
||||||
if (res.status !== 200) {
|
|
||||||
throw new Error(JSON.stringify(res.body))
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.body
|
|
||||||
}
|
}
|
||||||
|
|
||||||
previewQuery = async (queryPreview: QueryPreview) => {
|
previewQuery = async (queryPreview: PreviewQueryRequest) => {
|
||||||
const res = await this.request
|
return await this._post<PreviewQueryResponse>(`/api/queries/preview`, {
|
||||||
.post(`/api/queries/preview`)
|
body: queryPreview,
|
||||||
.send(queryPreview)
|
})
|
||||||
.set(this.config.defaultHeaders())
|
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
.expect(200)
|
|
||||||
|
|
||||||
if (res.status !== 200) {
|
|
||||||
throw new Error(JSON.stringify(res.body))
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.body
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,3 +13,4 @@ export * from "./searchFilter"
|
||||||
export * from "./cookies"
|
export * from "./cookies"
|
||||||
export * from "./automation"
|
export * from "./automation"
|
||||||
export * from "./layout"
|
export * from "./layout"
|
||||||
|
export * from "./query"
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
import { QueryPreview, QuerySchema } from "../../documents"
|
||||||
|
|
||||||
|
export interface PreviewQueryRequest extends QueryPreview {}
|
||||||
|
|
||||||
|
export interface PreviewQueryResponse {
|
||||||
|
rows: any[]
|
||||||
|
nestedSchemaFields: { [key: string]: { [key: string]: string | QuerySchema } }
|
||||||
|
schema: { [key: string]: string | QuerySchema }
|
||||||
|
info: any
|
||||||
|
extra: any
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ExecuteQueryRequest {
|
||||||
|
parameters?: { [key: string]: string }
|
||||||
|
pagination?: any
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ExecuteQueryResponse {
|
||||||
|
data: any[]
|
||||||
|
}
|
|
@ -62,22 +62,6 @@ export interface PaginationValues {
|
||||||
limit: number | null
|
limit: number | null
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PreviewQueryRequest extends Omit<Query, "parameters"> {
|
|
||||||
parameters: {}
|
|
||||||
flags?: {
|
|
||||||
urlName?: boolean
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ExecuteQueryRequest {
|
|
||||||
parameters?: { [key: string]: string }
|
|
||||||
pagination?: any
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ExecuteQueryResponse {
|
|
||||||
data: Row[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum HttpMethod {
|
export enum HttpMethod {
|
||||||
GET = "GET",
|
GET = "GET",
|
||||||
POST = "POST",
|
POST = "POST",
|
||||||
|
|
Loading…
Reference in New Issue