From 582d12c462b7309ba833f46e763da48fa858b039 Mon Sep 17 00:00:00 2001 From: Montassar Ghanmy Date: Mon, 16 Dec 2024 16:02:18 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20ORM=20query=20builder=20wi?= =?UTF-8?q?th=20$in=20option=20for=20postgres=20(#771)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../connectors/postgres/postgres-query-builder.ts | 13 +++++++++++-- .../src/services/user/services/users/service.ts | 2 +- .../postgres/postgres-query-builder.test.ts | 10 ++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres-query-builder.ts b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres-query-builder.ts index a223035ee..0aa9bdb47 100644 --- a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres-query-builder.ts +++ b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres-query-builder.ts @@ -83,8 +83,17 @@ export class PostgresQueryBuilder { // === IN === options.$in?.forEach(e => { - whereClause += `${e[0]} IN ($${e[1].map(() => idx++).join(",$")}) AND `; - values.push(...e[1]); + const [column, valuesArray] = e; + + // throw an error if the values array is empty + if (!valuesArray || valuesArray.length === 0) { + throw new Error( + `The $in operator for column '${column}' requires a non-empty array of values.`, + ); + } + + whereClause += `${column} IN ($${valuesArray.map(() => idx++).join(",$")}) AND `; + values.push(...valuesArray); }); // === LIKE ==== diff --git a/tdrive/backend/node/src/services/user/services/users/service.ts b/tdrive/backend/node/src/services/user/services/users/service.ts index d72091664..818979b7a 100644 --- a/tdrive/backend/node/src/services/user/services/users/service.ts +++ b/tdrive/backend/node/src/services/user/services/users/service.ts @@ -169,7 +169,7 @@ export class UserServiceImpl { pagination, }; - if (options?.userIds) { + if (Array.isArray(options?.userIds) && options.userIds.length > 0) { findOptions.$in = [["id", options.userIds]]; } diff --git a/tdrive/backend/node/test/unit/core/services/database/services/orm/connectors/postgres/postgres-query-builder.test.ts b/tdrive/backend/node/test/unit/core/services/database/services/orm/connectors/postgres/postgres-query-builder.test.ts index 6145b1d95..010572f3a 100644 --- a/tdrive/backend/node/test/unit/core/services/database/services/orm/connectors/postgres/postgres-query-builder.test.ts +++ b/tdrive/backend/node/test/unit/core/services/database/services/orm/connectors/postgres/postgres-query-builder.test.ts @@ -93,6 +93,16 @@ describe('The PostgresQueryBuilder', () => { expect(query[1]).toEqual(options.$in[0][1]); }); + test('buildSelect query "in" options with empty array', async () => { + //given + const options = { $in: [["added", []] as comparisonType] }; + + // When & Then + expect(() => { + subj.buildSelect(TestDbEntity, null, options); + }).toThrowError("The $in operator for column 'added' requires a non-empty array of values."); + }); + test('buildSelect query "like" options', async () => { //given const options = { $like: [["id", randomUUID()] as comparisonType, ["company_id", randomUUID()] as comparisonType]};