Quick Start
First install the module via yarn
or npm
and do not forget to install the
database driver as well:
or
Next you will need to enable support for decorators
as well as esModuleInterop
in tsconfig.json
via:
Then call MikroORM.init
as part of bootstrapping your app:
There are more ways to configure your entities, take a look at installation page.
Read more about all the possible configuration options in Advanced Configuration section.
Then you will need to fork entity manager for each request so their
identity maps will not collide.
To do so, use the RequestContext
helper:
You should register this middleware as the last one just before request handlers and before any of your custom middleware that is using the ORM. There might be issues when you register it before request processing middleware like
queryParser
orbodyParser
, so definitely register the context after them.
More info about RequestContext
is described here.
Now you can start defining your entities (in one of the entities
folders). This is how
simple entity can look like in mongo driver:
For SQL drivers, you can use id: number
PK:
Or if you want to use UUID primary keys:
More information can be found in defining entities section in docs.
When you have your entities defined, you can start using ORM either via EntityManager
or via EntityRepository
s.
To save entity state to database, you need to persist it. Persist determines
whether to use insert
or update
and computes appropriate change-set. Entity references
that are not persisted yet (does not have identifier) will be cascade persisted automatically.
To fetch entities from database you can use find()
and findOne()
of EntityManager
:
More convenient way of fetching entities from database is by using EntityRepository
, that
carries the entity name so you do not have to pass it to every find
and findOne
calls:
Take a look at docs about working with EntityManager
or using EntityRepository
instead.