This project demonstrates a system for synchronizing product data between a Directus instance (acting as a PIM or data source) and a MedusaJS e-commerce platform. It also includes a Next.js storefront powered by the Medusa backend.
The primary goal of this project is to maintain product consistency between an external product information management system (Directus) and an e-commerce backend (Medusa). Changes made to products in Directus (creations, updates, deletions) are reflected in Medusa via a synchronization script. The Medusa instance then serves a Next.js storefront, allowing customers to browse and purchase these products.
The project is structured into three main parts:
-
product-sync(Medusa Backend)- The core e-commerce platform built with MedusaJS (compatible with Medusa v2+).
- Handles product management, orders, customers, and other e-commerce functionalities.
- For more details, see the
product-sync/README.md.
-
product-sync-storefront(Next.js Storefront)- A modern, performant e-commerce storefront built with Next.js 15.
- Consumes data from the
product-syncMedusa backend. - For more details, see the
product-sync-storefront/README.md.
-
sync-product.js(Synchronization Script)- A Node.js script responsible for synchronizing product data from Directus to Medusa.
- It connects to both Directus and Medusa admin APIs to:
- Fetch products from Directus.
- Create new products in Medusa if they don't exist.
- Update existing products in Medusa if changes are detected in Directus.
- Delete products from Medusa if they are no longer present in Directus.
- Handles image uploads from Directus to Medusa.
- Node.js (>= v20, as specified in
product-sync/package.json) - Yarn (for
product-sync-storefrontand potentiallyproduct-syncif preferred for consistency) - NPM (for root project dependencies)
- A running Directus instance (e.g., via Docker). You can use a command similar to the one in
medusa.txtor refer to the Directus documentation.- Directus should have a "products" collection with fields like
name,description,price, andimages.
- Directus should have a "products" collection with fields like
- A running PostgreSQL database for the Medusa backend.
- Ensure your Directus instance is running and accessible (default
http://localhost:8055). - Configure a "products" collection with the necessary fields (e.g.,
name,description,price,images). - Obtain admin credentials (email and password) for Directus.
- Navigate to the
product-syncdirectory:cd product-sync - Install dependencies:
yarn install(ornpm install) - Configure environment variables. Create a
.envfile in theproduct-syncdirectory. Key variables include:DATABASE_URL=your_postgresql_connection_string JWT_SECRET=supersecretjwt COOKIE_SECRET=supersecretcookie STORE_CORS=http://localhost:8000 ADMIN_CORS=http://localhost:7001 # Ensure other Medusa-specific variables are set as needed.
- Run database migrations:
npx medusa migrations run(oryarn medusa migrations run) - (Optional) Seed data:
yarn seed(ornpx medusa exec ./src/scripts/seed.ts) - Start the Medusa backend:
yarn dev(runs onhttp://localhost:9000by default) - Obtain Medusa admin credentials (email and password).
- Navigate to the
product-sync-storefrontdirectory:cd product-sync-storefront - Install dependencies:
yarn install - Configure environment variables. As per its README, copy
.env.templateto.env.local:Ensuremv .env.template .env.local
NEXT_PUBLIC_MEDUSA_BACKEND_URLin.env.localpoints to your Medusa backend (e.g.,http://localhost:9000).NEXT_PUBLIC_MEDUSA_BACKEND_URL=http://localhost:9000 # Add NEXT_PUBLIC_STRIPE_KEY if you plan to use Stripe, as per its README # NEXT_PUBLIC_STRIPE_KEY=your-stripe-public-key
- Start the storefront:
yarn dev(runs onhttp://localhost:8000by default)
- Navigate to the root project directory (
product-synchronization). - Install root dependencies:
npm install - Create a
.envfile in the root directory (product-synchronization/.env) with the following credentials:DIRECTUS_EMAIL=your_directus_admin_email DIRECTUS_PASS=your_directus_admin_password MEDUSA_EMAIL=your_medusa_admin_email MEDUSA_PASS=your_medusa_admin_password
- Run the synchronization script:
node sync-product.js
- MedusaJS: Open-source composable commerce engine.
- Directus: Open-source data platform (used here as a Headless CMS/PIM).
- Next.js: React framework for building server-side rendered and static web applications.
- Node.js: JavaScript runtime environment.
- TypeScript: Superset of JavaScript that adds static typing.
- PostgreSQL: Database for Medusa.
product-synchronization/
├── .env # For sync-product.js credentials (GITIGNORED)
├── product-sync/ # Medusa backend
│ ├── .env # For Medusa backend config (GITIGNORED)
│ ├── src/
│ ├── package.json
│ └── README.md
├── product-sync-storefront/ # Next.js storefront
│ ├── .env.local # For Next.js storefront config (GITIGNORED)
│ ├── src/
│ ├── package.json
│ └── README.md
├── sync-product.js # Product synchronization script
├── medusa.txt # Notes on Medusa and Directus
├── package.json # Root dependencies for sync script
└── README.md # This file