Skip to content

Commit

Permalink
fix(query-builder): fix join on conditions where or operator
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Mar 12, 2024
1 parent ab48c8d commit 92936ef
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/knex/src/query/QueryBuilder.ts
Expand Up @@ -1421,6 +1421,9 @@ export class QueryBuilder<T extends object = AnyEntity> {
if (join) {
if (join.cond[k]) {
join.cond = { [op ?? '$and']: [join.cond, { [k]: cond[k] }] };
} else if (op === '$or') {
join.cond.$or ??= [];
join.cond.$or.push({ [k]: cond[k] });
} else {
join.cond = { ...join.cond, [k]: cond[k] };
}
Expand Down
30 changes: 30 additions & 0 deletions tests/features/collection/populate-where-infer.test.ts
Expand Up @@ -13,6 +13,9 @@ class User {
@OneToMany(() => Server, x => x.user)
servers = new Collection<Server>(this);

@Property()
name!: string;

@Property({ type: 'json', nullable: true })
data: any;

Expand Down Expand Up @@ -65,6 +68,7 @@ beforeAll(async () => {
{ name: 's2' },
{ name: 'test' },
],
name: 'u1',
data: { foo: 'bar' },
});
orm.em.create(User, {
Expand All @@ -75,6 +79,7 @@ beforeAll(async () => {
servers: [
{ name: 'test' },
],
name: 'u2',
data: { foo: 'baz' },
});

Expand Down Expand Up @@ -209,3 +214,28 @@ test('invalid query 3', async () => {
expect(mock.mock.calls[2][0]).toMatch(`select "u0".*, "s"."id" as "s__id", "s"."name" as "s__name", "s"."user_id" as "s__user_id" from "user" as "u0" left join "server" as "s" on "u0"."id" = "s"."user_id" where "u0"."id" in (select "u0"."id" from (select "u0"."id" from "user" as "u0" left join "server" as "s" on "u0"."id" = "s"."user_id" where "u0"."data"->>'foo' is not null group by "u0"."id" limit 3) as "u0")`);
expect(mock.mock.calls[3][0]).toMatch(`select count(distinct("u0"."id")) as "count" from "user" as "u0" left join "server" as "s" on "u0"."id" = "s"."user_id" where "u0"."data"->>'foo' is not null`);
});

test('invalid query 4', async () => {
const mock = mockLogger(orm);
const results = await orm.em.fork().find(
Server,
{
user: {
$or: [
{
name: 'u1',
},
{
id: null,
},
],
},
},
{
populate: ['user'],
populateWhere: 'infer',
logging: { enabled: true },
},
);
expect(results[3].user).toBeNull();
});

0 comments on commit 92936ef

Please sign in to comment.