docs: added loaders documentation (#4019)

* docs: added loaders documentation

* added a link to the loaders documentation
This commit is contained in:
Shahed Nasser
2023-05-04 18:55:50 +03:00
committed by GitHub
parent 7fd22ecb4d
commit e046aa17db
17 changed files with 254 additions and 76 deletions

View File

@@ -0,0 +1,86 @@
---
description: 'Learn how to create a loader in Medusa. A loader can be created in the Medusa backend codebase, in a plugin, or in a module.'
addHowToData: true
---
# How to Create a Loader
In this document, youll learn how to create a loader in Medusa. A loader can be created in the Medusa backend codebase, in a plugin, or in a module.
## Step 1: Create Loader File
Create a TypeScript or JavaScript file in the `src/loaders` directory that will hold your custom script. There are no restrictions on the name of the file.
For example, create the file `src/loaders/my-loader.ts` that will hold the loader.
---
## Step 2: Define the Loader
The loader file must export a function.
### Parameters of Loaders in Medusa Backend and Plugins
When the loader is defined in the Medusa backend or a plugin, the function receives the following parameters:
1. `container`: the first parameter, which is a `AwilixContainer` object. You can use the container to register custom resources into the dependency container or resolve resources from the dependency container.
2. `config`: the second parameter, which is an object that holds the loaders plugin options. If the loader is not created inside a plugin, the config object will be empty.
### Parameters of Loaders in Modules
When the loader is defined in a module, it receives the following parameters:
1. `container`: the first parameter, which is a `AwilixContainer` object. You can use the container to register custom resources into the dependency container or resolve resources from the dependency container.
2. `logger`: the second parameter, which is a `Logger` object. The logger can be used to log messages in the console.
3. `config`: the third parameter, which is an object that holds the loaders module options.
### Example Implementation
For example, this loader function resolves the `ProductService` and logs in the console the count of products in the Medusa backend:
```ts title=src/loaders/my-loader.ts
import { ProductService } from "@medusajs/medusa"
import { AwilixContainer } from "awilix"
export default async (
container: AwilixContainer,
config: Record<string, unknown>
): Promise<void> => {
console.info("Starting loader...")
const productService = container.resolve<ProductService>(
"productService"
)
console.info(`Products count: ${
await productService.count()
}`)
console.info("Ending loader...")
}
```
---
## Step 3: Run Build Command
In the directory of your project, run the following command to transpile the files from the `src` to the `dist` directory:
```bash npm2yarn
npm run build
```
---
## Test it Out
:::note
This section explains how to test out the loader if its created in the Medusa backend codebase. If youre creating your loader in a plugin, you can learn how to test it in the [plugins documentation](../plugins/create.md#test-your-plugin). Alternatively, if youre creating your loader in a module, you can learn how to test it in the [modules documentation](../modules/create.mdx#step-4-test-your-module).
:::
Run the following command to start the Medusa backend:
```bash npm2yarn
npm run start
```
Your loader script should run on the Medusa backend startup. If you logged a message in the console, similar to the example above, you should see it in the console.

View File

@@ -0,0 +1,69 @@
---
description: "earn what loaders are in Medusa. A loader is a script that runs when the Medusa backend starts."
---
import DocCard from '@theme/DocCard';
import Icons from '@theme/Icon';
# Loaders
In this document, youll learn what loaders are in Medusa.
## Overview
A loader is a script that runs when the Medusa backend starts. The Medusa backend uses loaders to initialize the database connection, load plugins, register resources in the dependency container, and more.
Loaders can be created within the Medusa backend codebase, in a plugin, or in a module to perform custom actions when the backend starts. The loader is created in a TypeScript or JavaScript file located in the `src/loaders` directory, then transpiled using the `build` command into the `dist/loaders` directory.
### Loader Examples
For example, the Redis Event Bus module uses a loader to establish a connection with Redis and log a message in the console.
Another example is the Algolia plugin, which uses a loader to update the index settings when the Medusa backend starts based on the plugins options.
Loaders can be used to access the dependency container and register custom resources in it.
Loaders can also be used to create [scheduled jobs](../scheduled-jobs/overview.mdx) that run at a specified interval of time. For example:
```ts
const publishJob = async (container, options) => {
const jobSchedulerService =
container.resolve("jobSchedulerService")
jobSchedulerService.create(
"publish-products",
{},
"0 0 * * *",
async () => {
// job to execute
const productService = container.resolve("productService")
const draftProducts = await productService.list({
status: "draft",
})
for (const product of draftProducts) {
await productService.update(product.id, {
status: "published",
})
}
}
)
}
export default publishJob
```
---
## Custom Development
Developers can create a loader with the desired functionality directly within the Medusa Core, in a plugin, or in a module.
<DocCard item={{
type: 'link',
href: '/development/loaders/create',
label: 'Create a Loader',
customProps: {
icon: Icons['academic-cap-solid'],
description: 'Learn how to create a loader.'
}
}} />