v1.3.0
Version 1.3.0 of Medusa introduces new features including the addition of Line Item Adjustments and a more advanced Promotions API, as well as a change in loading environment variables into your Medusa server. The changes do not affect the public APIs and require only running necessary data migrations.
Prerequisites
Both the actions required for this update need you to set the following environment variables:
TYPEORM_CONNECTION=postgres
TYPEORM_URL=<DATABASE_URL>
TYPEORM_LOGGING=true
TYPEORM_ENTITIES=./node_modules/@medusajs/medusa/dist/models/*.js
TYPEORM_MIGRATIONS=./node_modules/@medusajs/medusa/dist/migrations/*.js
These environment variables are used in the data migration scripts in this upgrade. Make sure to replace <DATABASE_URL>
with your PostgreSQL database URL.
Environment Variables
In previous versions of Medusa, The server automatically loads all environment variables in the .env
file at the root of the Medusa server.
This new update removes loading it automatically and gives developers the freedom in how to load their environment variables. All environment variables will be loaded by default from the system’s environment variables.
Actions Required
If you use a .env
file to load environment variables on your server, you need to load the variables manually in medusa-config.js
.
You can add the following code snippet at the top of the file which uses the dotenv package to load the environment variables based on the current Node environment:
const dotenv = require("dotenv")
let ENV_FILE_NAME = ""
switch (process.env.NODE_ENV) {
case "production":
ENV_FILE_NAME = ".env.production"
break
case "staging":
ENV_FILE_NAME = ".env.staging"
break
case "test":
ENV_FILE_NAME = ".env.test"
break
case "development":
default:
ENV_FILE_NAME = ".env"
break
}
try {
dotenv.config({ path: process.cwd() + "/" + ENV_FILE_NAME })
} catch (e) {
// handle error
}
Line Item Adjustments
This new version of Medusa allows store operators to adjust line items in an order or a swap which provides more customization capabilities.
It introduces a new entity LineItemAdjustment
which gives more flexibility to adjust the pricing of line items in a cart, order, or swap. A discount can be added, removed, or modified and the price will reflect on the total calculation of the cart, order, or swap.
This also introduces an optimization to the calculation of totals, as it is no longer necessary to calculate the discounts every time the totals are retrieved.
Actions Required
This new version adds a new data migration script that will go through your list of existing orders and add line item adjustments for each of the line items in the order.
For that reason, it’s essential to run the data migration script after upgrading your server and before starting your Medusa server:
node ./node_modules/@medusajs/medusa/dist/scripts/line-item-adjustment-migration.js
Advanced Discount Conditions
This new version of Medusa holds advanced promotions functionalities to provide store operators with even more customization capabilities when creating discounts. You can now add even more conditions to your discounts to make them specific for a set of products, collections, customer groups, and more.
This change required creating a new entity DiscountCondition
which belongs to DiscountRule
and includes a few relationships with other entities to make the aforementioned feature possible.
Actions Required
To ensure your old discount rules play well with the new Promotions API and schema, this version includes a migration script that will go through your existing discount rules, create discount conditions for these rules, and move the former direct relationship between discount rules and products to become between discount conditions and products.
For that reason, it’s essential to run the data migration script after upgrading your server and before starting your Medusa server:
node ./node_modules/@medusajs/medusa/dist/scripts/discount-rule-migration.js