diff --git a/docs/content/development/batch-jobs/index.mdx b/docs/content/development/batch-jobs/index.mdx index 1efeac73aa..06c45fce3f 100644 --- a/docs/content/development/batch-jobs/index.mdx +++ b/docs/content/development/batch-jobs/index.mdx @@ -71,7 +71,7 @@ If the batch job fails at any point in this flow, its status is changed to `fail ## Custom Development -Developers can create custom batch jobs in the Medusa backend, a plugin, or in a Commerce Module. Developers can also customize existing batch job strategies in the core, such as the import strategy. +Developers can create custom batch jobs in the Medusa backend, a plugin, or in a module. Developers can also customize existing batch job strategies in the core, such as the import strategy. +): Promise => { + console.info("Starting loader...") + const productService = container.resolve( + "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 it’s created in the Medusa backend codebase. If you’re 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 you’re 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. diff --git a/docs/content/development/loaders/overview.mdx b/docs/content/development/loaders/overview.mdx new file mode 100644 index 0000000000..39315818b4 --- /dev/null +++ b/docs/content/development/loaders/overview.mdx @@ -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, 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. + + \ No newline at end of file diff --git a/docs/content/development/modules/create.mdx b/docs/content/development/modules/create.mdx index 296743c8e5..63fc024e2d 100644 --- a/docs/content/development/modules/create.mdx +++ b/docs/content/development/modules/create.mdx @@ -139,7 +139,7 @@ All property types such as `ModuleLoaderFunction` can be loaded from the `@medus Where: - `service`: This is the only required property to be exported. It should be the main service your module exposes, and it must implement all the declared methods on the module interface. For example, if it's a cache module, it must implement the `ICacheService` interface exported from `@medusajs/types`. -- `loaders`: (optional) an array of functions used to perform an action while loading the module. For example, you can log a message that the module has been loaded, or if your module's scope is [isolated](#module-scope) you can use the loader to establish a database connection. +- `loaders`: (optional) an array of [loader](../loaders/overview.mdx) functions used to perform an action while loading the module. For example, you can log a message that the module has been loaded, or if your module's scope is [isolated](#module-scope) you can use the loader to establish a database connection. - `migrations`: (optional) an array of objects containing database migrations that should run when the `migration` command is used with Medusa's CLI. - `models`: (optional) an array of entities that your module creates. - `runMigrations`: (optional) a function that can be used to define migrations to run when the `migration run` command is used with Medusa's CLI. The migrations will only run if they haven't already. This will only be executed if the module's scope is [isolated](#module-scope). diff --git a/docs/content/development/modules/overview.mdx b/docs/content/development/modules/overview.mdx index 50b418752a..3e9bc2b334 100644 --- a/docs/content/development/modules/overview.mdx +++ b/docs/content/development/modules/overview.mdx @@ -13,7 +13,7 @@ In this document, you’ll learn what Modules are and how can you use them durin Modules are self-contained, reusable pieces of code that encapsulate specific functionality or features within an ecommerce application. They foster separation of concerns, maintainability, and reusability by organizing code into smaller, independent units that can be easily managed, tested, and integrated with other modules. -Modules further increase Medusa’s extensibility. Commerce modules, such as the cart engine, can be extended or entirely replaced with your own custom logic. They can also run independently of the core Medusa package, allowing you to utilize the commerce module within a larger commerce ecosystem. For example, you can use the Order module as an Order Management System (OMS) without using Medusa’s core. +Modules further increase Medusa’s extensibility. commerce modules, such as the cart engine, can be extended or entirely replaced with your own custom logic. They can also run independently of the core Medusa package, allowing you to utilize the commerce module within a larger commerce ecosystem. For example, you can use the Order module as an Order Management System (OMS) without using Medusa’s core. This also applies to core logic such as caching or events systems. You can use modules to integrate any logic or third-party service to handle this logic. This gives you greater flexibility in how you choose your tech stack. diff --git a/docs/content/development/notification/overview.mdx b/docs/content/development/notification/overview.mdx index 486551bf40..3e20f64c8c 100644 --- a/docs/content/development/notification/overview.mdx +++ b/docs/content/development/notification/overview.mdx @@ -95,7 +95,7 @@ An example of a flow that can be implemented using Medusa's Notification API is ## Custom Development -Developers can create custom notification providers in the Medusa backend, a plugin, or in a Commerce Module. +Developers can create custom notification providers in the Medusa backend, a plugin, or in a module.