docs: improve module isolation chapter (#12565)

* docs: improve module isolation chapter

* add note about loaders running with migrations
This commit is contained in:
Shahed Nasser
2025-05-21 18:57:28 +03:00
committed by GitHub
parent 499381d119
commit bc965eb6aa
5 changed files with 15758 additions and 15430 deletions
@@ -102,12 +102,18 @@ This indicates that the loader in the `hello` module ran and logged this message
## When are Loaders Executed?
### Loaders Executed on Application Startup
When you start the Medusa application, it executes the loaders of all modules in their registration order.
A loader is executed before the module's main service is instantiated. So, you can use loaders to register in the module's container resources that you want to use in the module's service. For example, you can register a database connection.
Loaders are also useful to only load a module if a certain condition is met. For example, if you try to connect to a database in a loader but the connection fails, you can throw an error in the loader to prevent the module from being loaded. This is useful if your module depends on an external service to work.
### Loaders Executed with Migrations
Loaders are also executed when you run [migrations](../../data-models/write-migration/page.mdx). This can be useful if you need to run some task before the migrations, or you want to migrate some data to an integrated third-party system as part of the migration process.
---
## Example: Register Custom MongoDB Connection