I'm using Mikro-orm (6.1.5) in serverless functions with a shared db package where I keep all my entities. Since the app started to get bigger I noticed a slow down and realised all my entities were getting loaded in the discovery phase in the start, even though I only pass down the ones that are being used in each function when calling MikroOrm.init() like so:
entities: [EntitiyA, EntityB]
As far as I can see there's a default metadata provider, responsible for fetching the entities - ReflectMetadataProvider. Do I need to implement my own MetadataProvider or there's a simpler way? I still can't understand why is there an entity array in the config when it's not respected in the discovery.
Thanks.
You can't discover just something while keeping references to other undiscovered entities in those you discover, this is all validated and if we didn't have the autodiscovery based on decorator options in place, you would just see an error on init anyway. The only way to do this is to actually have an isolated entity graph in your entities (so not refenrece anything you don't want to discover).
The discovery should be pretty fast (and if it's not, you can cache the metadata), are you sure it's about that? If you enable debug logs, you should see how long it takes. You might as well want to use the new initSync
method which is generally faster (because it does less). Also for production build, you can compile cached metadata into a single file and load that statically, that should give you a speed bump too in such environment. Check this guide on how to do that (you dont need to use the ts-morph provider to be able to use it):