Skip to content

Commit

Permalink
fix(core): ignore limit, offset and order in em.count on virtual en…
Browse files Browse the repository at this point in the history
…tity
  • Loading branch information
B4nan committed Nov 2, 2023
1 parent cef26c5 commit 03a7b86
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
9 changes: 6 additions & 3 deletions packages/knex/src/AbstractSqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,16 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection

protected async wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any>, type: QueryType): Promise<T[] | number> {
const qb = this.createQueryBuilder(meta.className, options?.ctx, options.connectionType, options.convertCustomTypes)
.limit(options?.limit, options?.offset)
.indexHint(options.indexHint!)
.comment(options.comments!)
.hintComment(options.hintComments!);

if (options.orderBy) {
qb.orderBy(options.orderBy);
if (type !== QueryType.COUNT) {
qb.limit(options?.limit, options?.offset);

if (options.orderBy) {
qb.orderBy(options.orderBy);
}
}

qb.where(where);
Expand Down
25 changes: 24 additions & 1 deletion tests/features/virtual-entities/virtual-entities.sqlite.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntitySchema, MikroORM, ReferenceType } from '@mikro-orm/core';
import { EntitySchema, MikroORM, QueryFlag, ReferenceType } from '@mikro-orm/core';
import type { EntityManager } from '@mikro-orm/better-sqlite';
import { mockLogger } from '../../bootstrap';
import type { IAuthor4 } from '../../entities-schema';
Expand Down Expand Up @@ -299,6 +299,29 @@ describe('virtual entities (sqlite)', () => {
expect(orm.em.getUnitOfWork().getIdentityMap().keys()).toHaveLength(0);
expect(mock.mock.calls[0][0]).toMatch(sql);
expect(orm.em.getUnitOfWork().getIdentityMap().keys()).toHaveLength(0);

// pagination
{
const [books, total] = await orm.em.findAndCount(BookWithAuthor, {}, { flags: [QueryFlag.PAGINATE], limit: 3, offset: 3 });
expect(books).toEqual([
{
title: 'My Life on the Wall, part 1/2',
authorName: 'Jon Snow 2',
tags: ['silly-2', 'sick-2'],
},
{
title: 'My Life on the Wall, part 2/2',
authorName: 'Jon Snow 2',
tags: ['silly-2', 'funny-2', 'sexy-2'],
},
{
title: 'My Life on the Wall, part 3/2',
authorName: 'Jon Snow 2',
tags: ['funny-2', 'strange-2', 'sexy-2'],
},
]);
expect(total).toBe(9);
}
});

});

0 comments on commit 03a7b86

Please sign in to comment.