Correctly handling aliasing for sorting/json field types with SQS.
This commit is contained in:
parent
5de6601f51
commit
b13b7df678
|
@ -22,6 +22,8 @@ import {
|
||||||
SortDirection,
|
SortDirection,
|
||||||
SqlQueryBinding,
|
SqlQueryBinding,
|
||||||
Table,
|
Table,
|
||||||
|
TableSourceType,
|
||||||
|
INTERNAL_TABLE_SOURCE_ID,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import environment from "../../environment"
|
import environment from "../../environment"
|
||||||
|
|
||||||
|
@ -135,6 +137,21 @@ function generateSelectStatement(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getTableName(table?: Table): string {
|
||||||
|
if (!table) {
|
||||||
|
throw new Error("No table name available.")
|
||||||
|
}
|
||||||
|
// SQS uses the table ID rather than the table name
|
||||||
|
if (
|
||||||
|
table.sourceType === TableSourceType.INTERNAL ||
|
||||||
|
table.sourceId === INTERNAL_TABLE_SOURCE_ID
|
||||||
|
) {
|
||||||
|
return table._id!
|
||||||
|
} else {
|
||||||
|
return table.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class InternalBuilder {
|
class InternalBuilder {
|
||||||
private readonly client: string
|
private readonly client: string
|
||||||
|
|
||||||
|
@ -149,7 +166,7 @@ class InternalBuilder {
|
||||||
tableName: string,
|
tableName: string,
|
||||||
opts: { aliases?: Record<string, string>; relationship?: boolean }
|
opts: { aliases?: Record<string, string>; relationship?: boolean }
|
||||||
): Knex.QueryBuilder {
|
): Knex.QueryBuilder {
|
||||||
function getTableName(name: string) {
|
function getTableAlias(name: string) {
|
||||||
const alias = opts.aliases?.[name]
|
const alias = opts.aliases?.[name]
|
||||||
return alias || name
|
return alias || name
|
||||||
}
|
}
|
||||||
|
@ -161,11 +178,11 @@ class InternalBuilder {
|
||||||
const updatedKey = dbCore.removeKeyNumbering(key)
|
const updatedKey = dbCore.removeKeyNumbering(key)
|
||||||
const isRelationshipField = updatedKey.includes(".")
|
const isRelationshipField = updatedKey.includes(".")
|
||||||
if (!opts.relationship && !isRelationshipField) {
|
if (!opts.relationship && !isRelationshipField) {
|
||||||
fn(`${getTableName(tableName)}.${updatedKey}`, value)
|
fn(`${getTableAlias(tableName)}.${updatedKey}`, value)
|
||||||
}
|
}
|
||||||
if (opts.relationship && isRelationshipField) {
|
if (opts.relationship && isRelationshipField) {
|
||||||
const [filterTableName, property] = updatedKey.split(".")
|
const [filterTableName, property] = updatedKey.split(".")
|
||||||
fn(`${getTableName(filterTableName)}.${property}`, value)
|
fn(`${getTableAlias(filterTableName)}.${property}`, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -346,9 +363,10 @@ class InternalBuilder {
|
||||||
addSorting(query: Knex.QueryBuilder, json: QueryJson): Knex.QueryBuilder {
|
addSorting(query: Knex.QueryBuilder, json: QueryJson): Knex.QueryBuilder {
|
||||||
let { sort, paginate } = json
|
let { sort, paginate } = json
|
||||||
const table = json.meta?.table
|
const table = json.meta?.table
|
||||||
|
const tableName = getTableName(table)
|
||||||
const aliases = json.tableAliases
|
const aliases = json.tableAliases
|
||||||
const aliased =
|
const aliased =
|
||||||
table?.name && aliases?.[table.name] ? aliases[table.name] : table?.name
|
table?.name && aliases?.[tableName] ? aliases[tableName] : table?.name
|
||||||
if (sort && Object.keys(sort || {}).length > 0) {
|
if (sort && Object.keys(sort || {}).length > 0) {
|
||||||
for (let [key, value] of Object.entries(sort)) {
|
for (let [key, value] of Object.entries(sort)) {
|
||||||
const direction =
|
const direction =
|
||||||
|
@ -729,12 +747,13 @@ class SqlQueryBuilder extends SqlTableQueryBuilder {
|
||||||
results: Record<string, any>[],
|
results: Record<string, any>[],
|
||||||
aliases?: Record<string, string>
|
aliases?: Record<string, string>
|
||||||
): Record<string, any>[] {
|
): Record<string, any>[] {
|
||||||
|
const tableName = getTableName(table)
|
||||||
for (const [name, field] of Object.entries(table.schema)) {
|
for (const [name, field] of Object.entries(table.schema)) {
|
||||||
if (!this._isJsonColumn(field)) {
|
if (!this._isJsonColumn(field)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
const tableName = aliases?.[table.name] || table.name
|
const aliasedTableName = aliases?.[tableName] || tableName
|
||||||
const fullName = `${tableName}.${name}`
|
const fullName = `${aliasedTableName}.${name}`
|
||||||
for (let row of results) {
|
for (let row of results) {
|
||||||
if (typeof row[fullName] === "string") {
|
if (typeof row[fullName] === "string") {
|
||||||
row[fullName] = JSON.parse(row[fullName])
|
row[fullName] = JSON.parse(row[fullName])
|
||||||
|
|
Loading…
Reference in New Issue