Persist and Flush
There are 2 methods we should first describe to understand how persisting works in MikroORM:
em.persist(entity) is used to mark new entities for future persisting.
It will make the entity managed by given
EntityManager and once
flush will be called, it
will be written to the database.
flush, lets first define what managed entity is: An entity is managed if
it’s fetched from the database (via
em.findOne() or via other managed entity)
or registered as new through
em.flush() will go through all managed entities, compute appropriate change sets and
perform according database queries. As an entity loaded from database becomes managed
automatically, you do not have to call persist on those, and flush is enough to update
Persisting and Cascading
To save entity state to database, you need to persist it. Persist takes care or deciding
whether to use
update and computes appropriate change-set. Entity references
that are not persisted yet (does not have identifier) will be cascade persisted automatically.
Fetching Entities with EntityManager
To fetch entities from database you can use
To populate entity relations, you can use
You can also use
em.populate() helper to populate relations (or to ensure they
are fully populated) on already loaded entities. This is also handy when loading
Conditions Object (
Querying entities via conditions object (
em.find(Entity, where: FilterQuery<T>))
supports many different ways:
As you can see in the fifth example, one can also use operators like
$re. More about that can be found in
Query Conditions section.
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
Another problem you might be facing is
RangeError: Maximum call stack size exceeded error
thrown during TypeScript compilation (usually from file
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 this and all requested relationships will be automatically joined. Currently it will only join them so you can search and sort by those. To populate entities, do not forget to pass the populate parameter as well.
This feature is fully available only for SQL drivers. In MongoDB always you need to query from the owning side - so in the example above, first load book tag by name, then associated book, then the author. Another option is to denormalize the schema.
Fetching Partial Entities
When fetching single entity, you can choose to select only parts of an entity via
Fetching Paginated Results
If you are going to paginate your results, you can use
em.findAndCount() that will return
total count of entities before applying limit and offset.
Handling Not Found Entities
When you call
em.findOne() and no entity is found based on your criteria,
null will be
returned. If you rather have an
Error instance thrown, you can use
You can customize the error either globally via
findOneOrFailHandler option, or locally via
failHandler option in
Type of Fetched Entities
em.findOne() methods have generic return types.
All of following examples are equal and will let typescript correctly infer the entity type:
As the last one is the least verbose, it should be preferred.
Although you can use
EntityManager directly, much more convenient way is to use
EntityRepository instead. You can register
your repositories in dependency injection container like InversifyJS
so you do not need to get them from
EntityManager each time.