-
-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sql): fix prefixing of JSON queries nested on relations
Closes #3242
- Loading branch information
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Collection, Entity, JsonType, ManyToOne, MikroORM, OneToMany, PrimaryKey, Property } from '@mikro-orm/core'; | ||
import { mockLogger } from '../helpers'; | ||
|
||
@Entity() | ||
export class Author { | ||
|
||
@PrimaryKey() | ||
id!: string; | ||
|
||
@OneToMany({ | ||
entity: 'Book', | ||
mappedBy: 'author', | ||
}) | ||
books = new Collection<Book>(this); | ||
|
||
} | ||
|
||
@Entity() | ||
export class Book { | ||
|
||
@PrimaryKey() | ||
id!: string; | ||
|
||
@Property({ type: JsonType }) | ||
data!: Record<string, unknown>; | ||
|
||
@ManyToOne(() => Author) | ||
author!: Author; | ||
|
||
} | ||
|
||
describe('aliasing of nested JSON queries (GH 3242)', () => { | ||
|
||
test('sqlite', async () => { | ||
const orm = await MikroORM.init({ | ||
entities: [Book], | ||
dbName: ':memory:', | ||
type: 'better-sqlite', | ||
}); | ||
await orm.getSchemaGenerator().createSchema(); | ||
|
||
const mock = mockLogger(orm); | ||
await orm.em.find(Author, { books: { data: { title: 'test' } } }); | ||
await orm.em.find(Author, { books: { id: 'test' } }); | ||
await orm.em.find(Book, { data: { title: 'test' } }); | ||
|
||
expect(mock.mock.calls[0][0]).toMatch("select `a0`.* from `author` as `a0` left join `book` as `b1` on `a0`.`id` = `b1`.`author_id` where json_extract(`b1`.`data`, '$.title') = 'test'"); | ||
expect(mock.mock.calls[1][0]).toMatch("select `a0`.* from `author` as `a0` left join `book` as `b1` on `a0`.`id` = `b1`.`author_id` where `b1`.`id` = 'test'"); | ||
expect(mock.mock.calls[2][0]).toMatch("select `b0`.* from `book` as `b0` where json_extract(`b0`.`data`, '$.title') = 'test'"); | ||
|
||
await orm.close(true); | ||
}); | ||
|
||
test('mysql', async () => { | ||
const orm = await MikroORM.init({ | ||
entities: [Book], | ||
dbName: 'mikro_orm_test_3242', | ||
type: 'mysql', | ||
port: 3308, | ||
}); | ||
await orm.getSchemaGenerator().refreshDatabase(); | ||
|
||
const mock = mockLogger(orm); | ||
await orm.em.find(Author, { books: { data: { title: 'test' } } }); | ||
await orm.em.find(Author, { books: { id: 'test' } }); | ||
await orm.em.find(Book, { data: { title: 'test' } }); | ||
|
||
expect(mock.mock.calls[0][0]).toMatch("select `a0`.* from `author` as `a0` left join `book` as `b1` on `a0`.`id` = `b1`.`author_id` where `b1`.`data`->'$.title' = 'test'"); | ||
expect(mock.mock.calls[1][0]).toMatch("select `a0`.* from `author` as `a0` left join `book` as `b1` on `a0`.`id` = `b1`.`author_id` where `b1`.`id` = 'test'"); | ||
expect(mock.mock.calls[2][0]).toMatch("select `b0`.* from `book` as `b0` where `b0`.`data`->'$.title' = 'test'"); | ||
|
||
await orm.close(true); | ||
}); | ||
|
||
test('postgres', async () => { | ||
const orm = await MikroORM.init({ | ||
entities: [Book], | ||
dbName: 'mikro_orm_test_3242', | ||
type: 'postgresql', | ||
}); | ||
await orm.getSchemaGenerator().refreshDatabase(); | ||
|
||
const mock = mockLogger(orm); | ||
await orm.em.find(Author, { books: { data: { title: 'test' } } }); | ||
await orm.em.find(Author, { books: { id: 'test' } }); | ||
await orm.em.find(Book, { data: { title: 'test' } }); | ||
|
||
expect(mock.mock.calls[0][0]).toMatch(`select "a0".* from "author" as "a0" left join "book" as "b1" on "a0"."id" = "b1"."author_id" where "b1"."data"->>'title' = 'test'`); | ||
expect(mock.mock.calls[1][0]).toMatch(`select "a0".* from "author" as "a0" left join "book" as "b1" on "a0"."id" = "b1"."author_id" where "b1"."id" = 'test'`); | ||
expect(mock.mock.calls[2][0]).toMatch(`select "b0".* from "book" as "b0" where "b0"."data"->>'title' = 'test'`); | ||
|
||
await orm.close(true); | ||
}); | ||
|
||
}); |