Skip to content

Commit

Permalink
docs: add note about possible TS inference problems with FilterQuery
Browse files Browse the repository at this point in the history
Closes #208
  • Loading branch information
B4nan committed Oct 23, 2019
1 parent acb1d24 commit ee240ef
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
25 changes: 24 additions & 1 deletion docs/entity-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ for (const author of authors) {
}
```

### Conditions Object
### Conditions Object (FilterQuery<T>)

Querying entities via conditions object (`where` in `em.find(Entity, where: FilterQuery<T>)`)
supports many different ways:
Expand Down Expand Up @@ -134,6 +134,29 @@ As you can see in the fifth example, one can also use operators like `$and`, `$o
`$gt`, `$lte`, `$lt`, `$in`, `$nin`, `$eq`, `$ne`. More about that can be found in
[Query Conditions](query-conditions.md) section.

#### Mitigating `Type instantiation is excessively deep and possibly infinite.ts(2589)` error

Sometimes you might be facing TypeScript errors caused by too complex query for it to
properly infer all types. Usually it can be solved by providing the type argument
explicitly.

You can also opt in to use repository instead, as there the type inference should not be
problematic.

> As a last resort, you can always type cast the query to `any`.
```typescript
const books = await orm.em.find<Book>(Book, { ... your complex query ... });
// or
const books = await orm.em.getRepository(Book).find({ ... your complex query ... });
// or
const books = await orm.em.find<any>(Book, { ... your complex query ... }) as Book[];
```

Another problem you might be facing is `RangeError: Maximum call stack size exceeded` error
thrown during TypeScript compilation (usually from file `node_modules/typescript/lib/typescript.js`).
The solution to this is the same, just provide the type argument explicitly.

### Searching by referenced entity fields

You can also search by referenced entity properties. Simply pass nested where condition like
Expand Down
8 changes: 6 additions & 2 deletions tests/EntityManager.mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1376,8 +1376,12 @@ describe('EntityManagerMySql', () => {
author.books.add(book1, book2, book3);
await orm.em.persistAndFlush(author);
await expect(orm.em.count(Book2, [book1.uuid, book2.uuid, book3.uuid])).resolves.toBe(3);
// this test was causing TS recursion errors, see https://github.com/mikro-orm/mikro-orm/issues/124
// await expect(orm.em.count(Book2, [book1, book2, book3])).resolves.toBe(3);
// this test was causing TS recursion errors without the type argument
// see https://github.com/mikro-orm/mikro-orm/issues/124 and https://github.com/mikro-orm/mikro-orm/issues/208
await expect(orm.em.count<Book2>(Book2, [book1, book2, book3])).resolves.toBe(3);
await expect(orm.em.count<any>(Book2, [book1, book2, book3])).resolves.toBe(3);
const a = await orm.em.find<any>(Book2, [book1, book2, book3]) as Book2[];
await expect(orm.em.getRepository(Book2).count([book1, book2, book3])).resolves.toBe(3);
});

test('find by joined property', async () => {
Expand Down

0 comments on commit ee240ef

Please sign in to comment.