From 12db64513ba2597397daf2db9fedcab71d875c71 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 4 Sep 2024 16:21:32 +0100 Subject: [PATCH] Revert to testing against mssql 2017, attempt to get relationship aggreggation working. --- packages/backend-core/src/sql/sql.ts | 30 ++++++++++--------- packages/server/datasource-sha.env | 2 +- .../src/integrations/microsoftSqlServer.ts | 4 +-- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/backend-core/src/sql/sql.ts b/packages/backend-core/src/sql/sql.ts index 0bc9126924..b51079ac2d 100644 --- a/packages/backend-core/src/sql/sql.ts +++ b/packages/backend-core/src/sql/sql.ts @@ -913,30 +913,25 @@ class InternalBuilder { const fieldList: string = relationshipFields .map(field => jsonField(field)) .join(",") - let rawJsonArray: Knex.Raw, limit: number + let select: Knex.Raw, limit: number switch (sqlClient) { case SqlClient.SQL_LITE: - rawJsonArray = this.knex.raw( - `json_group_array(json_object(${fieldList}))` - ) + select = this.knex.raw(`json_group_array(json_object(${fieldList}))`) limit = getBaseLimit() break case SqlClient.POSTGRES: - rawJsonArray = this.knex.raw( - `json_agg(json_build_object(${fieldList}))` - ) + select = this.knex.raw(`json_agg(json_build_object(${fieldList}))`) limit = 1 break case SqlClient.MY_SQL: case SqlClient.ORACLE: - rawJsonArray = this.knex.raw( - `json_arrayagg(json_object(${fieldList}))` - ) + select = this.knex.raw(`json_arrayagg(json_object(${fieldList}))`) limit = getBaseLimit() break case SqlClient.MS_SQL: - rawJsonArray = this.knex.raw(`json_array(json_object(${fieldList}))`) - limit = 1 + // Cursed, needs some code later instead + select = this.knex.raw(`*`) + limit = getBaseLimit() break default: throw new Error(`JSON relationships not implement for ${this.client}`) @@ -944,8 +939,8 @@ class InternalBuilder { // SQL Server uses TOP - which performs a little differently to the normal LIMIT syntax // it reduces the result set rather than limiting how much data it filters over const primaryKey = `${toAlias}.${toPrimary || toKey}` - let subQuery = this.knex - .select(rawJsonArray) + let subQuery: Knex.QueryBuilder | Knex.Raw = this.knex + .select(select) .from(toTableWithSchema) .limit(limit) // add sorting to get consistent order @@ -985,6 +980,13 @@ class InternalBuilder { if (this.client === SqlClient.SQL_LITE) { subQuery = this.addJoinFieldCheck(subQuery, relationship) } + + if (this.client === SqlClient.MS_SQL) { + subQuery = this.knex.raw( + `(SELECT a.* FROM (${subQuery}) AS a FOR JSON PATH)` + ) + } + query = query.select({ [relationship.column]: subQuery }) } return query diff --git a/packages/server/datasource-sha.env b/packages/server/datasource-sha.env index 9b935ed8eb..61249d530c 100644 --- a/packages/server/datasource-sha.env +++ b/packages/server/datasource-sha.env @@ -1,4 +1,4 @@ -MSSQL_SHA=sha256:c4369c38385eba011c10906dc8892425831275bb035d5ce69656da8e29de50d8 +MSSQL_SHA=sha256:3b913841850a4d57fcfcb798be06acc88ea0f2acc5418bc0c140a43e91c4a545 MYSQL_SHA=sha256:9de9d54fecee6253130e65154b930978b1fcc336bcc86dfd06e89b72a2588ebe POSTGRES_SHA=sha256:bd0d8e485d1aca439d39e5ea99b931160bd28d862e74c786f7508e9d0053090e MONGODB_SHA=sha256:afa36bca12295b5f9dae68a493c706113922bdab520e901bd5d6c9d7247a1d8d diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index 88c75891e6..0a07371cd3 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -343,9 +343,9 @@ class SqlServerIntegration extends Sql implements DatasourcePlus { err.number ) if (readableMessage) { - throw new Error(readableMessage) + throw new Error(readableMessage, { cause: err }) } else { - throw new Error(err.message as string) + throw new Error(err.message as string, { cause: err }) } } }