90 lines
2.7 KiB
Plaintext
90 lines
2.7 KiB
Plaintext
export const metadata = {
|
||
title: `${pageNumber} Loaders Outside Modules`,
|
||
}
|
||
|
||
# {metadata.title}
|
||
|
||
In this chapter, you’ll 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 you’re performing a task on application start-up, but don’t 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
|
||
```
|
||
|
||
You’ll 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 module’s 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. |