Fix sqs
This commit is contained in:
parent
225d062fcc
commit
8a6dbef249
|
@ -406,23 +406,26 @@ class InternalBuilder {
|
||||||
addRelationshipForFilter(
|
addRelationshipForFilter(
|
||||||
query: Knex.QueryBuilder,
|
query: Knex.QueryBuilder,
|
||||||
filterKey: string,
|
filterKey: string,
|
||||||
whereCb: (query: Knex.QueryBuilder) => Knex.QueryBuilder
|
whereCb: (filterKey: string, query: Knex.QueryBuilder) => Knex.QueryBuilder
|
||||||
): Knex.QueryBuilder {
|
): Knex.QueryBuilder {
|
||||||
const mainKnex = this.knex
|
const mainKnex = this.knex
|
||||||
const { relationships, endpoint, tableAliases: aliases } = this.query
|
const { relationships, endpoint, tableAliases: aliases } = this.query
|
||||||
const tableName = endpoint.entityId
|
const tableName = endpoint.entityId
|
||||||
const fromAlias = aliases?.[tableName] || tableName
|
const fromAlias = aliases?.[tableName] || tableName
|
||||||
const matches = (possibleTable: string) =>
|
const matches = (value: string) => filterKey.startsWith(`${value}`)
|
||||||
filterKey.startsWith(`${possibleTable}`)
|
|
||||||
if (!relationships) {
|
if (!relationships) {
|
||||||
return query
|
return query
|
||||||
}
|
}
|
||||||
for (const relationship of relationships) {
|
for (const relationship of relationships) {
|
||||||
const relatedTableName = relationship.tableName
|
const relatedTableName = relationship.tableName
|
||||||
const toAlias = aliases?.[relatedTableName] || relatedTableName
|
const toAlias = aliases?.[relatedTableName] || relatedTableName
|
||||||
|
|
||||||
|
const matchesTableName = matches(relatedTableName) || matches(toAlias)
|
||||||
|
const matchesRelationName = matches(relationship.column)
|
||||||
|
|
||||||
// this is the relationship which is being filtered
|
// this is the relationship which is being filtered
|
||||||
if (
|
if (
|
||||||
(matches(relatedTableName) || matches(toAlias)) &&
|
(matchesTableName || matchesRelationName) &&
|
||||||
relationship.to &&
|
relationship.to &&
|
||||||
relationship.tableName
|
relationship.tableName
|
||||||
) {
|
) {
|
||||||
|
@ -430,6 +433,17 @@ class InternalBuilder {
|
||||||
.select(mainKnex.raw(1))
|
.select(mainKnex.raw(1))
|
||||||
.from({ [toAlias]: relatedTableName })
|
.from({ [toAlias]: relatedTableName })
|
||||||
const manyToMany = validateManyToMany(relationship)
|
const manyToMany = validateManyToMany(relationship)
|
||||||
|
let updatedKey
|
||||||
|
|
||||||
|
if (matchesRelationName) {
|
||||||
|
updatedKey = filterKey.replace(
|
||||||
|
new RegExp(`^${relationship.column}.`),
|
||||||
|
`${aliases![relationship.tableName]}.`
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
updatedKey = filterKey
|
||||||
|
}
|
||||||
|
|
||||||
if (manyToMany) {
|
if (manyToMany) {
|
||||||
const throughAlias =
|
const throughAlias =
|
||||||
aliases?.[manyToMany.through] || relationship.through
|
aliases?.[manyToMany.through] || relationship.through
|
||||||
|
@ -470,7 +484,7 @@ class InternalBuilder {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
query = query.whereExists(whereCb(subQuery))
|
query = query.whereExists(whereCb(updatedKey, subQuery))
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -558,9 +572,13 @@ class InternalBuilder {
|
||||||
if (allOr) {
|
if (allOr) {
|
||||||
query = query.or
|
query = query.or
|
||||||
}
|
}
|
||||||
query = builder.addRelationshipForFilter(query, updatedKey, q => {
|
query = builder.addRelationshipForFilter(
|
||||||
|
query,
|
||||||
|
updatedKey,
|
||||||
|
(updatedKey, q) => {
|
||||||
return handleRelationship(q, updatedKey, value)
|
return handleRelationship(q, updatedKey, value)
|
||||||
})
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,11 +39,6 @@ import AliasTables from "../../sqlAlias"
|
||||||
import { outputProcessing } from "../../../../../utilities/rowProcessor"
|
import { outputProcessing } from "../../../../../utilities/rowProcessor"
|
||||||
import pick from "lodash/pick"
|
import pick from "lodash/pick"
|
||||||
import { processRowCountResponse } from "../../utils"
|
import { processRowCountResponse } from "../../utils"
|
||||||
import {
|
|
||||||
getRelationshipColumns,
|
|
||||||
getTableIDList,
|
|
||||||
updateFilterKeys,
|
|
||||||
} from "../filters"
|
|
||||||
import {
|
import {
|
||||||
dataFilters,
|
dataFilters,
|
||||||
helpers,
|
helpers,
|
||||||
|
@ -133,31 +128,7 @@ async function buildInternalFieldList(
|
||||||
return [...new Set(fieldList)]
|
return [...new Set(fieldList)]
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanupFilters(
|
function cleanupFilters(filters: SearchFilters, allTables: Table[]) {
|
||||||
filters: SearchFilters,
|
|
||||||
table: Table,
|
|
||||||
allTables: Table[]
|
|
||||||
) {
|
|
||||||
// get a list of all relationship columns in the table for updating
|
|
||||||
const relationshipColumns = getRelationshipColumns(table)
|
|
||||||
// get table names to ID map for relationships
|
|
||||||
const tableNameToID = getTableIDList(allTables)
|
|
||||||
// all should be applied at once
|
|
||||||
filters = updateFilterKeys(
|
|
||||||
filters,
|
|
||||||
relationshipColumns
|
|
||||||
.map(({ name, definition }) => ({
|
|
||||||
original: name,
|
|
||||||
updated: definition.tableId,
|
|
||||||
}))
|
|
||||||
.concat(
|
|
||||||
tableNameToID.map(({ name, id }) => ({
|
|
||||||
original: name,
|
|
||||||
updated: id,
|
|
||||||
}))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
// generate a map of all possible column names (these can be duplicated across tables
|
// generate a map of all possible column names (these can be duplicated across tables
|
||||||
// the map of them will always be the same
|
// the map of them will always be the same
|
||||||
const userColumnMap: Record<string, string> = {}
|
const userColumnMap: Record<string, string> = {}
|
||||||
|
@ -356,7 +327,7 @@ export async function search(
|
||||||
const relationships = buildInternalRelationships(table, allTables)
|
const relationships = buildInternalRelationships(table, allTables)
|
||||||
|
|
||||||
const searchFilters: SearchFilters = {
|
const searchFilters: SearchFilters = {
|
||||||
...cleanupFilters(query, table, allTables),
|
...cleanupFilters(query, allTables),
|
||||||
documentType: DocumentType.ROW,
|
documentType: DocumentType.ROW,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue