Some typing updates to fix some build issues which were occurring.

This commit is contained in:
mike12345567 2024-03-06 18:07:46 +00:00
parent 2eb16a70db
commit 851a423615
11 changed files with 40 additions and 26 deletions

View File

@ -231,7 +231,7 @@ export class DatabaseImpl implements Database {
}) })
} }
async sql<T extends Document>(sql: string): Promise<T> { async sql<T extends Document>(sql: string): Promise<T[]> {
const dbName = this.name const dbName = this.name
const url = `/${dbName}/${SQLITE_DESIGN_DOC_ID}` const url = `/${dbName}/${SQLITE_DESIGN_DOC_ID}`
const response = await directCouchUrlCall({ const response = await directCouchUrlCall({
@ -243,7 +243,7 @@ export class DatabaseImpl implements Database {
if (response.status > 300) { if (response.status > 300) {
throw new Error(await response.text()) throw new Error(await response.text())
} }
return (await response.json()) as T return (await response.json()) as T[]
} }
async query<T extends Document>( async query<T extends Document>(

View File

@ -147,7 +147,7 @@ export class DDInstrumentedDatabase implements Database {
}) })
} }
sql<T extends Document>(sql: string): Promise<T> { sql<T extends Document>(sql: string): Promise<T[]> {
return tracer.trace("db.sql", span => { return tracer.trace("db.sql", span => {
span?.addTags({ db_name: this.name }) span?.addTags({ db_name: this.name })
return this.db.sql(sql) return this.db.sql(sql)

View File

@ -142,17 +142,17 @@ export async function sqlSearch(ctx: UserCtx) {
} }
} }
try { try {
let sql = builder._query(request, { let { sql } = builder._query(request, {
disableReturning: true, disableReturning: true,
disablePreparedStatements: true, disableBindings: true,
}) as string })
// 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`")
const db = context.getAppDB() const db = context.getAppDB()
const rows = await db.sql<Row[]>(sql) const rows = await db.sql<Row>(sql)
return { return {
rows: sqlOutputProcessing(rows, table!, allTablesMap, relationships, { rows: sqlOutputProcessing(rows, table!, allTablesMap, relationships, {

View File

@ -6,5 +6,5 @@
export interface QueryOptions { export interface QueryOptions {
disableReturning?: boolean disableReturning?: boolean
disablePreparedStatements?: boolean disableBindings?: boolean
} }

View File

@ -6,13 +6,15 @@ import SqlTableQueryBuilder from "./sqlTable"
import { import {
Operation, Operation,
QueryJson, QueryJson,
SqlQuery,
RelationshipsJson, RelationshipsJson,
SearchFilters, SearchFilters,
SortDirection, SortDirection,
SqlQueryBinding,
} from "@budibase/types" } from "@budibase/types"
import environment from "../../environment" import environment from "../../environment"
type QueryFunction = (query: Knex.SqlNative, operation: Operation) => any type QueryFunction = (query: 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)
@ -584,10 +586,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( _query(json: QueryJson, opts: QueryOptions = {}): SqlQuery {
json: QueryJson,
opts: QueryOptions = {}
): Knex.SqlNative | Knex.Sql | string {
const sqlClient = this.getSqlClient() const sqlClient = this.getSqlClient()
const config: { client: string; useNullAsDefault?: boolean } = { const config: { client: string; useNullAsDefault?: boolean } = {
client: sqlClient, client: sqlClient,
@ -622,10 +621,10 @@ class SqlQueryBuilder extends SqlTableQueryBuilder {
throw `Operation type is not supported by SQL query builder` throw `Operation type is not supported by SQL query builder`
} }
if (opts?.disablePreparedStatements) { if (opts?.disableBindings) {
return query.toString() return { sql: query.toString() }
} else { } else {
return query.toSQL().toNative() return query.toSQL().toNative() as SqlQuery
} }
} }
@ -708,7 +707,7 @@ class SqlQueryBuilder extends SqlTableQueryBuilder {
return results.length ? results : [{ [operation.toLowerCase()]: true }] return results.length ? results : [{ [operation.toLowerCase()]: true }]
} }
log(query: string, values?: any[]) { log(query: string, values?: SqlQueryBinding) {
if (!environment.SQL_LOGGING_ENABLE) { if (!environment.SQL_LOGGING_ENABLE) {
return return
} }

View File

@ -8,6 +8,7 @@ import {
RenameColumn, RenameColumn,
Table, Table,
FieldType, FieldType,
SqlQuery,
} from "@budibase/types" } from "@budibase/types"
import { breakExternalTableId, SqlClient } from "../utils" import { breakExternalTableId, SqlClient } from "../utils"
import SchemaBuilder = Knex.SchemaBuilder import SchemaBuilder = Knex.SchemaBuilder
@ -198,7 +199,7 @@ class SqlTableQueryBuilder {
return json.endpoint.operation return json.endpoint.operation
} }
_tableQuery(json: QueryJson): Knex.Sql | Knex.SqlNative { _tableQuery(json: QueryJson): 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) {
@ -243,7 +244,7 @@ class SqlTableQueryBuilder {
default: default:
throw "Table operation is of unknown type" throw "Table operation is of unknown type"
} }
return query.toSQL() return query.toSQL().toNative() as SqlQuery
} }
} }

View File

@ -13,6 +13,7 @@ import {
Schema, Schema,
TableSourceType, TableSourceType,
DatasourcePlusQueryResponse, DatasourcePlusQueryResponse,
SqlQueryBinding,
} from "@budibase/types" } from "@budibase/types"
import { import {
getSqlQuery, getSqlQuery,
@ -112,7 +113,7 @@ const defaultTypeCasting = function (field: any, next: any) {
return next() return next()
} }
export function bindingTypeCoerce(bindings: any[]) { export function bindingTypeCoerce(bindings: SqlQueryBinding) {
for (let i = 0; i < bindings.length; i++) { for (let i = 0; i < bindings.length; i++) {
const binding = bindings[i] const binding = bindings[i]
if (typeof binding !== "string") { if (typeof binding !== "string") {

View File

@ -1,10 +1,15 @@
import { Datasource, Operation, QueryJson, SourceName } from "@budibase/types" import {
Datasource,
Operation,
QueryJson,
SourceName,
SqlQuery,
} from "@budibase/types"
import { join } from "path" import { join } from "path"
import Sql from "../base/sql" import Sql from "../base/sql"
import { SqlClient } from "../utils" import { SqlClient } from "../utils"
import AliasTables from "../../api/controllers/row/alias" import AliasTables from "../../api/controllers/row/alias"
import { generator } from "@budibase/backend-core/tests" import { generator } from "@budibase/backend-core/tests"
import { Knex } from "knex"
function multiline(sql: string) { function multiline(sql: string) {
return sql.replace(/\n/g, "").replace(/ +/g, " ") return sql.replace(/\n/g, "").replace(/ +/g, " ")
@ -172,8 +177,8 @@ describe("Captures of real examples", () => {
}) })
// now check returning // now check returning
let returningQuery: Knex.SqlNative = { sql: "", bindings: [] } let returningQuery: SqlQuery = { sql: "", bindings: [] }
SQL.getReturningRow((input: Knex.SqlNative) => { SQL.getReturningRow((input: SqlQuery) => {
returningQuery = input returningQuery = input
}, queryJson) }, queryJson)
expect(returningQuery).toEqual({ expect(returningQuery).toEqual({

View File

@ -141,7 +141,7 @@ export interface Database {
opts?: DatabasePutOpts opts?: DatabasePutOpts
): Promise<Nano.DocumentInsertResponse> ): Promise<Nano.DocumentInsertResponse>
bulkDocs(documents: AnyDocument[]): Promise<Nano.DocumentBulkResponse[]> bulkDocs(documents: AnyDocument[]): Promise<Nano.DocumentBulkResponse[]>
sql<T extends Document>(sql: string): Promise<T> sql<T extends Document>(sql: string): Promise<T[]>
allDocs<T extends Document | RowValue>( allDocs<T extends Document | RowValue>(
params: DatabaseQueryOpts params: DatabaseQueryOpts
): Promise<AllDocsResponse<T>> ): Promise<AllDocsResponse<T>>

View File

@ -1,6 +1,7 @@
import { Operation, SortDirection } from "./datasources" import { Operation, SortDirection } from "./datasources"
import { Row, Table } from "../documents" import { Row, Table } from "../documents"
import { SortType } from "../api" import { SortType } from "../api"
import { Knex } from "knex"
export interface SearchFilters { export interface SearchFilters {
allOr?: boolean allOr?: boolean
@ -101,9 +102,11 @@ export interface QueryJson {
tableAliases?: Record<string, string> tableAliases?: Record<string, string>
} }
export type SqlQueryBinding = Knex.Value[]
export interface SqlQuery { export interface SqlQuery {
sql: string sql: string
bindings?: string[] bindings?: SqlQueryBinding
} }
export enum EmptyFilterOption { export enum EmptyFilterOption {

View File

@ -20783,6 +20783,11 @@ svelte-spa-router@^4.0.1:
dependencies: dependencies:
regexparam "2.0.2" regexparam "2.0.2"
svelte@3.49.0:
version "3.49.0"
resolved "https://registry.yarnpkg.com/svelte/-/svelte-3.49.0.tgz#5baee3c672306de1070c3b7888fc2204e36a4029"
integrity sha512-+lmjic1pApJWDfPCpUUTc1m8azDqYCG1JN9YEngrx/hUyIcFJo6VZhj0A1Ai0wqoHcEIuQy+e9tk+4uDgdtsFA==
svelte@^4.2.10: svelte@^4.2.10:
version "4.2.12" version "4.2.12"
resolved "https://registry.yarnpkg.com/svelte/-/svelte-4.2.12.tgz#13d98d2274d24d3ad216c8fdc801511171c70bb1" resolved "https://registry.yarnpkg.com/svelte/-/svelte-4.2.12.tgz#13d98d2274d24d3ad216c8fdc801511171c70bb1"