First install the module via
npm and do not forget to install the
database driver as well:
Next you will need to enable support for decorators
as well as
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
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
bodyParser, 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
To save entity state to database, you need to persist it. Persist determines
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.
To fetch entities from database you can use
More convenient way of fetching entities from database is by using
carries the entity name so you do not have to pass it to every