* docs: migrate ui docs to docs universe * created yarn workspace * added eslint and tsconfig configurations * fix eslint configurations * fixed eslint configurations * shared tailwind configurations * added shared ui package * added more shared components * migrating more components * made details components shared * move InlineCode component * moved InputText * moved Loading component * Moved Modal component * moved Select components * Moved Tooltip component * moved Search components * moved ColorMode provider * Moved Notification components and providers * used icons package * use UI colors in api-reference * moved Navbar component * used Navbar and Search in UI docs * added Feedback to UI docs * general enhancements * fix color mode * added copy colors file from ui-preset * added features and enhancements to UI docs * move Sidebar component and provider * general fixes and preparations for deployment * update docusaurus version * adjusted versions * fix output directory * remove rootDirectory property * fix yarn.lock * moved code component * added vale for all docs MD and MDX * fix tests * fix vale error * fix deployment errors * change ignore commands * add output directory * fix docs test * general fixes * content fixes * fix announcement script * added changeset * fix vale checks * added nofilter option * fix vale error
69 lines
2.2 KiB
Plaintext
69 lines
2.2 KiB
Plaintext
---
|
||
description: "Learn 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, you’ll 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 plugin’s 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.'
|
||
}
|
||
}} /> |