Files
medusa-store/www/apps/docs/content/experimental/pricing/install-nodejs.md
Shahed Nasser bb87db8342 docs: prep for v2 documentation (#6710)
This PR includes documentation that preps for v2 docs (but doesn't introduce new docs).

_Note: The number of file changes in the PR is due to find-and-replace within the `references` which is unavoidable. Let me know if I should move it to another PR._

## Changes

- Change Medusa version in base OAS used for v2.
- Fix to docblock generator related to not catching all path parameters.
- Added typedoc plugin that generates ER Diagrams, which will be used specifically for data model references in commerce modules.
- Changed OAS tool to output references in `www/apps/api-reference/specs-v2` directory when the `--v2` option is used.
- Added a version switcher to the API reference to switch between V1 and V2. This switcher is enabled by an environment variable, so it won't be visible/usable at the moment.
- Upgraded docusaurus to v3.0.1
- Added new Vale rules to ensure correct spelling of Medusa Admin and module names.
- Added new components to the `docs-ui` package that will be used in future documentation changes.
2024-03-18 07:47:35 +00:00

4.7 KiB
Raw Blame History

Install in Node.js-Based Application

In this document, youll learn how to setup and use the Pricing Module in a Node.js based application.

Prerequisites

Before installing the Pricing Module in your application, make sure you have the following prerequisites:

  • Node.js v16 or greater
  • PostgreSQL database. You can use an existing Medusa database, or set up a new PostgreSQL database.

Install Package

In your Node.js-based applications, such as a Next.js application, you can install the Pricing Module with the following command:

npm install @medusajs/pricing

Add Database Configuration

Add the following environment variable to your application:

POSTGRES_URL=<DATABASE_URL>

Where <DATABASE_URL> is your database connection URL of the format postgres://[user][:password]@[host][:port]/[dbname]. You can learn more about the connection URL format in this guide.

You can also set the following optional environment variables:

  • POSTGRES_SCHEMA: a string indicating the PostgreSQL schema to use. By default, it's public.
  • POSTGRES_DRIVER_OPTIONS: a stringified JSON object indicating the PostgreSQL options to use. The JSON object is then parsed to be used as a JavaScript object. By default, it's {"connection":{"ssl":false}} for local PostgreSQL databases, and {"connection":{"ssl":{"rejectUnauthorized":false}}} for remote databases.

:::note

If POSTGRES_DRIVER_OPTIONS is not specified, the PostgreSQL database is considered local if POSTGRES_URL includes localhost. Otherwise, it's considered remote.

:::

Run Database Migrations

:::note

You can skip this step if you use an existing Medusa database.

:::

Migrations are used to create your database schema. Before you can run migrations, add in your package.json the following scripts:

"scripts": {
  //...other scripts
  "price:migrations:run": "medusa-pricing-migrations-up",
  "price:seed": "medusa-pricing-seed ./pricing-seed-data.js"
},

The first command runs the migrations, and the second command allows you to seed your database with demo prices optionally.

However, youd need the following seed file added to the root of your project directory:

Seed file
const currenciesData = [
  {
    code: "USD",
    symbol: "$",
    symbol_native: "$",
    name: "US Dollar",
  },
  {
    code: "CAD",
    symbol: "CA$",
    symbol_native: "$",
    name: "Canadian Dollar",
  },
  {
    code: "EUR",
    symbol: "€",
    symbol_native: "€",
    name: "Euro",
  },
]

const moneyAmountsData = [
  {
    id: "money-amount-USD",
    currency_code: "USD",
    amount: 500,
    min_quantity: 1,
    max_quantity: 10,
  },
  {
    id: "money-amount-EUR",
    currency_code: "EUR",
    amount: 400,
    min_quantity: 1,
    max_quantity: 5,
  },
  {
    id: "money-amount-CAD",
    currency_code: "CAD",
    amount: 600,
    min_quantity: 1,
    max_quantity: 8,
  },
]

const priceSetsData = [
  {
    id: "price-set-USD",
  },
  {
    id: "price-set-EUR",
  },
  {
    id: "price-set-CAD",
  },
]

const priceSetMoneyAmountsData = [
  {
    title: "USD Price Set",
    price_set: "price-set-USD",
    money_amount: "money-amount-USD",
  },
  {
    title: "EUR Price Set",
    price_set: "price-set-EUR",
    money_amount: "money-amount-EUR",
  },
]

module.exports = {
  currenciesData,
  moneyAmountsData,
  priceSetsData,
  priceSetMoneyAmountsData,
}    

Then run the commands you added to migrate the database schema and optionally seed data:

npm run price:migrations:run
# optionally
npm run price:seed

Next.js Application: Adjust Configurations

The Pricing Module uses dependencies that arent Webpack optimized. Since Next.js uses Webpack for compilation, you need to add the Pricing Module as an external dependency.

To do that, add the serverComponentsExternalPackages option in next.config.js:

/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    serverComponentsExternalPackages: [
      "@medusajs/pricing",
    ],
  },
}

module.exports = nextConfig


Start Development

You can refer to the Example Usages documentation page for examples of using the Pricing Module.

You can also refer to the Module Interface Reference for a detailed reference on all available methods.