diff --git a/packages/knex/src/query/QueryBuilderHelper.ts b/packages/knex/src/query/QueryBuilderHelper.ts index 9ed120979d1e..d895eae3182d 100644 --- a/packages/knex/src/query/QueryBuilderHelper.ts +++ b/packages/knex/src/query/QueryBuilderHelper.ts @@ -521,7 +521,6 @@ export class QueryBuilderHelper { } private processObjectSubCondition(cond: any, key: string, qb: Knex.QueryBuilder, method: 'where' | 'having', m: 'where' | 'orWhere' | 'having', type: QueryType): void { - // grouped condition for one field let value = cond[key]; const size = Utils.getObjectKeysSize(value); @@ -529,8 +528,14 @@ export class QueryBuilderHelper { return; } + // grouped condition for one field, e.g. `{ age: { $gte: 10, $lt: 50 } }` if (size > 1) { - const subCondition = Object.entries(value).map(([subKey, subValue]) => ({ [key]: { [subKey]: subValue } })); + const rawField = RawQueryFragment.getKnownFragment(key); + const subCondition = Object.entries(value).map(([subKey, subValue]) => { + key = rawField?.clone().toString() ?? key; + return ({ [key]: { [subKey]: subValue } }); + }); + return subCondition.forEach(sub => this.appendQueryCondition(type, sub, qb, '$and', method)); } diff --git a/tests/issues/GHx6.test.ts b/tests/issues/GHx6.test.ts index 8aee7463d8e3..330f52475286 100644 --- a/tests/issues/GHx6.test.ts +++ b/tests/issues/GHx6.test.ts @@ -93,3 +93,13 @@ test('raw fragments with populateOrderBy on relation', async () => { 'left join `job` as `j1` on `t0`.`custom_name` = `j1`.`id` ' + 'order by t0.created desc, j1.DateCompleted desc'); }); + +test('raw fragments with multiple items in filter', async () => { + const mock = mockLogger(orm); + await orm.em.findAll(Tag, { + where: { + [raw('id')]: { $gte: 10, $lte: 50 }, + }, + }); + expect(mock.mock.calls[0][0]).toMatch('select `t0`.* from `tag` as `t0` where id >= 10 and id <= 50'); +});