From 23d9808cb61cf045e9347863df1ebaf4bfb7bd71 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 3 Dec 2024 17:29:41 +0000 Subject: [PATCH] Query API typing. --- .../server/src/api/controllers/query/index.ts | 79 ++++++++++++------- packages/server/src/api/routes/query.ts | 2 +- .../src/automations/steps/executeQuery.ts | 3 +- .../server/src/tests/utilities/api/query.ts | 6 +- packages/types/src/api/web/app/index.ts | 1 + packages/types/src/api/web/app/query.ts | 47 +++++++++++ packages/types/src/api/web/index.ts | 1 - packages/types/src/api/web/query.ts | 20 ----- 8 files changed, 104 insertions(+), 55 deletions(-) create mode 100644 packages/types/src/api/web/app/query.ts delete mode 100644 packages/types/src/api/web/query.ts diff --git a/packages/server/src/api/controllers/query/index.ts b/packages/server/src/api/controllers/query/index.ts index 15c60bcf47..d73bb8c024 100644 --- a/packages/server/src/api/controllers/query/index.ts +++ b/packages/server/src/api/controllers/query/index.ts @@ -4,26 +4,38 @@ import { save as saveDatasource } from "../datasource" import { RestImporter } from "./import" import { invalidateCachedVariable } from "../../../threads/utils" import env from "../../../environment" -import { events, context, utils, constants } from "@budibase/backend-core" +import { constants, context, events, utils } from "@budibase/backend-core" import sdk from "../../../sdk" import { QueryEvent, QueryEventParameters } from "../../../threads/definitions" import { ConfigType, - Query, - UserCtx, - SessionCookie, - JsonFieldSubType, - QueryResponse, - QuerySchema, - FieldType, + CreateDatasourceRequest, + Datasource, ExecuteQueryRequest, - ExecuteQueryResponse, + ExecuteV2QueryResponse, + ExecuteV1QueryResponse, + FetchQueriesResponse, + FieldType, + FindQueryResponse, + ImportRestQueryRequest, + ImportRestQueryResponse, + JsonFieldSubType, PreviewQueryRequest, PreviewQueryResponse, + Query, + QueryResponse, + QuerySchema, + SaveQueryRequest, + SaveQueryResponse, + SessionCookie, + SourceName, + UserCtx, + DeleteQueryResponse, } from "@budibase/types" -import { ValidQueryNameRegex, utils as JsonUtils } from "@budibase/shared-core" +import { utils as JsonUtils, ValidQueryNameRegex } from "@budibase/shared-core" import { findHBSBlocks } from "@budibase/string-templates" import { ObjectId } from "mongodb" +import { merge } from "lodash" const Runner = new Thread(ThreadType.QUERY, { timeoutMs: env.QUERY_THREAD_TIMEOUT, @@ -43,11 +55,13 @@ function validateQueryInputs(parameters: QueryEventParameters) { } } -export async function fetch(ctx: UserCtx) { +export async function fetch(ctx: UserCtx) { ctx.body = await sdk.queries.fetch() } -const _import = async (ctx: UserCtx) => { +const _import = async ( + ctx: UserCtx +) => { const body = ctx.request.body const data = body.data @@ -58,9 +72,9 @@ const _import = async (ctx: UserCtx) => { if (!body.datasourceId) { // construct new datasource const info: any = await importer.getInfo() - let datasource = { + let datasource: Datasource = { type: "datasource", - source: "REST", + source: SourceName.REST, config: { url: info.url, defaultHeaders: [], @@ -69,8 +83,14 @@ const _import = async (ctx: UserCtx) => { name: info.name, } // save the datasource - const datasourceCtx = { ...ctx } - datasourceCtx.request.body.datasource = datasource + const datasourceCtx: UserCtx = merge(ctx, { + request: { + body: { + datasource, + tablesFilter: [], + }, + }, + }) await saveDatasource(datasourceCtx) datasourceId = datasourceCtx.body.datasource._id } else { @@ -88,7 +108,7 @@ const _import = async (ctx: UserCtx) => { } export { _import as import } -export async function save(ctx: UserCtx) { +export async function save(ctx: UserCtx) { const db = context.getAppDB() const query: Query = ctx.request.body @@ -119,10 +139,9 @@ export async function save(ctx: UserCtx) { query._rev = response.rev ctx.body = query - ctx.message = `Query ${query.name} saved successfully.` } -export async function find(ctx: UserCtx) { +export async function find(ctx: UserCtx) { const queryId = ctx.params.queryId ctx.body = await sdk.queries.find(queryId) } @@ -335,7 +354,7 @@ export async function preview( async function execute( ctx: UserCtx< ExecuteQueryRequest, - ExecuteQueryResponse | Record[] + ExecuteV2QueryResponse | ExecuteV1QueryResponse >, opts: any = { rowsOnly: false, isAutomation: false } ) { @@ -390,19 +409,21 @@ async function execute( } export async function executeV1( - ctx: UserCtx[]> + ctx: UserCtx ) { return execute(ctx, { rowsOnly: true, isAutomation: false }) } export async function executeV2( - ctx: UserCtx< - ExecuteQueryRequest, - ExecuteQueryResponse | Record[] - >, - { isAutomation }: { isAutomation?: boolean } = {} + ctx: UserCtx ) { - return execute(ctx, { rowsOnly: false, isAutomation }) + return execute(ctx, { rowsOnly: false }) +} + +export async function executeV2AsAutomation( + ctx: UserCtx +) { + return execute(ctx, { rowsOnly: false, isAutomation: true }) } const removeDynamicVariables = async (queryId: string) => { @@ -426,14 +447,14 @@ const removeDynamicVariables = async (queryId: string) => { } } -export async function destroy(ctx: UserCtx) { +export async function destroy(ctx: UserCtx) { const db = context.getAppDB() const queryId = ctx.params.queryId as string await removeDynamicVariables(queryId) const query = await db.get(queryId) const datasource = await sdk.datasources.get(query.datasourceId) await db.remove(ctx.params.queryId, ctx.params.revId) - ctx.message = `Query deleted.` + ctx.body = { message: `Query deleted.` } ctx.status = 200 await events.query.deleted(datasource, query) } diff --git a/packages/server/src/api/routes/query.ts b/packages/server/src/api/routes/query.ts index eb857d0637..89d3651227 100644 --- a/packages/server/src/api/routes/query.ts +++ b/packages/server/src/api/routes/query.ts @@ -56,7 +56,7 @@ router "/api/v2/queries/:queryId", paramResource("queryId"), authorized(PermissionType.QUERY, PermissionLevel.WRITE), - queryController.executeV2 as any + queryController.executeV2 ) export default router diff --git a/packages/server/src/automations/steps/executeQuery.ts b/packages/server/src/automations/steps/executeQuery.ts index a42182df37..095c91a44d 100644 --- a/packages/server/src/automations/steps/executeQuery.ts +++ b/packages/server/src/automations/steps/executeQuery.ts @@ -12,6 +12,7 @@ import { ExecuteQueryStepInputs, ExecuteQueryStepOutputs, } from "@budibase/types" +import { executeV2AsAutomation } from "../../api/controllers/query" export const definition: AutomationStepDefinition = { name: "External Data Connector", @@ -94,7 +95,7 @@ export async function run({ }) try { - await queryController.executeV2(ctx, { isAutomation: true }) + await queryController.executeV2AsAutomation(ctx) const { data, ...rest } = ctx.body return { diff --git a/packages/server/src/tests/utilities/api/query.ts b/packages/server/src/tests/utilities/api/query.ts index 2d5f7970cd..fc81fc4561 100644 --- a/packages/server/src/tests/utilities/api/query.ts +++ b/packages/server/src/tests/utilities/api/query.ts @@ -1,7 +1,7 @@ import { Query, ExecuteQueryRequest, - ExecuteQueryResponse, + ExecuteV2QueryResponse, PreviewQueryRequest, PreviewQueryResponse, } from "@budibase/types" @@ -17,8 +17,8 @@ export class QueryAPI extends TestAPI { queryId: string, body?: ExecuteQueryRequest, expectations?: Expectations - ): Promise => { - return await this._post( + ): Promise => { + return await this._post( `/api/v2/queries/${queryId}`, { body, diff --git a/packages/types/src/api/web/app/index.ts b/packages/types/src/api/web/app/index.ts index 40c8ebf9ca..82770543df 100644 --- a/packages/types/src/api/web/app/index.ts +++ b/packages/types/src/api/web/app/index.ts @@ -12,3 +12,4 @@ export * from "./automation" export * from "./component" export * from "./integration" export * from "./metadata" +export * from "./query" diff --git a/packages/types/src/api/web/app/query.ts b/packages/types/src/api/web/app/query.ts new file mode 100644 index 0000000000..302f0d03e5 --- /dev/null +++ b/packages/types/src/api/web/app/query.ts @@ -0,0 +1,47 @@ +import { + Datasource, + Query, + QueryPreview, + QuerySchema, +} from "../../../documents" + +export type FetchQueriesResponse = Query[] + +export interface SaveQueryRequest extends Query {} +export interface SaveQueryResponse extends Query {} + +export interface ImportRestQueryRequest { + datasourceId: string + data: string + datasource: Datasource +} +export interface ImportRestQueryResponse { + errorQueries: Query[] + queries: Query[] + datasourceId: string +} + +export interface FindQueryResponse extends Query {} + +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?: Record + pagination?: any +} +export type ExecuteV1QueryResponse = Record[] +export interface ExecuteV2QueryResponse { + data: Record[] +} + +export interface DeleteQueryResponse { + message: string +} diff --git a/packages/types/src/api/web/index.ts b/packages/types/src/api/web/index.ts index 4021eafee8..dd2ee3beae 100644 --- a/packages/types/src/api/web/index.ts +++ b/packages/types/src/api/web/index.ts @@ -13,7 +13,6 @@ export * from "./searchFilter" export * from "./cookies" export * from "./automation" export * from "./layout" -export * from "./query" export * from "./role" export * from "./plugins" export * from "./apikeys" diff --git a/packages/types/src/api/web/query.ts b/packages/types/src/api/web/query.ts deleted file mode 100644 index 66f0248647..0000000000 --- a/packages/types/src/api/web/query.ts +++ /dev/null @@ -1,20 +0,0 @@ -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?: Record - pagination?: any -} - -export interface ExecuteQueryResponse { - data: Record[] -}