A robust backend-heavy API built with Node.js, Express, and MongoDB to track and manage products in a warehouse. This project features full CRUD functionality for products and intelligent inventory logic to handle stock updates safely and efficiently.
Product Management: Full CRUD (Create, Read, Update, Delete) endpoints for products.
- Atomic Stock Operations: Safely increase or decrease product stock with logic that prevents race conditions and ensures data integrity.
- Inventory Validation: Business logic to prevent stock quantities from ever going below zero.
- Low Stock Alerts: An endpoint to quickly identify all products that have fallen below their specified low-stock threshold.
- Unit & Integration Testing: A complete test suite using Jest and an in-memory database to validate the API's business logic.
- Backend: Node.js, Express.js
- Database: MongoDB with Mongoose ODM
- Testing: Jest, Supertest, MongoDB-in-memory-server
Follow these instructions to get the project set up and running on your local machine.
Make sure you have the following installed:
- Node.js (v14 or newer)
- npm
- MongoDB (for local development database)
Clone the repository:
git clone <your-repository-link>
cd <repository-name>Install dependencies:
npm installSet up environment variables: Create a file named .env in the root of the project and add the following variables. Replace the placeholder with your own MongoDB connection string.
MONGO_URI="mongodb://localhost:27017/inventoryDB"
PORT=5000
To start the server, run the following command:
npm startThe API will be available at http://localhost:5000.
To run the automated tests, use the following command. The tests use an in-memory MongoDB instance and do not require a separate database connection.
npm testAll endpoints are prefixed with /api
| Method | Endpoint | Description |
|---|---|---|
| POST | /products | Creates a new product. |
| GET | /products | Retrieves a list of all products. |
| GET | /products/stock/low | Retrieves products below their stock threshold. |
| GET | /products/:id | Retrieves a single product by its ID. |
| PATCH | /products/:id | Updates a product's details. |
| DELETE | /products/:id | Deletes a product by its ID. |
| PATCH | /products/:id/increase-stock | Increases the stock of a specific product. |
| PATCH | /products/:id/decrease-stock | Decreases the stock of a specific product. |
- RESTful API Design: The API is structured following REST principles, with endpoints grouped by the "product" resource. Actions like increasing stock are handled as sub-resources (/products/:id/increase-stock) for clarity.
- Atomic Operations: To prevent race conditions during concurrent stock updates, the system uses atomic MongoDB operations ($inc, findOneAndUpdate with query conditions) instead of a "read-then-write" pattern. This ensures that the database is the single source of truth and maintains data integrity.
- Data Validation: Business rules, such as preventing negative stock, are enforced at the database schema level using Mongoose validators. This provides a robust and centralized layer of data validation.
- Isolated Testing Environment: Tests are designed to be self-contained and reliable by using an in-memory MongoDB server. This ensures that running tests does not affect any development data and provides a fresh, predictable state for each test run.