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)
}
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<Row> {
let thisRow: Row = {}
// filter the row down to what is actually the row (not joined)
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
* 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,

View File

@ -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,

View File

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

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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({

View File

@ -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 &&

View File

@ -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`")