Some typing updates to fix some build issues which were occurring.
This commit is contained in:
parent
2eb16a70db
commit
851a423615
|
@ -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 url = `/${dbName}/${SQLITE_DESIGN_DOC_ID}`
|
||||
const response = await directCouchUrlCall({
|
||||
|
@ -243,7 +243,7 @@ export class DatabaseImpl implements Database {
|
|||
if (response.status > 300) {
|
||||
throw new Error(await response.text())
|
||||
}
|
||||
return (await response.json()) as T
|
||||
return (await response.json()) as T[]
|
||||
}
|
||||
|
||||
async query<T extends Document>(
|
||||
|
|
|
@ -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 => {
|
||||
span?.addTags({ db_name: this.name })
|
||||
return this.db.sql(sql)
|
||||
|
|
|
@ -142,17 +142,17 @@ export async function sqlSearch(ctx: UserCtx) {
|
|||
}
|
||||
}
|
||||
try {
|
||||
let sql = builder._query(request, {
|
||||
let { sql } = builder._query(request, {
|
||||
disableReturning: true,
|
||||
disablePreparedStatements: true,
|
||||
}) as string
|
||||
disableBindings: true,
|
||||
})
|
||||
|
||||
// quick hack for docIds
|
||||
sql = sql.replace(/`doc1`.`rowId`/g, "`doc1.rowId`")
|
||||
sql = sql.replace(/`doc2`.`rowId`/g, "`doc2.rowId`")
|
||||
|
||||
const db = context.getAppDB()
|
||||
const rows = await db.sql<Row[]>(sql)
|
||||
const rows = await db.sql<Row>(sql)
|
||||
|
||||
return {
|
||||
rows: sqlOutputProcessing(rows, table!, allTablesMap, relationships, {
|
||||
|
|
|
@ -6,5 +6,5 @@
|
|||
|
||||
export interface QueryOptions {
|
||||
disableReturning?: boolean
|
||||
disablePreparedStatements?: boolean
|
||||
disableBindings?: boolean
|
||||
}
|
||||
|
|
|
@ -6,13 +6,15 @@ import SqlTableQueryBuilder from "./sqlTable"
|
|||
import {
|
||||
Operation,
|
||||
QueryJson,
|
||||
SqlQuery,
|
||||
RelationshipsJson,
|
||||
SearchFilters,
|
||||
SortDirection,
|
||||
SqlQueryBinding,
|
||||
} from "@budibase/types"
|
||||
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
|
||||
? 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.
|
||||
* @return the query ready to be passed to the driver.
|
||||
*/
|
||||
_query(
|
||||
json: QueryJson,
|
||||
opts: QueryOptions = {}
|
||||
): Knex.SqlNative | Knex.Sql | string {
|
||||
_query(json: QueryJson, opts: QueryOptions = {}): SqlQuery {
|
||||
const sqlClient = this.getSqlClient()
|
||||
const config: { client: string; useNullAsDefault?: boolean } = {
|
||||
client: sqlClient,
|
||||
|
@ -622,10 +621,10 @@ class SqlQueryBuilder extends SqlTableQueryBuilder {
|
|||
throw `Operation type is not supported by SQL query builder`
|
||||
}
|
||||
|
||||
if (opts?.disablePreparedStatements) {
|
||||
return query.toString()
|
||||
if (opts?.disableBindings) {
|
||||
return { sql: query.toString() }
|
||||
} 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 }]
|
||||
}
|
||||
|
||||
log(query: string, values?: any[]) {
|
||||
log(query: string, values?: SqlQueryBinding) {
|
||||
if (!environment.SQL_LOGGING_ENABLE) {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import {
|
|||
RenameColumn,
|
||||
Table,
|
||||
FieldType,
|
||||
SqlQuery,
|
||||
} from "@budibase/types"
|
||||
import { breakExternalTableId, SqlClient } from "../utils"
|
||||
import SchemaBuilder = Knex.SchemaBuilder
|
||||
|
@ -198,7 +199,7 @@ class SqlTableQueryBuilder {
|
|||
return json.endpoint.operation
|
||||
}
|
||||
|
||||
_tableQuery(json: QueryJson): Knex.Sql | Knex.SqlNative {
|
||||
_tableQuery(json: QueryJson): SqlQuery {
|
||||
let client = knex({ client: this.sqlClient }).schema
|
||||
let schemaName = json?.endpoint?.schema
|
||||
if (schemaName) {
|
||||
|
@ -243,7 +244,7 @@ class SqlTableQueryBuilder {
|
|||
default:
|
||||
throw "Table operation is of unknown type"
|
||||
}
|
||||
return query.toSQL()
|
||||
return query.toSQL().toNative() as SqlQuery
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ import {
|
|||
Schema,
|
||||
TableSourceType,
|
||||
DatasourcePlusQueryResponse,
|
||||
SqlQueryBinding,
|
||||
} from "@budibase/types"
|
||||
import {
|
||||
getSqlQuery,
|
||||
|
@ -112,7 +113,7 @@ const defaultTypeCasting = function (field: any, next: any) {
|
|||
return next()
|
||||
}
|
||||
|
||||
export function bindingTypeCoerce(bindings: any[]) {
|
||||
export function bindingTypeCoerce(bindings: SqlQueryBinding) {
|
||||
for (let i = 0; i < bindings.length; i++) {
|
||||
const binding = bindings[i]
|
||||
if (typeof binding !== "string") {
|
||||
|
|
|
@ -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 Sql from "../base/sql"
|
||||
import { SqlClient } from "../utils"
|
||||
import AliasTables from "../../api/controllers/row/alias"
|
||||
import { generator } from "@budibase/backend-core/tests"
|
||||
import { Knex } from "knex"
|
||||
|
||||
function multiline(sql: string) {
|
||||
return sql.replace(/\n/g, "").replace(/ +/g, " ")
|
||||
|
@ -172,8 +177,8 @@ describe("Captures of real examples", () => {
|
|||
})
|
||||
|
||||
// now check returning
|
||||
let returningQuery: Knex.SqlNative = { sql: "", bindings: [] }
|
||||
SQL.getReturningRow((input: Knex.SqlNative) => {
|
||||
let returningQuery: SqlQuery = { sql: "", bindings: [] }
|
||||
SQL.getReturningRow((input: SqlQuery) => {
|
||||
returningQuery = input
|
||||
}, queryJson)
|
||||
expect(returningQuery).toEqual({
|
||||
|
|
|
@ -141,7 +141,7 @@ export interface Database {
|
|||
opts?: DatabasePutOpts
|
||||
): Promise<Nano.DocumentInsertResponse>
|
||||
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>(
|
||||
params: DatabaseQueryOpts
|
||||
): Promise<AllDocsResponse<T>>
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { Operation, SortDirection } from "./datasources"
|
||||
import { Row, Table } from "../documents"
|
||||
import { SortType } from "../api"
|
||||
import { Knex } from "knex"
|
||||
|
||||
export interface SearchFilters {
|
||||
allOr?: boolean
|
||||
|
@ -101,9 +102,11 @@ export interface QueryJson {
|
|||
tableAliases?: Record<string, string>
|
||||
}
|
||||
|
||||
export type SqlQueryBinding = Knex.Value[]
|
||||
|
||||
export interface SqlQuery {
|
||||
sql: string
|
||||
bindings?: string[]
|
||||
bindings?: SqlQueryBinding
|
||||
}
|
||||
|
||||
export enum EmptyFilterOption {
|
||||
|
|
|
@ -20783,6 +20783,11 @@ svelte-spa-router@^4.0.1:
|
|||
dependencies:
|
||||
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:
|
||||
version "4.2.12"
|
||||
resolved "https://registry.yarnpkg.com/svelte/-/svelte-4.2.12.tgz#13d98d2274d24d3ad216c8fdc801511171c70bb1"
|
||||
|
|
Loading…
Reference in New Issue