From 157e75b9a6224bf69489ca4d894cff973cab2c3d Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 6 Sep 2024 19:34:02 +0100 Subject: [PATCH] Using a CTE for the main query, then adding the JSON aggregation on afterwards - fixing issue with offset pagination applying the JSON aggregation to all rows before hand. --- packages/backend-core/src/sql/sql.ts | 31 +++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/packages/backend-core/src/sql/sql.ts b/packages/backend-core/src/sql/sql.ts index d433cca504..05c9b036ca 100644 --- a/packages/backend-core/src/sql/sql.ts +++ b/packages/backend-core/src/sql/sql.ts @@ -1202,12 +1202,33 @@ class InternalBuilder { if (!counting) { query = this.addSorting(query) } - // handle joins - if (relationships) { - query = this.addJsonRelationships(query, tableName, relationships) - } - return this.addFilters(query, filters, { relationship: true }) + query = this.addFilters(query, filters, { relationship: true }) + + // SQLite (SQS) cannot use the WITH statement yet + if (relationships?.length && this.client === SqlClient.SQL_LITE) { + return this.addJsonRelationships(query, tableName, relationships) + } + // handle relationships with a CTE for all others + else if (relationships?.length) { + const mainTable = + this.query.tableAliases?.[this.query.endpoint.entityId] || + this.query.endpoint.entityId + const cte = this.addSorting( + this.knex + .with("paginated", query) + .select(this.generateSelectStatement()) + .from({ + [mainTable]: "paginated", + }) + ) + // add JSON aggregations attached to the CTE + return this.addJsonRelationships(cte, tableName, relationships) + } + // no relationships found - return query + else { + return query + } } update(opts: QueryOptions): Knex.QueryBuilder {