A progressive Node.js framework for building efficient and scalable server-side applications.
This is a RESTful API for managing products and their variants. It allows you to create, read, update, and delete products and variants, as well as search for them by keywords. It also generates unique SKUs for each variant based on the product name and the variant attributes.
I chose NestJS as the framework for building this API because of its advantages such as:
- It uses TypeScript, which provides type safety, code completion, and better developer experience.
- It follows the Model-View-Controller (MVC) pattern, which helps to organize the code and separate the concerns of different components.
- It is scalable, testable, and maintainable, thanks to its modular structure and dependency injection system.
- It supports various features and tools out of the box, such as validation, authentication, authorization, logging, configuration, etc.
- It is platform-agnostic, which means it can work with any Node HTTP framework, such as Express or Fastify.
The application leverages the power of NestJS's design patterns and built-in modules to provide a robust and well-structured back end. It also incorporates Pactum for end-to-end (E2E) testing, ensuring that your application functions correctly from the user's perspective.
NestJS: The application is built with NestJS, a Node.js framework that simplifies server-side development by providing a scalable, extensible, and maintainable architecture.
Dependency Injection: Follows the dependency injection pattern, a core concept of NestJS, to enhance code modularity, improve testability, and make your codebase more maintainable.
Pactum E2E Testing: Utilizes Pactum for end-to-end (E2E) testing. Pactum is a versatile testing library that allows you to write concise and expressive E2E tests to ensure the reliability of your application.
Modularity: NestJS's module system encourages code modularity, allowing you to organize your application into manageable components and promote reusability.
To run the project, you need to have the following requirements:
- Node.js (version >= 16)
- Docker (version >= 20)
- Yarn (version >= 1)
- Prisma CLI (version >= 3)
You also need to clone the project from GitHub and install the dependencies:
$ git clone https://github.com/gosLp/mirrar-ecom-api.git
$ cd mirrar-ecom-api
$ yarn install
set up the environment variables for the database connection using the example .env file and .env.test for test db build the postgres DB based on the docker compose file to reset the db and applying all the migrations use the yarn db:dev:restart
$ docker-compose build
$ docker-compose up
$ yarn db:dev:restart
other commands for DB
# Remove the existing database container if any
$ yarn db:dev:rm
# Start a new database container in the background
$ yarn db:dev:up
# Wait for the database to be ready and run the migrations
$ yarn db:dev:restart
# development
$ yarn run start
# watch mode
$ yarn run start:dev
# production mode
$ yarn run start:prod
This will run the application in development mode, with hot reload and debugging enabled. You can access the API at http://localhost:3333.
To test the project, you need to have the same requirements as above, plus the following:
Jest (version >= 27) Pactum (version >= 3) Set up the environment variables for the test database connection. Copy the .env file and rename it to .env.test, and fill in the values.
Start the test database using Docker:
# Remove the existing test database container if any
$ yarn db:test:rm
# Start a new test database container in the background
$ yarn db:test:up
# Wait for the test database to be ready and run the migrations
$ yarn db:test:restart
finally run the tests:
$ yarn test:e2e
The API exposes the following endpoints for managing products and variants:
- GET /product: Get all products and their variants
- GET /product/:id: Get a product and its variants by id
- GET /product/search?q=: Search for products and variants by keyword
- POST /product: Create a new product and its variants
- PATCH /product/:id: Update a product and its variants by id
- DELETE /product/:id: Delete a product and its variants by id
look at the route collection on postman here
DATABASE_URL='postgresql://postgres:123@localhost:5434/nest?schema=public'
store these in an .env file at the root of the project
for the testing environment use the yarn db:test:restart to reset test db environment
End to end testing has been implemnted to test functionality of the following:
- Get Product
- Create Products
- Search Products
- Update Products
Here are some of the design choices that I made for this project and the reasons behind them:
- I used Prisma as the ORM for interacting with the PostgreSQL database, because it provides a type-safe and declarative way to model and query the data. It also supports migrations, seeding, and introspection, which makes it easy to manage the database schema and data.
- I used a UUID as the primary key for the product and variant models, because it provides a unique and random identifier that avoids collisions and exposes less information than a sequential id.
- I used a custom function to generate the SKU for each variant, based on the product name and the variant attributes. The function concatenates the product name, the variant size, the variant color, and the variant additional cost, and converts them to uppercase. For example, the SKU for a T-shirt with size large, color blue, and additional cost 0 is TSHIRT-L-BLUE-0. This makes the SKU easy to read and understand, and also avoids duplicates.
- I used NestJS as the framework for building the API