docs: added how to override strategy documentation (#4004)
This commit is contained in:
@@ -5,13 +5,13 @@ addHowToData: true
|
||||
|
||||
# How to Customize Import Strategy
|
||||
|
||||
In this document, you’ll learn how to create a custom product import strategy either by overwriting the default strategy or creating your own.
|
||||
In this document, you’ll learn how to create a custom product import strategy either by overriding the default strategy or creating your own.
|
||||
|
||||
## Overview
|
||||
|
||||
Product Import Strategy is essentially a batch job strategy. Medusa provides the necessary mechanisms to overwrite or create your own strategy.
|
||||
Product Import Strategy is essentially a batch job strategy. Medusa provides the necessary mechanisms to override or create your own strategy.
|
||||
|
||||
Although this documentation specifically targets import strategies, you can use the same steps to overwrite any batch job strategy in Medusa, including export strategies.
|
||||
Although this documentation specifically targets import strategies, you can use the same steps to override any batch job strategy in Medusa, including export strategies.
|
||||
|
||||
---
|
||||
|
||||
@@ -27,9 +27,9 @@ If you use SQLite during your development, it’s highly recommended that you us
|
||||
|
||||
---
|
||||
|
||||
## Overwrite Batch Job Strategy
|
||||
## Override Batch Job Strategy
|
||||
|
||||
The steps required for overwriting a batch job strategy are essentially the same steps required to create a batch job strategy with a minor difference. For that reason, this documentation does not cover the basics of a batch job strategy.
|
||||
The steps required for overriding a batch job strategy are essentially the same steps required to create a batch job strategy with a minor difference. For that reason, this documentation does not cover the basics of a batch job strategy.
|
||||
|
||||
If you’re interested to learn more about batch job strategies and how they work, please check out the [Create Batch Job Strategy documentation](./create.mdx).
|
||||
|
||||
@@ -78,9 +78,9 @@ This is the base implementation of a batch job strategy. You can learn about all
|
||||
|
||||
Every batch job strategy class must have the static property `batchType` defined. It determines the type of batch job this strategy handles.
|
||||
|
||||
Since only one batch job strategy can handle a batch job type, you can overwrite Medusa’s default batch job strategies by using the same `batchType` value in your custom strategy.
|
||||
Since only one batch job strategy can handle a batch job type, you can override Medusa’s default batch job strategies by using the same `batchType` value in your custom strategy.
|
||||
|
||||
So, for example, to overwrite the product import strategy set the `batchType` property in your strategy to `product-import`:
|
||||
So, for example, to override the product import strategy set the `batchType` property in your strategy to `product-import`:
|
||||
|
||||
```ts
|
||||
class MyImportStrategy extends AbstractBatchJobStrategy {
|
||||
@@ -115,7 +115,7 @@ If you overwrote the import functionality, you can follow [these steps to learn
|
||||
|
||||
## Create Custom Batch Job Strategy
|
||||
|
||||
If you don’t want to overwrite Medusa’s batch job strategy, you can create a custom batch job strategy with a different `batchType` value. Then, use that type when you send a request to [Create a Batch Job](/api/admin/#tag/Batch-Job).
|
||||
If you don’t want to override Medusa’s batch job strategy, you can create a custom batch job strategy with a different `batchType` value. Then, use that type when you send a request to [Create a Batch Job](/api/admin/#tag/Batch-Job).
|
||||
|
||||
For more details on creating custom batch job strategies, please check out the [Create Batch Job Strategy documentation](./create.mdx).
|
||||
|
||||
|
||||
81
docs/content/development/strategies/override-strategy.md
Normal file
81
docs/content/development/strategies/override-strategy.md
Normal file
@@ -0,0 +1,81 @@
|
||||
---
|
||||
description: "Learn what a Strategy is in Medusa. A strategy is an isolated piece of business logic that can be overridden and customized."
|
||||
addHowToData: true
|
||||
---
|
||||
|
||||
# How to Override a Strategy
|
||||
|
||||
In this document, you’ll learn how to override a strategy in a Medusa backend or plugin.
|
||||
|
||||
## Overview
|
||||
|
||||
The Medusa core defines and uses strategies for certain functionalities, which allows developers to override these functionalities and implement them as fits for their use case.
|
||||
|
||||
For example, the cart completion process is implemented within a `CartCompletionStrategy` that is used inside the Complete Cart endpoint. If you need to change the cart completion process, you can override the `CartCompletionStrategy` and implement your own strategy. The Medusa backend will then use your strategy instead of the one defined in the core.
|
||||
|
||||
### Hierarchy of Strategy Resolution
|
||||
|
||||
When the Medusa backend starts, the strategies defined in the core are registered in the dependency container first.
|
||||
|
||||
If a strategy is overridden in a plugin, it will be registered in the dependency container in place of the original strategy.
|
||||
|
||||
If a strategy is overridden in the Medusa backend codebase, it will be registered in the dependency container in place of the original strategy.
|
||||
|
||||
So, if a strategy is overridden within a plugin and in the Medusa backend codebase, the one in the codebase is registered and used instead of the one in the plugin.
|
||||
|
||||
:::tip
|
||||
|
||||
You can learn more about the dependency container and injection [here](../fundamentals/dependency-injection.md).
|
||||
|
||||
:::
|
||||
|
||||
### Existing Resources
|
||||
|
||||
This guide explains how to override a strategy in general. It doesn’t explain how to override specific strategies.
|
||||
|
||||
There are other resources that provide steps specific to a strategy type:
|
||||
|
||||
- [How to override the Cart Completion Strategy](../../modules/carts-and-checkout/backend/cart-completion-strategy.md)
|
||||
- [How to override the Tax Calculation Strategy](../../modules/taxes/backend/tax-calculation-strategy.md)
|
||||
- [How to override the Price Selection Strategy](../../modules/price-lists/backend/override-price-selection-strategy.md)
|
||||
- [How to override a Batch Job Strategy](../batch-jobs/customize-import.md)
|
||||
|
||||
---
|
||||
|
||||
## Step 1: Create Strategy Class
|
||||
|
||||
A strategy class is created in a TypeScript or JavaScript file in the `src/strategies` directory of your Medusa backend or plugin.
|
||||
|
||||
The class must extend or implement a strategy interface or class from within the core, depending on what strategy you’re overriding. For example, if you’re overriding the `CartCompletionStrategy`, you must extend the `AbstractCartCompletionStrategy`.
|
||||
|
||||
---
|
||||
|
||||
## Step 2: Implement Functionalities
|
||||
|
||||
Each strategy class is required to implement certain methods defined by the interface it implements or the abstract class it extends. Depending on what the strategy is used for, you must implement the functionalities in each of the required methods.
|
||||
|
||||
For example, if you’re overriding the `CartCompletionStrategy`, you must implement the `complete` method using the signature defined in `AbstractCartCompletionStrategy`.
|
||||
|
||||
---
|
||||
|
||||
## Step 3: Run Build Command
|
||||
|
||||
In the directory of the Medusa backend, run the `build` command to transpile the files in the `src` directory into the `dist` directory:
|
||||
|
||||
```bash npm2yarn
|
||||
npm run build
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Test it Out
|
||||
|
||||
Run your backend to test it out:
|
||||
|
||||
```bash npm2yarn
|
||||
npm run start
|
||||
```
|
||||
|
||||
You can test now whether your strategy is working by performing the actions that run your strategy.
|
||||
|
||||
For example, if you’re overriding the `CartCompletionStrategy`, you can simulate the cart completion flow and see if your strategy is being used.
|
||||
@@ -2,7 +2,7 @@
|
||||
description: "Learn what a Strategy is in Medusa. A strategy is an isolated piece of business logic that can be overridden and customized."
|
||||
---
|
||||
|
||||
import DocCardList from '@theme/DocCardList';
|
||||
import DocCard from '@theme/DocCard';
|
||||
import Icons from '@theme/Icon';
|
||||
|
||||
# Strategy
|
||||
@@ -40,29 +40,18 @@ export default async (req, res) => {
|
||||
}
|
||||
```
|
||||
|
||||
Developers can override these strategies defined in the core to customize their functionality. Dependency injection then resolves the strategy (in the code example above, `CartCompletionStrategy`) to the custom strategy that the developer created. Then, the method (in the code example above, `complete`) of the custom strategy will be executed instead of the one defined in the core package.
|
||||
When a strategy is overriden, dependency injection then resolves the strategy (in the code example above, `CartCompletionStrategy`) to the custom strategy that the developer created. Then, the method (in the code example above, `complete`) of the custom strategy will be executed instead of the one defined in the core package.
|
||||
|
||||
## Custom Development
|
||||
|
||||
<DocCardList colSize={6} items={[
|
||||
{
|
||||
type: 'link',
|
||||
href: '#',
|
||||
label: 'Backend: Create a Strategy',
|
||||
customProps: {
|
||||
icon: Icons['academic-cap-solid'],
|
||||
description: 'Learn how to create a strategy.',
|
||||
isSoon: true
|
||||
}
|
||||
},
|
||||
{
|
||||
type: 'link',
|
||||
href: '#',
|
||||
label: 'Backend: Override a Strategy',
|
||||
customProps: {
|
||||
icon: Icons['academic-cap-solid'],
|
||||
description: 'Learn how to override a strategy.',
|
||||
isSoon: true
|
||||
}
|
||||
},
|
||||
]} />
|
||||
Developers can override strategies defined in the core to customize their functionality within their Medusa backend codebase or within a plugin.
|
||||
|
||||
<DocCard item={{
|
||||
type: 'link',
|
||||
href: '/development/strategies/override-strategy',
|
||||
label: 'Backend: Override a Strategy',
|
||||
customProps: {
|
||||
icon: Icons['academic-cap-solid'],
|
||||
description: 'Learn how to override a strategy.',
|
||||
}
|
||||
}} />
|
||||
|
||||
Reference in New Issue
Block a user