Check for alias as well when deciding whether filter requires relationship addition.

This commit is contained in:
mike12345567 2024-08-29 17:58:11 +01:00
parent b217e83b31
commit 3e51dde6d2
1 changed files with 7 additions and 3 deletions

View File

@ -346,21 +346,25 @@ class InternalBuilder {
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) =>
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 toAlias = aliases?.[relatedTableName] || relatedTableName
// this is the relationship which is being filtered // this is the relationship which is being filtered
if ( if (
filterKey.startsWith(`${relationship.tableName}.`) && (matches(relatedTableName) || matches(toAlias)) &&
relationship.to && relationship.to &&
relationship.tableName relationship.tableName
) { ) {
const relatedTableName = relationship.tableName
const toAlias = aliases?.[relatedTableName] || relatedTableName
let subQuery = mainKnex let subQuery = mainKnex
.select(mainKnex.raw(1)) .select(mainKnex.raw(1))
.from({ [toAlias]: relatedTableName }) .from({ [toAlias]: relatedTableName })
// relationships should never have more than the base limit
.limit(getBaseLimit())
let mainTableRelatesTo = toAlias let mainTableRelatesTo = toAlias
if (relationship.through) { if (relationship.through) {
const throughAlias = const throughAlias =