Some typing and test fixes.

This commit is contained in:
mike12345567 2024-04-04 18:16:23 +01:00
parent f937e1ac47
commit 30c81e58ec
9 changed files with 46 additions and 19 deletions

View File

@ -50,7 +50,7 @@ export function generateIdForRow(
return generateRowIdField(idParts) return generateRowIdField(idParts)
} }
export function basicProcessing({ export async function basicProcessing({
row, row,
table, table,
isLinked, isLinked,
@ -60,7 +60,7 @@ export function basicProcessing({
table: Table table: Table
isLinked: boolean isLinked: boolean
internal?: boolean internal?: boolean
}): Row { }): Promise<Row> {
let thisRow: Row = {} let thisRow: Row = {}
// filter the row down to what is actually the row (not joined) // filter the row down to what is actually the row (not joined)
let toIterate = Object.keys(table.schema) let toIterate = Object.keys(table.schema)

View File

@ -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 * 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. * from where (which tables) and how to convert that into budibase columns.
*/ */
export function updateRelationshipColumns( export async function updateRelationshipColumns(
table: Table, table: Table,
tables: TableMap, tables: TableMap,
row: Row, row: Row,
@ -89,7 +89,7 @@ export function updateRelationshipColumns(
continue continue
} }
let linked = basicProcessing({ let linked = await basicProcessing({
row, row,
table: linkedTable, table: linkedTable,
isLinked: true, isLinked: true,

View File

@ -131,7 +131,7 @@ export async function sqlOutputProcessing(
} }
// this is a relationship of some sort // this is a relationship of some sort
if (finalRows[rowId]) { if (finalRows[rowId]) {
finalRows = updateRelationshipColumns( finalRows = await updateRelationshipColumns(
table, table,
tables, tables,
row, row,
@ -142,7 +142,7 @@ export async function sqlOutputProcessing(
continue continue
} }
const thisRow = fixArrayTypes( const thisRow = fixArrayTypes(
basicProcessing({ await basicProcessing({
row, row,
table, table,
isLinked: false, isLinked: false,
@ -155,7 +155,7 @@ export async function sqlOutputProcessing(
} }
finalRows[thisRow._id] = thisRow finalRows[thisRow._id] = thisRow
// do this at end once its been added to the final rows // do this at end once its been added to the final rows
finalRows = updateRelationshipColumns( finalRows = await updateRelationshipColumns(
table, table,
tables, tables,
row, row,

View File

@ -652,7 +652,6 @@ describe.each([
? {} ? {}
: { : {
hasNextPage: false, hasNextPage: false,
bookmark: null,
}), }),
}) })
}) })
@ -705,7 +704,6 @@ describe.each([
? {} ? {}
: { : {
hasNextPage: false, hasNextPage: false,
bookmark: null,
}), }),
}) })
}) })

View File

@ -1,7 +1,12 @@
import { Knex, knex } from "knex" import { Knex, knex } from "knex"
import { db as dbCore } from "@budibase/backend-core" import { db as dbCore } from "@budibase/backend-core"
import { QueryOptions } from "../../definitions/datasource" import { QueryOptions } from "../../definitions/datasource"
import { isIsoDateString, SqlClient, isValidFilter } from "../utils" import {
isIsoDateString,
SqlClient,
isValidFilter,
getNativeSql,
} from "../utils"
import SqlTableQueryBuilder from "./sqlTable" import SqlTableQueryBuilder from "./sqlTable"
import { import {
BBReferenceFieldMetadata, BBReferenceFieldMetadata,
@ -20,7 +25,7 @@ import {
} from "@budibase/types" } from "@budibase/types"
import environment from "../../environment" 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 const envLimit = environment.SQL_MAX_ROWS
? parseInt(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. * 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. * @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 sqlClient = this.getSqlClient()
const config: { client: string; useNullAsDefault?: boolean } = { const config: { client: string; useNullAsDefault?: boolean } = {
client: sqlClient, client: sqlClient,
@ -630,7 +635,7 @@ class SqlQueryBuilder extends SqlTableQueryBuilder {
if (opts?.disableBindings) { if (opts?.disableBindings) {
return { sql: query.toString() } return { sql: query.toString() }
} else { } else {
return query.toSQL().toNative() as SqlQuery return getNativeSql(query)
} }
} }

View File

@ -10,7 +10,7 @@ import {
FieldType, FieldType,
SqlQuery, SqlQuery,
} from "@budibase/types" } from "@budibase/types"
import { breakExternalTableId, SqlClient } from "../utils" import { breakExternalTableId, getNativeSql, SqlClient } from "../utils"
import SchemaBuilder = Knex.SchemaBuilder import SchemaBuilder = Knex.SchemaBuilder
import CreateTableBuilder = Knex.CreateTableBuilder import CreateTableBuilder = Knex.CreateTableBuilder
import { utils } from "@budibase/shared-core" import { utils } from "@budibase/shared-core"
@ -200,7 +200,7 @@ class SqlTableQueryBuilder {
return json.endpoint.operation return json.endpoint.operation
} }
_tableQuery(json: QueryJson): SqlQuery { _tableQuery(json: QueryJson): SqlQuery | SqlQuery[] {
let client = knex({ client: this.sqlClient }).schema let client = knex({ client: this.sqlClient }).schema
let schemaName = json?.endpoint?.schema let schemaName = json?.endpoint?.schema
if (schemaName) { if (schemaName) {
@ -245,7 +245,7 @@ class SqlTableQueryBuilder {
default: default:
throw "Table operation is of unknown type" throw "Table operation is of unknown type"
} }
return query.toSQL().toNative() as SqlQuery return getNativeSql(query)
} }
} }

View File

@ -177,8 +177,8 @@ describe("Captures of real examples", () => {
}) })
// now check returning // now check returning
let returningQuery: SqlQuery = { sql: "", bindings: [] } let returningQuery: SqlQuery | SqlQuery[] = { sql: "", bindings: [] }
SQL.getReturningRow((input: SqlQuery) => { SQL.getReturningRow((input: SqlQuery | SqlQuery[]) => {
returningQuery = input returningQuery = input
}, queryJson) }, queryJson)
expect(returningQuery).toEqual({ expect(returningQuery).toEqual({

View File

@ -9,6 +9,7 @@ import { DocumentType, SEPARATOR } from "../db/utils"
import { InvalidColumns, DEFAULT_BB_DATASOURCE_ID } from "../constants" import { InvalidColumns, DEFAULT_BB_DATASOURCE_ID } from "../constants"
import { helpers } from "@budibase/shared-core" import { helpers } from "@budibase/shared-core"
import env from "../environment" import env from "../environment"
import { Knex } from "knex"
const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}` const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}`
const ROW_ID_REGEX = /^\[.*]$/g const ROW_ID_REGEX = /^\[.*]$/g
@ -105,6 +106,23 @@ export function isInternalTableID(tableId: string) {
return !isExternalTableID(tableId) 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) { export function isExternalTable(table: Table) {
if ( if (
table?.sourceId && table?.sourceId &&

View File

@ -154,11 +154,17 @@ export async function search(
} }
} }
try { try {
let { sql } = builder._query(request, { const query = builder._query(request, {
disableReturning: true, disableReturning: true,
disableBindings: true, disableBindings: true,
}) })
if (Array.isArray(query)) {
throw new Error("SQS cannot currently handle multiple queries")
}
let sql = query.sql
// quick hack for docIds // quick hack for docIds
sql = sql.replace(/`doc1`.`rowId`/g, "`doc1.rowId`") sql = sql.replace(/`doc1`.`rowId`/g, "`doc1.rowId`")
sql = sql.replace(/`doc2`.`rowId`/g, "`doc2.rowId`") sql = sql.replace(/`doc2`.`rowId`/g, "`doc2.rowId`")