* Add data migration script after running migrations * Update install-medusa.mdx * Update examples.md (#5751) * fix lint errors * fix comment in example * added examples for modules usage in Medusa --------- Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
159 lines
3.6 KiB
Plaintext
159 lines
3.6 KiB
Plaintext
import DocCard from '@theme/DocCard'
|
|
import Icons from '@theme/Icon'
|
|
import Tabs from '@theme/Tabs';
|
|
import TabItem from '@theme/TabItem';
|
|
|
|
# Install Pricing Module in Medusa
|
|
|
|
In this document, you'll learn how to install the Pricing module using NPM in the Medusa backend.
|
|
|
|
## Step 1: Install Module
|
|
|
|
To install the Pricing module, run the following command in the root directory of the Medusa backend:
|
|
|
|
```bash npm2yarn
|
|
npm install @medusajs/pricing
|
|
```
|
|
|
|
---
|
|
|
|
## Step 2: Add Module to Configurations
|
|
|
|
In `medusa-config.js`, add the pricing module to the exported object under the `modules` property:
|
|
|
|
```js title=medusa-config.js
|
|
module.exports = {
|
|
// ...
|
|
featureFlags: {
|
|
// ...
|
|
medusa_v2: true,
|
|
},
|
|
modules: {
|
|
// ...
|
|
pricingService: {
|
|
resolve: "@medusajs/pricing",
|
|
},
|
|
},
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## Step 3: Run Migrations
|
|
|
|
Run the following commands to reflect schema changes into your database:
|
|
|
|
```bash
|
|
npx medusa migrations run
|
|
node node_modules/@medusajs/medusa/dist/scripts/migrate-to-pricing-module.js
|
|
```
|
|
|
|
---
|
|
|
|
## Use the Module
|
|
|
|
You can now start using the module's `PricingModuleService` by resolving it through [dependency injection](../../development/fundamentals/dependency-injection.md).
|
|
|
|
For example:
|
|
|
|
<Tabs groupId="resource-type" isCodeTabs={true}>
|
|
<TabItem value="api-route" label="API Route" attributes={{
|
|
title: "src/api/store/custom/route.ts"
|
|
}} default>
|
|
|
|
```ts
|
|
import type {
|
|
MedusaRequest,
|
|
MedusaResponse
|
|
} from "@medusajs/medusa";
|
|
import {
|
|
PricingModuleService
|
|
} from "@medusajs/pricing"
|
|
|
|
export async function GET(
|
|
req: MedusaRequest,
|
|
res: MedusaResponse
|
|
) {
|
|
const pricingModuleService: PricingModuleService =
|
|
req.scope.resolve(
|
|
"pricingModuleService"
|
|
)
|
|
|
|
return res.json({
|
|
pricings: pricingModuleService.list()
|
|
})
|
|
}
|
|
```
|
|
|
|
</TabItem>
|
|
<TabItem value="subscribers" label="Subscribers" attributes={{
|
|
title: "src/subscribers/create-customer.ts"
|
|
}}>
|
|
|
|
```ts
|
|
import {
|
|
type SubscriberConfig,
|
|
type SubscriberArgs,
|
|
PricingService,
|
|
} from "@medusajs/medusa"
|
|
import {
|
|
PricingModuleService
|
|
} from "@medusajs/pricing"
|
|
|
|
export default async function handleListPriceSets({
|
|
data, eventName, container, pluginOptions
|
|
}: SubscriberArgs<Customer>) {
|
|
const pricingModuleService: PricingModuleService =
|
|
container.resolve(
|
|
"pricingModuleService"
|
|
)
|
|
|
|
console.log(await pricingModuleService.list())
|
|
}
|
|
|
|
export const config: SubscriberConfig = {
|
|
event: PricingService.Events.CREATED,
|
|
context: {
|
|
subscriberId: "list-pricings"
|
|
}
|
|
}
|
|
```
|
|
|
|
</TabItem>
|
|
<TabItem value="service" label="Service" attributes={{
|
|
title: "src/service/hello.ts"
|
|
}}>
|
|
|
|
```ts
|
|
import { TransactionBaseService } from "@medusajs/medusa"
|
|
import {
|
|
PricingModuleService
|
|
} from "@medusajs/pricing"
|
|
|
|
class HelloService extends TransactionBaseService {
|
|
private pricingModuleService: PricingModuleService
|
|
|
|
constructor(container) {
|
|
super(container)
|
|
this.pricingModuleService = container.pricingModuleService
|
|
}
|
|
|
|
await listPriceSets() {
|
|
return await this.pricingModuleService.list()
|
|
}
|
|
}
|
|
|
|
export default HelloService
|
|
```
|
|
|
|
</TabItem>
|
|
</Tabs>
|
|
|
|
---
|
|
|
|
## Start Development
|
|
|
|
You can refer to the [Example Usages documentation page](./examples.mdx) for examples of using the Pricing module.
|
|
|
|
You can also refer to the [Module Interface Reference](../../references/pricing/interfaces/IPricingModuleService.mdx) for a detailed reference on all available methods.
|