* initialized next.js project * finished markdown sections * added operation schema component * change page metadata * eslint fixes * fixes related to deployment * added response schema * resolve max stack issue * support for different property types * added support for property types * added loading for components * added more loading * type fixes * added oneOf type * removed console * fix replace with push * refactored everything * use static content for description * fixes and improvements * added code examples section * fix path name * optimizations * fixed tag navigation * add support for admin and store references * general enhancements * optimizations and fixes * fixes and enhancements * added search bar * loading enhancements * added loading * added code blocks * added margin top * add empty response text * fixed oneOf parameters * added path and query parameters * general fixes * added base path env variable * small fix for arrays * enhancements * design enhancements * general enhancements * fix isRequired * added enum values * enhancements * general fixes * general fixes * changed oas generation script * additions to the introduction section * added copy button for code + other enhancements * fix response code block * fix metadata * formatted store introduction * move sidebar logic to Tags component * added test env variables * fix code block bug * added loading animation * added expand param + loading * enhance operation loading * made responsive + improvements * added loading provider * fixed loading * adjustments for small devices * added sidebar label for endpoints * added feedback component * fixed analytics * general fixes * listen to scroll for other headings * added sample env file * update api ref files + support new fields * fix for external docs link * added new sections * fix last item in sidebar not showing * move docs content to www/docs * change redirect url * revert change * resolve build errors * configure rewrites * changed to environment variable url * revert changing environment variable name * add environment variable for API path * fix links * fix tailwind settings * remove vercel file * reconfigured api route * move api page under api * fix page metadata * fix external link in navigation bar * update api spec * updated api specs * fixed google lint error * add max-height on request samples * add padding before loading * fix for one of name * fix undefined types * general fixes * remove response schema example * redesigned navigation bar * redesigned sidebar * fixed up paddings * added feedback component + report issue * fixed up typography, padding, and general styling * redesigned code blocks * optimization * added error timeout * fixes * added indexing with algolia + fixes * fix errors with algolia script * redesign operation sections * fix heading scroll * design fixes * fix padding * fix padding + scroll issues * fix scroll issues * improve scroll performance * fixes for safari * optimization and fixes * fixes to docs + details animation * padding fixes for code block * added tab animation * fixed incorrect link * added selection styling * fix lint errors * redesigned details component * added detailed feedback form * api reference fixes * fix tabs * upgrade + fixes * updated documentation links * optimizations to sidebar items * fix spacing in sidebar item * optimizations and fixes * fix endpoint path styling * remove margin * final fixes * change margin on small devices * generated OAS * fixes for mobile * added feedback modal * optimize dark mode button * fixed color mode useeffect * minimize dom size * use new style system * radius and spacing design system * design fixes * fix eslint errors * added meta files * change cron schedule * fix docusaurus configurations * added operating system to feedback data * change content directory name * fixes to contribution guidelines * revert renaming content * added api-reference to documentation workflow * fixes for search * added dark mode + fixes * oas fixes * handle bugs * added code examples for clients * changed tooltip text * change authentication to card * change page title based on selected section * redesigned mobile navbar * fix icon colors * fix key colors * fix medusa-js installation command * change external regex in algolia * change changeset * fix padding on mobile * fix hydration error * update depedencies
5.1 KiB
description, addHowToData
| description | addHowToData |
|---|---|
| Learn how to customize the import strategy in Medusa. The import strategy can be used to import entities such as products, prices in a price list, orders, or other entities. | true |
How to Customize Import Strategy
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 override or create your own strategy.
Although this documentation specifically targets import strategies, you can use the same steps to override any batch job strategy in Medusa, including export strategies.
Prerequisites
Medusa Components
It's assumed that you already have a Medusa backend installed and set up. If not, you can follow our quickstart guide to get started. The Medusa backend must also have an event bus module installed, which is available when using the default Medusa backend starter.
Override 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.
1. Create a File
You must store batch job strategies in the src/strategies directory of your Medusa backend. They are either TypeScript or JavaScript files.
So, for example, you can create the file src/strategies/import.ts.
2. Create a Class
The batch job strategy class must extend the AbstractBatchJobStrategy class which you can import from Medusa’s core repository.
For example, you can define the following class in the file you created:
import {
AbstractBatchJobStrategy,
BatchJobService,
} from "@medusajs/medusa"
import { EntityManager } from "typeorm"
class MyImportStrategy extends AbstractBatchJobStrategy {
protected batchJobService_: BatchJobService
protected manager_: EntityManager
protected transactionManager_: EntityManager
processJob(batchJobId: string): Promise<void> {
throw new Error("Method not implemented.")
}
buildTemplate(): Promise<string> {
throw new Error("Method not implemented.")
}
}
export default MyImportStrategy
:::note
This is the base implementation of a batch job strategy. You can learn about all the different methods and properties in this documentation.
:::
3. Set the batchType Property
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 override Medusa’s default batch job strategies by using the same batchType value in your custom strategy.
So, for example, to override the product import strategy set the batchType property in your strategy to product-import:
class MyImportStrategy extends AbstractBatchJobStrategy {
static batchType = "product-import"
// ...
}
4. Define your Custom Functionality
You can now define your custom functionality in your batch job strategy. For example, you can create custom import logic to import products.
Refer to the Create a Batch Job documentation to understand what properties and methods are required in your batch job strategy and how you can use them to implement your custom functionality.
5. Run Build Command
Before you can test out your batch job strategy, you must run the build command:
npm run build
6. Test your Functionality
Since you didn’t create a new batch job type and overwrote the functionality of the strategy, you can test out your functionality using the same steps used with the default strategy.
Specifically, since you create batch jobs using the Create Batch Job endpoint which accepts the batch job type as a body parameter, you just need to send the same type you used for this field. In the example of this documentation, the type would be product-import.
If you overwrote the import functionality, you can follow these steps to learn how to import products using the Admin APIs.
Create Custom Batch Job Strategy
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.
For more details on creating custom batch job strategies, please check out the Create Batch Job Strategy documentation.