Installation & Usage
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:
Read more about all the possible configuration options in Advanced Configuration section.
You can also provide paths where you store your entities via entitiesDirs
array. Internally
it uses globby
so you can use
globbing patterns.
You should provide list of directories, not paths to entities directly. If you want to do that
instead, you should use entities
array and use globby
manually:
You can pass additional options to the underlying driver (e.g.
mysql2
) viadriverOptions
. The object will be deeply merged, overriding all internally used options.
#
Possible issues with circular dependenciesYour entities will most probably contain circular dependencies (e.g. if you use bi-directional
relationship). While this is fine, there might be issues caused by wrong order of entities
during discovery, especially when you are using the folder based way (via entitiesDirs
).
The errors caused by circular dependencies are usually similar to this one:
If you encounter this, you have basically two options:
- Use
entities
array to have control over the order of discovery. You might need to play with the actual order you provide here, or possibly with the order of import statements. - Use strings instead of references (e.g.
@OneToMany('Book', 'author')
). The downside here is that you will loose the typechecking capabilities of the decorators.
#
Entity Discovery in TypeScriptInternally, MikroORM
uses ts-morph
to perform analysis of source files
of entities to sniff types of all properties. This process can be slow if your project contains lots
of files. To speed up the discovery process a bit, you can provide more accurate paths where your
entity source files are:
You can also use different metadata provider or even write custom one:
ReflectMetadataProvider
that usesreflect-metadata
instead ofts-morph
JavaScriptMetadataProvider
that allows you to manually provide the entity schema (mainly for Vanilla JS)
#
Setting up the Commandline ToolMikroORM ships with a number of command line tools that are very helpful during development,
like Schema Generator and Entity Generator. You can call this command from the NPM binary
directory or use npx
:
For CLI to be able to access your database, you will need to create mikro-orm.config.js
file that
exports your ORM configuration. TypeScript is also supported, just enable useTsNode
flag in your
package.json
file. There you can also set up array of possible paths to mikro-orm.config
file,
as well as use different file name:
Do not forget to install
ts-node
when enablinguseTsNode
flag.
Once you have the CLI config properly set up, you can omit the MikroORM.init()
options
parameter and the CLI config will be automatically used.
You can also use different names for this file, simply rename it in the
configPaths
array your inpackage.json
. You can also useMIKRO_ORM_CLI
environment variable with the path to overrideconfigPaths
value.
Now you should be able to start using the CLI. All available commands are listed in the CLI help:
To verify your setup, you can use mikro-orm debug
command.
When you have CLI config properly set up, you can omit the
options
parameter when callingMikroORM.init()
.
#
Request ContextThen you will need to fork Entity Manager for each request so their identity maps will not
collide. To do so, use the RequestContext
helper:
More info about RequestContext
is described here.
Now you can start defining your entities (in one of the entitiesDirs
folders).