Some improvements to get SQS tests passing.

This commit is contained in:
mike12345567 2024-08-29 18:56:14 +01:00
parent 3e51dde6d2
commit a9b1a22403
3 changed files with 25 additions and 5 deletions

View File

@ -337,6 +337,11 @@ class InternalBuilder {
return filters return filters
} }
addJoinFieldCheck(query: Knex.QueryBuilder, relationship: RelationshipsJson) {
const document = relationship.from?.split(".")[0] || ""
return query.andWhere(`${document}.fieldName`, "=", relationship.column)
}
addRelationshipForFilter( addRelationshipForFilter(
query: Knex.QueryBuilder, query: Knex.QueryBuilder,
filterKey: string, filterKey: string,
@ -363,8 +368,6 @@ class InternalBuilder {
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 =
@ -375,12 +378,15 @@ class InternalBuilder {
}) })
subQuery = subQuery.innerJoin(throughTable, function () { subQuery = subQuery.innerJoin(throughTable, function () {
// @ts-ignore // @ts-ignore
this.orOn( this.on(
`${toAlias}.${relationship.toPrimary}`, `${toAlias}.${relationship.toPrimary}`,
"=", "=",
`${throughAlias}.${relationship.to}` `${throughAlias}.${relationship.to}`
) )
}) })
if (this.client === SqlClient.SQL_LITE) {
subQuery = this.addJoinFieldCheck(subQuery, relationship)
}
mainTableRelatesTo = throughAlias mainTableRelatesTo = throughAlias
} }
// "join" to the main table, making sure the ID matches that of the main // "join" to the main table, making sure the ID matches that of the main
@ -907,7 +913,7 @@ class InternalBuilder {
default: default:
throw new Error(`JSON relationships not implement for ${this.client}`) throw new Error(`JSON relationships not implement for ${this.client}`)
} }
const subQuery = this.knex let subQuery = this.knex
.select(rawJsonArray) .select(rawJsonArray)
.from(toTableWithSchema) .from(toTableWithSchema)
.join(throughTableWithSchema, function () { .join(throughTableWithSchema, function () {
@ -918,6 +924,12 @@ class InternalBuilder {
"=", "=",
this.knex.raw(this.quotedIdentifier(`${fromAlias}.${fromPrimary}`)) this.knex.raw(this.quotedIdentifier(`${fromAlias}.${fromPrimary}`))
) )
// relationships should never have more than the base limit
.limit(getBaseLimit())
// need to check the junction table document is to the right column
if (this.client === SqlClient.SQL_LITE) {
subQuery = this.addJoinFieldCheck(subQuery, relationship)
}
query = query.select({ [relationship.column]: subQuery }) query = query.select({ [relationship.column]: subQuery })
} }
return query return query

View File

@ -2687,7 +2687,8 @@ describe.each([
}) })
}) })
isSql && // TODO: when all SQL databases use the same mechanism - remove this test, new relationship system doesn't have this problem
!isInternal &&
describe("pagination edge case with relationships", () => { describe("pagination edge case with relationships", () => {
let mainRows: Row[] = [] let mainRows: Row[] = []

View File

@ -336,6 +336,13 @@ export async function outputProcessing<T extends Row[] | Row>(
row[property] = `${hours}:${minutes}:${seconds}` row[property] = `${hours}:${minutes}:${seconds}`
} }
} }
} else if (column.type === FieldType.LINK) {
for (let row of enriched) {
// if relationship is empty - remove the array, this has been part of the API for some time
if (Array.isArray(row[property]) && row[property].length === 0) {
delete row[property]
}
}
} }
} }