Skip to content

Commit

Permalink
🐛 Fix ORM query builder with $in option for postgres (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
MontaGhanmy authored Dec 16, 2024
1 parent 64ec163 commit 582d12c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ====
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]};
Expand Down

0 comments on commit 582d12c

Please sign in to comment.