Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): support calling
em.findAndCount()
on virtual entities wi…
…th `orderBy` Closes #4628
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Entity, PrimaryKey, Property, SimpleLogger } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/postgresql'; | ||
import { mockLogger } from '../../bootstrap'; | ||
|
||
@Entity() | ||
class Book { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property() | ||
title!: string; | ||
|
||
} | ||
|
||
@Entity({ expression: 'select title from book' }) | ||
class BookSimple { | ||
|
||
@Property() | ||
title!: string; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: 'virtual_entities', | ||
entities: [Book, BookSimple], | ||
loggerFactory: options => new SimpleLogger(options), | ||
}); | ||
await orm.schema.refreshDatabase(); | ||
}); | ||
beforeEach(async () => orm.schema.clearDatabase()); | ||
afterAll(async () => orm.close(true)); | ||
|
||
test('virtual entities (postgres)', async () => { | ||
const mock = mockLogger(orm); | ||
await orm.em.findAndCount(BookSimple, {}, { orderBy: { title: 1 } }); | ||
expect(mock.mock.calls.map(r => r[0]).sort()).toEqual([ | ||
`[query] select * from (select title from book) as "b0" order by "b0"."title" asc`, | ||
`[query] select count(*) as count from (select title from book) as "b0"`, | ||
]); | ||
}); |