Under the hood,
ts-morph to read
TypeScript source files of all entities to be able to detect all types. Thanks to this,
defining the type is enough for runtime validation.
This has some consequences for deployment of your application. Sometimes you will want to deploy only your compiled output, without TS source files at all. In that case, discovery process will probably fail. You have several options:
By default, output of metadata discovery will be cached in
temp folder. You can reuse this
cache in your deployed application. Currently the cache is saved in files named like the entity
source file, e.g.
Author.ts entity will store cache in
When running compiled code, JS entities will be taken into account instead, so you will need to
generate the cache by running the compiled code locally. That will generate
which is the file you will need to deploy alongside your application.
What discovery process does is to sniff TS types and save their value to string, so it can be used later for validation. You can skip the whole process by simply providing those values manually:
For numeric enums this is not be required.
Usually it does not matter much that you deploy more files than needed, so the easiest way is to just deploy your TS source files next to the compiled output, just like during development.
Webpack can be used to bundle every entity and dependency: you get a single file that contains every required module/file and has no external dependencies.
Webpack requires every required file to be hardcoded in your code. Code like this won't work (it will throw an error because Webpack doesn't know which file to include in the bundle):
As Webpack creates a file bundle, it isn't desired that it scans directories for entities
or metadata. Therefore you need to provide list of entities in the
entities option in
the initialization function, folder/file based discovery is not supported (see dynamically
including entities as an alternative solution). Also you need to fill
attributes everywhere (see above) and disable caching (it will decrease start-time slightly).
In v4 caching is disabled by default when using
First thing you should do is to disable dynamic file access in the discovery process via the
discovery.disableDynamicFileAccess toggle. This will effectively do:
- set metadata provider to
- disable caching
- disallow usage of paths in
This will make use of a Webpack feature called dynamic imports. This way you can import dependencies as long as part of the path is known.
In following example
is used. This 'function' is only usable during the building process from Webpack so therefore
there is an alternative solution provided that will as long as the environment variable
WEBPACK is not set (e.g. during development with
Here, all files with the extension
.ts will be imported from the directory
Webpack can be run without configuration file but
for building MikroORM and Node.js bundles it requires additional
configuration. Configuration for Webpack is stored in the root of the project as
webpack.config.js. For all the options please refer to the following page.
For bundling MikroORM the following configuration is required:
To run Webpack execute
npx webpack if not installed globally) in the root
of the project. It will probably throw a few warnings but you can ignore the errors regarding
MikroORM: the mentioned pieces of code won't be executed if properly bundled with Webpack.