This project is a simple store API built using Fastify with TypeScript, Prisma for database interactions, and MySQL as the database. It includes functionality for managing categories and products, with image resizing during upload. The project is Dockerized.
- Node.js
- Fastify
- npm
- MySQL server
- Prisma
- Docker
-
Clone the repository:
git clone https://github.com/yourusername/store-api.git cd store-api
-
Install dependencies:
npm install
-
Configure the database connection in the .env file:
- change username, password and database_name
- keep mysql_db "it's the same host we running in docker container"
- ADDRESS keep host_address it's the same host running in docker container
DATABASE_URL="mysql://username:password@mysql_db:3306/database_name" PORT=5000 NODE_ENV=development IMAGES_SERVER=http://localhost:5000 ADDRESS=host_address
-
Run server in docker:
- to run the server you only need to setup .env file and run this command.
docker compose up --build -d
-
To Run Project in local - setup .env file to
DATABASE_URL="mysql://username:password@localhost:3306/database_name" PORT=5000 NODE_ENV=development IMAGES_SERVER=http://localhost:5000 ADDRESS=localhost
-
After setup env files Run this command
- run this command to migrate database and run project in development
npm run migrate:dev
-
Run prettier:
- to run prettier for formatting the code run
npm run prettier
-
Run Eslint:
npm run lint
- id (Primary Key): Unique identifier for the category.
- name: Name of the category.
- picture: Picture associated with the category.
- parent_id: Identifier of the parent category, if applicable.
- created_at: Timestamp indicating when the category was created.
- updated_at: Timestamp indicating when the category was last updated.
- id (Primary Key): Unique identifier for the product.
- name: Name of the product.
- picture: Picture associated with the product.
- category_id (Foreign Key): Identifier referencing the Categories table.
- created_at: Timestamp indicating when the product was created.
- updated_at: Timestamp indicating when the product was last updated.
- GET /api/categories: Find all Categories
- GET /api/categories/:categoryId: Find a specific category by ID.
- GET /api/categories/tree: setup all categories as a tree, if you want to logic the tree build by
- POST /api/categories: Create a new category.
- PUT /api/categories/:categoryId: Update an existing category by ID.
- DELETE /api/categories/:categoryId: Delete a category by ID.
- GET /api/products: Find all products.
- GET /api/products/:productId: Find a specific product by ID.
- POST /api/products: Create a new product.
- PUT /api/products/:productId: Update an existing product by ID.
- DELETE /api/products/:productId: Delete a product by ID.
The project follows the structure below:
- controllers: Contains the controller logic for handling HTTP requests.
- routes: Defines the API routes.
- services: Business logic and service layer.
- pisma database schema
store-api/
|-- src/
| |-- config/
| |-- config..ts
| |-- database.ts // user for connection prisma @client
| |-- controllers/
| |-- category.controller.ts
| |-- product.controller.ts
| |-- interfaces/
| |-- categories/
| |-- category.interface.ts
| |-- category.route-opts.ts
| |-- products/
| |-- product.interface.ts
| |-- product.route-opts.ts
| |-- config.interface.ts
| |-- middlewares/
| |-- errorHandle.ts
| |-- routes/
| |-- category/
| |-- index.routes.ts
| |-- product/
| |-- index.routes.ts
| |-- services/
| |-- category.service.ts
| |-- product.service.ts
| |-- utils/
| |-- createFolders.ts
| |-- enums.ts
| |-- errors.ts
| |-- uploadFile.ts
| |-- app.ts
|-- .env.example
|-- .eslintrc.json
|-- .prettierrc
|-- docker-compose.yml
|-- Dockerfile
|-- package.json
|-- README.md
|-- tree.drawio
|-- tree.png
|-- tsconfig.json
- Create Instance in server
- create certificate .pem file
- Setup network configuration
- login to server with certificate
- install docker in machine
- install docker-compose in machine
- setup github repository
- in instatance git clone from repository
- setup environment variable
- run docker-compose up --build