From 68e8630d85bd54cdd90f8edae63f9c66f423654f Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 18 Oct 2023 10:57:04 +0100 Subject: [PATCH] Improving the typing around the ExternalRequest object, which has implications throughout the row API and SDK, cleaning up where possible based on it. --- .../api/controllers/row/ExternalRequest.ts | 26 +++++++------------ .../src/api/controllers/row/external.ts | 14 +++++----- packages/server/src/sdk/app/rows/external.ts | 4 +-- packages/server/src/sdk/app/rows/search.ts | 6 ++--- .../src/sdk/app/rows/search/external.ts | 24 ++++++++++------- .../src/sdk/app/rows/search/internal.ts | 17 ++++++------ 6 files changed, 44 insertions(+), 47 deletions(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index 71532c37d5..c3c5468840 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -280,17 +280,8 @@ function isEditableColumn(column: FieldSchema) { return !(isExternalAutoColumn || isFormula) } -export type ExternalRequestReturnType = T extends Operation.READ - ? - | Row[] - | { - row: Row - table: Table - } - : { - row: Row - table: Table - } +export type ExternalRequestReturnType = + T extends Operation.READ ? Row[] : { row: Row; table: Table } export class ExternalRequest { private readonly operation: T @@ -857,11 +848,12 @@ export class ExternalRequest { } const output = this.outputProcessing(response, table, relationships) // if reading it'll just be an array of rows, return whole thing - const result = ( - operation === Operation.READ && Array.isArray(response) - ? output - : { row: output[0], table } - ) as ExternalRequestReturnType - return result + if (operation === Operation.READ) { + return ( + Array.isArray(output) ? output : [output] + ) as ExternalRequestReturnType + } else { + return { row: output[0], table } as ExternalRequestReturnType + } } } diff --git a/packages/server/src/api/controllers/row/external.ts b/packages/server/src/api/controllers/row/external.ts index ddc63e5790..0515b6b97e 100644 --- a/packages/server/src/api/controllers/row/external.ts +++ b/packages/server/src/api/controllers/row/external.ts @@ -44,7 +44,7 @@ export async function handleRequest( return [] as any } - return new ExternalRequest(operation, tableId, opts?.datasource).run( + return new ExternalRequest(operation, tableId, opts?.datasource).run( opts || {} ) } @@ -148,17 +148,17 @@ export async function find(ctx: UserCtx): Promise { export async function destroy(ctx: UserCtx) { const tableId = utils.getTableId(ctx) const _id = ctx.request.body._id - const { row } = (await handleRequest(Operation.DELETE, tableId, { + const { row } = await handleRequest(Operation.DELETE, tableId, { id: breakRowIdField(_id), includeSqlRelationships: IncludeRelationship.EXCLUDE, - })) as { row: Row } + }) return { response: { ok: true, id: _id }, row } } export async function bulkDestroy(ctx: UserCtx) { const { rows } = ctx.request.body const tableId = utils.getTableId(ctx) - let promises: Promise[] = [] + let promises: Promise<{ row: Row; table: Table }>[] = [] for (let row of rows) { promises.push( handleRequest(Operation.DELETE, tableId, { @@ -167,7 +167,7 @@ export async function bulkDestroy(ctx: UserCtx) { }) ) } - const responses = (await Promise.all(promises)) as { row: Row }[] + const responses = await Promise.all(promises) return { response: { ok: true }, rows: responses.map(resp => resp.row) } } @@ -183,11 +183,11 @@ export async function fetchEnrichedRow(ctx: UserCtx) { ctx.throw(400, "Datasource has not been configured for plus API.") } const tables = datasource.entities - const response = (await handleRequest(Operation.READ, tableId, { + const response = await handleRequest(Operation.READ, tableId, { id, datasource, includeSqlRelationships: IncludeRelationship.INCLUDE, - })) as Row[] + }) const table: Table = tables[tableName] const row = response[0] // this seems like a lot of work, but basically we need to dig deeper for the enrich diff --git a/packages/server/src/sdk/app/rows/external.ts b/packages/server/src/sdk/app/rows/external.ts index 568bd07e9d..8bcf89a3f5 100644 --- a/packages/server/src/sdk/app/rows/external.ts +++ b/packages/server/src/sdk/app/rows/external.ts @@ -7,11 +7,11 @@ export async function getRow( rowId: string, opts?: { relationships?: boolean } ) { - const response = (await handleRequest(Operation.READ, tableId, { + const response = await handleRequest(Operation.READ, tableId, { id: breakRowIdField(rowId), includeSqlRelationships: opts?.relationships ? IncludeRelationship.INCLUDE : IncludeRelationship.EXCLUDE, - })) as Row[] + }) return response ? response[0] : response } diff --git a/packages/server/src/sdk/app/rows/search.ts b/packages/server/src/sdk/app/rows/search.ts index f75bd07437..ced35db9be 100644 --- a/packages/server/src/sdk/app/rows/search.ts +++ b/packages/server/src/sdk/app/rows/search.ts @@ -1,4 +1,4 @@ -import { SearchFilters, SearchParams } from "@budibase/types" +import { SearchFilters, SearchParams, Row } from "@budibase/types" import { isExternalTable } from "../../../integrations/utils" import * as internal from "./search/internal" import * as external from "./search/external" @@ -45,7 +45,7 @@ export async function exportRows( return pickApi(options.tableId).exportRows(options) } -export async function fetch(tableId: string) { +export async function fetch(tableId: string): Promise { return pickApi(tableId).fetch(tableId) } @@ -53,6 +53,6 @@ export async function fetchView( tableId: string, viewName: string, params: ViewParams -) { +): Promise { return pickApi(tableId).fetchView(viewName, params) } diff --git a/packages/server/src/sdk/app/rows/search/external.ts b/packages/server/src/sdk/app/rows/search/external.ts index 8dd141f8ef..c41efad171 100644 --- a/packages/server/src/sdk/app/rows/search/external.ts +++ b/packages/server/src/sdk/app/rows/search/external.ts @@ -55,15 +55,15 @@ export async function search(options: SearchParams) { try { const table = await sdk.tables.getTable(tableId) options = searchInputMapping(table, options) - let rows = (await handleRequest(Operation.READ, tableId, { + let rows = await handleRequest(Operation.READ, tableId, { filters: query, sort, paginate: paginateObj as PaginationJson, includeSqlRelationships: IncludeRelationship.INCLUDE, - })) as Row[] + }) let hasNextPage = false if (paginate && rows.length === limit) { - const nextRows = (await handleRequest(Operation.READ, tableId, { + const nextRows = await handleRequest(Operation.READ, tableId, { filters: query, sort, paginate: { @@ -71,7 +71,7 @@ export async function search(options: SearchParams) { page: bookmark! * limit + 1, }, includeSqlRelationships: IncludeRelationship.INCLUDE, - })) as Row[] + }) hasNextPage = nextRows.length > 0 } @@ -172,12 +172,18 @@ export async function exportRows( } } -export async function fetch(tableId: string) { - const response = await handleRequest(Operation.READ, tableId, { - includeSqlRelationships: IncludeRelationship.INCLUDE, - }) +export async function fetch(tableId: string): Promise { + const response = await handleRequest( + Operation.READ, + tableId, + { + includeSqlRelationships: IncludeRelationship.INCLUDE, + } + ) const table = await sdk.tables.getTable(tableId) - return await outputProcessing(table, response, { preserveLinks: true }) + return await outputProcessing(table, response, { + preserveLinks: true, + }) } export async function fetchView(viewName: string) { diff --git a/packages/server/src/sdk/app/rows/search/internal.ts b/packages/server/src/sdk/app/rows/search/internal.ts index d78c0213b3..779ff5f777 100644 --- a/packages/server/src/sdk/app/rows/search/internal.ts +++ b/packages/server/src/sdk/app/rows/search/internal.ts @@ -6,26 +6,26 @@ import { import env from "../../../../environment" import { fullSearch, paginatedSearch } from "./internalSearch" import { - InternalTables, - getRowParams, DocumentType, + getRowParams, + InternalTables, } from "../../../../db/utils" import { getGlobalUsersFromMetadata } from "../../../../utilities/global" import { outputProcessing } from "../../../../utilities/rowProcessor" -import { Database, Row, Table, SearchParams } from "@budibase/types" +import { Database, Row, SearchParams, Table } from "@budibase/types" import { cleanExportRows } from "../utils" import { - Format, csv, + Format, json, jsonWithSchema, } from "../../../../api/controllers/view/exporters" import * as inMemoryViews from "../../../../db/inMemoryView" import { - migrateToInMemoryView, - migrateToDesignView, getFromDesignDoc, getFromMemoryDoc, + migrateToDesignView, + migrateToInMemoryView, } from "../../../../api/controllers/view/utils" import sdk from "../../../../sdk" import { ExportRowsParams, ExportRowsResult } from "../search" @@ -139,13 +139,12 @@ export async function exportRows( } } -export async function fetch(tableId: string) { +export async function fetch(tableId: string): Promise { const db = context.getAppDB() const table = await sdk.tables.getTable(tableId) const rows = await getRawTableData(db, tableId) - const result = await outputProcessing(table, rows) - return result + return await outputProcessing(table, rows) } async function getRawTableData(db: Database, tableId: string) {