Files
medusa-store/www/apps/book/app/advanced-development/loaders/outside-modules/page.mdx
Shahed Nasser 327e446974 docs: general fixes and overall changes (#7258)
* editing halfway

* edited second half

* adjust starter steps

* fix build

* typo fix
2024-05-07 18:00:28 +02:00

90 lines
2.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export const metadata = {
title: `${pageNumber} Loaders Outside Modules`,
}
# {metadata.title}
In this chapter, youll learn how to create a loader outside a module.
## Loaders in the Medusa Application
In your Medusa application, you can create loaders under the `src/loaders` directory outside a module. The Medusa application runs these loaders on start-up.
This is useful if youre performing a task on application start-up, but dont need to define it in a module.
---
## How to Create a Loader Outside a Module?
To create a loader in your Medusa application outside a module, create a TypeScript or JavaScript file under the `src/loaders` directory that default exports a function.
For example, create the file `src/loaders/hello-world.ts` with the following content:
```ts title="src/loaders/hello-world.ts"
export default function () {
console.log(
"[HELLO LOADER] Just started the Medusa application!"
)
}
```
This loader logs the message `[HELLO LOADER] Just started the Medusa application!` when the Medusa application starts.
### Test Out the Loader
To test out your loader, start the Medusa application:
```bash npm2yarn
npm run dev
```
Youll see in the terminal the logged message, indicating that the loader function was executed successfully.
---
## Resolve Resources
When the loader function is created outside a module, it receives the Medusa container as a parameter. Use it to resolve other resources in your application, such as a modules service.
For example:
```ts title="src/loaders/hello-world.ts"
import { MedusaContainer } from "@medusajs/medusa"
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export default async function (container: MedusaContainer) {
const productModuleService: IProductModuleService =
container.resolve(ModuleRegistrationName.PRODUCT)
const [, count] = await productModuleService.listAndCount()
console.log(`Hello! You have ${count} product(s)`)
}
```
In the loader you resolve the `IProductModuleService`, then use its `listAndCount` method to log in the terminal the number of products in the store.
---
## Medusa Configuration Parameter
When the loader function is created outside a module, it receives the Medusa configurations defined in `medusa-config.js` as a second parameter.
For example:
```ts title="src/loaders/hello-world.ts"
import { MedusaContainer } from "@medusajs/medusa"
import { ConfigModule } from "@medusajs/types"
export default async function (
container: MedusaContainer,
config: ConfigModule
) {
console.log(`You have ${
Object.values(config.modules || {}).length
} modules!`)
}
```
This loader logs on application start-up the number of modules defined in your Medusa configurations.