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 tableName = endpoint.entityId
const fromAlias = aliases?.[tableName] || tableName
const matches = (possibleTable: string) =>
filterKey.startsWith(`${possibleTable}`)
if (!relationships) {
return query
}
for (const relationship of relationships) {
const relatedTableName = relationship.tableName
const toAlias = aliases?.[relatedTableName] || relatedTableName
// this is the relationship which is being filtered
if (
filterKey.startsWith(`${relationship.tableName}.`) &&
(matches(relatedTableName) || matches(toAlias)) &&
relationship.to &&
relationship.tableName
) {
const relatedTableName = relationship.tableName
const toAlias = aliases?.[relatedTableName] || relatedTableName
let subQuery = mainKnex
.select(mainKnex.raw(1))
.from({ [toAlias]: relatedTableName })
// relationships should never have more than the base limit
.limit(getBaseLimit())
let mainTableRelatesTo = toAlias
if (relationship.through) {
const throughAlias =