From 30c81e58eca5b3cc402daa2dd48a88c690c9088f Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 4 Apr 2024 18:16:23 +0100 Subject: [PATCH] Some typing and test fixes. --- .../src/api/controllers/row/utils/basic.ts | 4 ++-- .../src/api/controllers/row/utils/sqlUtils.ts | 4 ++-- .../src/api/controllers/row/utils/utils.ts | 6 +++--- .../server/src/api/routes/tests/viewV2.spec.ts | 2 -- packages/server/src/integrations/base/sql.ts | 13 +++++++++---- .../server/src/integrations/base/sqlTable.ts | 6 +++--- .../src/integrations/tests/sqlAlias.spec.ts | 4 ++-- packages/server/src/integrations/utils.ts | 18 ++++++++++++++++++ packages/server/src/sdk/app/rows/search/sqs.ts | 8 +++++++- 9 files changed, 46 insertions(+), 19 deletions(-) diff --git a/packages/server/src/api/controllers/row/utils/basic.ts b/packages/server/src/api/controllers/row/utils/basic.ts index 012e9ce080..ce7179035f 100644 --- a/packages/server/src/api/controllers/row/utils/basic.ts +++ b/packages/server/src/api/controllers/row/utils/basic.ts @@ -50,7 +50,7 @@ export function generateIdForRow( return generateRowIdField(idParts) } -export function basicProcessing({ +export async function basicProcessing({ row, table, isLinked, @@ -60,7 +60,7 @@ export function basicProcessing({ table: Table isLinked: boolean internal?: boolean -}): Row { +}): Promise { let thisRow: Row = {} // filter the row down to what is actually the row (not joined) let toIterate = Object.keys(table.schema) diff --git a/packages/server/src/api/controllers/row/utils/sqlUtils.ts b/packages/server/src/api/controllers/row/utils/sqlUtils.ts index 850c702640..3a4558a6cd 100644 --- a/packages/server/src/api/controllers/row/utils/sqlUtils.ts +++ b/packages/server/src/api/controllers/row/utils/sqlUtils.ts @@ -64,7 +64,7 @@ export function squashRelationshipColumns( * will be separate rows, with all of the data in each row. We have to decipher what comes * from where (which tables) and how to convert that into budibase columns. */ -export function updateRelationshipColumns( +export async function updateRelationshipColumns( table: Table, tables: TableMap, row: Row, @@ -89,7 +89,7 @@ export function updateRelationshipColumns( continue } - let linked = basicProcessing({ + let linked = await basicProcessing({ row, table: linkedTable, isLinked: true, diff --git a/packages/server/src/api/controllers/row/utils/utils.ts b/packages/server/src/api/controllers/row/utils/utils.ts index 0d1519cbf0..c33517c5f4 100644 --- a/packages/server/src/api/controllers/row/utils/utils.ts +++ b/packages/server/src/api/controllers/row/utils/utils.ts @@ -131,7 +131,7 @@ export async function sqlOutputProcessing( } // this is a relationship of some sort if (finalRows[rowId]) { - finalRows = updateRelationshipColumns( + finalRows = await updateRelationshipColumns( table, tables, row, @@ -142,7 +142,7 @@ export async function sqlOutputProcessing( continue } const thisRow = fixArrayTypes( - basicProcessing({ + await basicProcessing({ row, table, isLinked: false, @@ -155,7 +155,7 @@ export async function sqlOutputProcessing( } finalRows[thisRow._id] = thisRow // do this at end once its been added to the final rows - finalRows = updateRelationshipColumns( + finalRows = await updateRelationshipColumns( table, tables, row, diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index a0e0b16dee..eb35c171f1 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -652,7 +652,6 @@ describe.each([ ? {} : { hasNextPage: false, - bookmark: null, }), }) }) @@ -705,7 +704,6 @@ describe.each([ ? {} : { hasNextPage: false, - bookmark: null, }), }) }) diff --git a/packages/server/src/integrations/base/sql.ts b/packages/server/src/integrations/base/sql.ts index 081cdcff05..e99e34ab0f 100644 --- a/packages/server/src/integrations/base/sql.ts +++ b/packages/server/src/integrations/base/sql.ts @@ -1,7 +1,12 @@ import { Knex, knex } from "knex" import { db as dbCore } from "@budibase/backend-core" import { QueryOptions } from "../../definitions/datasource" -import { isIsoDateString, SqlClient, isValidFilter } from "../utils" +import { + isIsoDateString, + SqlClient, + isValidFilter, + getNativeSql, +} from "../utils" import SqlTableQueryBuilder from "./sqlTable" import { BBReferenceFieldMetadata, @@ -20,7 +25,7 @@ import { } from "@budibase/types" import environment from "../../environment" -type QueryFunction = (query: SqlQuery, operation: Operation) => any +type QueryFunction = (query: SqlQuery | SqlQuery[], operation: Operation) => any const envLimit = environment.SQL_MAX_ROWS ? parseInt(environment.SQL_MAX_ROWS) @@ -592,7 +597,7 @@ class SqlQueryBuilder extends SqlTableQueryBuilder { * which for the sake of mySQL stops adding the returning statement to inserts, updates and deletes. * @return the query ready to be passed to the driver. */ - _query(json: QueryJson, opts: QueryOptions = {}): SqlQuery { + _query(json: QueryJson, opts: QueryOptions = {}): SqlQuery | SqlQuery[] { const sqlClient = this.getSqlClient() const config: { client: string; useNullAsDefault?: boolean } = { client: sqlClient, @@ -630,7 +635,7 @@ class SqlQueryBuilder extends SqlTableQueryBuilder { if (opts?.disableBindings) { return { sql: query.toString() } } else { - return query.toSQL().toNative() as SqlQuery + return getNativeSql(query) } } diff --git a/packages/server/src/integrations/base/sqlTable.ts b/packages/server/src/integrations/base/sqlTable.ts index 8611d2a864..b3950259e9 100644 --- a/packages/server/src/integrations/base/sqlTable.ts +++ b/packages/server/src/integrations/base/sqlTable.ts @@ -10,7 +10,7 @@ import { FieldType, SqlQuery, } from "@budibase/types" -import { breakExternalTableId, SqlClient } from "../utils" +import { breakExternalTableId, getNativeSql, SqlClient } from "../utils" import SchemaBuilder = Knex.SchemaBuilder import CreateTableBuilder = Knex.CreateTableBuilder import { utils } from "@budibase/shared-core" @@ -200,7 +200,7 @@ class SqlTableQueryBuilder { return json.endpoint.operation } - _tableQuery(json: QueryJson): SqlQuery { + _tableQuery(json: QueryJson): SqlQuery | SqlQuery[] { let client = knex({ client: this.sqlClient }).schema let schemaName = json?.endpoint?.schema if (schemaName) { @@ -245,7 +245,7 @@ class SqlTableQueryBuilder { default: throw "Table operation is of unknown type" } - return query.toSQL().toNative() as SqlQuery + return getNativeSql(query) } } diff --git a/packages/server/src/integrations/tests/sqlAlias.spec.ts b/packages/server/src/integrations/tests/sqlAlias.spec.ts index 95e9fabf9f..bfca24ff7d 100644 --- a/packages/server/src/integrations/tests/sqlAlias.spec.ts +++ b/packages/server/src/integrations/tests/sqlAlias.spec.ts @@ -177,8 +177,8 @@ describe("Captures of real examples", () => { }) // now check returning - let returningQuery: SqlQuery = { sql: "", bindings: [] } - SQL.getReturningRow((input: SqlQuery) => { + let returningQuery: SqlQuery | SqlQuery[] = { sql: "", bindings: [] } + SQL.getReturningRow((input: SqlQuery | SqlQuery[]) => { returningQuery = input }, queryJson) expect(returningQuery).toEqual({ diff --git a/packages/server/src/integrations/utils.ts b/packages/server/src/integrations/utils.ts index 1eb7e44c00..d5f6d191e1 100644 --- a/packages/server/src/integrations/utils.ts +++ b/packages/server/src/integrations/utils.ts @@ -9,6 +9,7 @@ import { DocumentType, SEPARATOR } from "../db/utils" import { InvalidColumns, DEFAULT_BB_DATASOURCE_ID } from "../constants" import { helpers } from "@budibase/shared-core" import env from "../environment" +import { Knex } from "knex" const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}` const ROW_ID_REGEX = /^\[.*]$/g @@ -105,6 +106,23 @@ export function isInternalTableID(tableId: string) { return !isExternalTableID(tableId) } +export function getNativeSql( + query: Knex.SchemaBuilder | Knex.QueryBuilder +): SqlQuery | SqlQuery[] { + let sql = query.toSQL() + if (Array.isArray(sql)) { + return sql as SqlQuery[] + } + let native: Knex.SqlNative | undefined + if (sql.toNative) { + native = sql.toNative() + } + return { + sql: native?.sql || sql.sql, + bindings: native?.bindings || sql.bindings, + } as SqlQuery +} + export function isExternalTable(table: Table) { if ( table?.sourceId && diff --git a/packages/server/src/sdk/app/rows/search/sqs.ts b/packages/server/src/sdk/app/rows/search/sqs.ts index 7ad048819c..8aba94c886 100644 --- a/packages/server/src/sdk/app/rows/search/sqs.ts +++ b/packages/server/src/sdk/app/rows/search/sqs.ts @@ -154,11 +154,17 @@ export async function search( } } try { - let { sql } = builder._query(request, { + const query = builder._query(request, { disableReturning: true, disableBindings: true, }) + if (Array.isArray(query)) { + throw new Error("SQS cannot currently handle multiple queries") + } + + let sql = query.sql + // quick hack for docIds sql = sql.replace(/`doc1`.`rowId`/g, "`doc1.rowId`") sql = sql.replace(/`doc2`.`rowId`/g, "`doc2.rowId`")