Skip to content

gitmariosalazar/microservices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Donate us Support us Follow us on Twitter

๐Ÿงฟ Microservice using Clean Architecture & NestJS

Clone repository

$ git clone https://github.com/gitmariosalazar/microservices.git

Project setup

Gateway

$ cd microservices/backend/gateway
$ npm install

Products microservice

$ cd microservices/backend/products-ms
$ npm install

Authentication microservice

$ cd microservices/backend/authentication-ms
$ npm install

Sales microservice

$ cd microservices/backend/sales-ms
$ npm install

Returns microservice

$ cd microservices/backend/returns-ms
$ npm install

Run prisma

  • Run the following command:
$ cd microservices/backend/authentication-ms # Change the folder name for others microservices
$ npm run prisma:generate 

Compile and run the microservices and gateway

$ cd microservices/backend/gateway  # Change the folder name for others
# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run build
$ npm run start:prod

๐Ÿ›ข๏ธ Configure Database

  • Create database
mysql -u root -p
create database microservices
  • Restore database
$ mysql -u root -p <database name> < "<Path>\microservices\backup\microservices.sql"

๐Ÿ“‚ Project Structure for the microservices

๐Ÿ“‚products-ms/
โ”œโ”€โ”€๐Ÿ“‚src/
โ”‚   โ”œโ”€โ”€๐Ÿ“‚errors/               # Configuraciones para manejar errores
โ”‚   โ”œโ”€โ”€๐Ÿ“‚modules/
โ”‚   โ”‚   โ”œโ”€โ”€๐Ÿ“‚products/         # Mรณdulo de productos
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€๐Ÿ“‚application/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€๐Ÿ“‚domain/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€๐Ÿ“‚infrastructure/
โ”‚   โ”‚   โ””โ”€โ”€๐Ÿ“‚users/            # Agregar estructura similar al mรณdulo de productos
โ”‚   โ”‚       โ”œโ”€โ”€๐Ÿ“‚application/...
โ”‚   โ”‚       โ”œโ”€โ”€๐Ÿ“‚domain/...
โ”‚   โ”‚       โ””โ”€โ”€๐Ÿ“‚infrastructure/..
โ”‚   โ”œโ”€โ”€๐Ÿ“‚settings/
โ”‚   โ”‚   โ”œโ”€โ”€๐Ÿ“œenvs.ts
โ”‚   โ”‚   โ””โ”€โ”€๐Ÿ“œindex.ts
โ”‚   โ”œโ”€โ”€๐Ÿ“‚shared/
โ”‚   โ”‚    โ”œโ”€โ”€๐Ÿ“‚database/
โ”‚   โ”‚    โ”‚   โ”œโ”€โ”€๐Ÿ“œmysql.service.ts
โ”‚   โ”‚    โ”‚   โ”œโ”€โ”€๐Ÿ“œpostgres.service.ts
โ”‚   โ”‚    โ”‚   โ””โ”€โ”€๐Ÿ“œadd-others.ts
โ”‚   โ”‚    โ”œโ”€โ”€๐Ÿ“‚prisma/
โ”‚   โ”‚    โ”‚   โ”œโ”€โ”€๐Ÿ“‚migrations/
โ”‚   โ”‚    โ”‚   โ”œโ”€โ”€๐Ÿ“œprisma.service.ts
โ”‚   โ”‚    โ”‚   โ””โ”€โ”€ โ–ฒ schema.prisma
โ”‚   โ”‚    โ””โ”€โ”€๐Ÿ“‚typeorm/
โ”‚   โ”‚        โ””โ”€โ”€๐Ÿ“œtypeorm.database.ts
โ”‚   โ”‚โ”€โ”€โ”€๐Ÿ“œapp.module.ts
โ”‚   โ”‚โ”€โ”€โ”€๐Ÿ“œmain.ts
โ”‚   โ””โ”€โ”€โ”€๐Ÿ“‚test/
โ””โ”€โ”€โ”€.env

1. Errors (/src/errors)

This folder contains global error-handling utilities to manage application errors effectively.


2. Modules (/src/modules)

Modules represent the core business domains (e.g., products, users) and are structured into three layers:

a. Application Layer

Handles business logic, use cases, and mappers. It interacts with the domain layer and infrastructure.

  • Mappers: Converts entities and models between layers (e.g., product.mapper.ts).
  • Service: Implements use cases (product-use-case.service.ts).
  • Use Case: Defines interfaces for services.

b. Domain Layer

Defines business rules and core concepts:

  • Contracts: Interfaces to define repository behaviors.
  • DTOs: Data Transfer Objects for request/response handling.
  • Models: Business models encapsulating core logic.

c. Infrastructure Layer

Handles database operations, external services, and frameworks:

  • Adapters: Bridges domain and infrastructure.
  • Controller: Exposes endpoints to interact with application logic.
  • Entities: Defines database schemas.
  • Repositories: Implements persistence logic for databases (e.g., MySQL, PostgreSQL, Prisma).

3. Settings (/src/settings)

Contains environment configuration files and settings for different environments (envs.ts).


4. Shared (/src/shared)

Reusable utilities and services:

  • Database: Provides database connection services (e.g., mysql.service.ts, prisma.service.ts).
  • Prisma/TypeORM: Configurations for ORM tools.

5. Application Root

  • app.module.ts: Entry point for module registration.
  • main.ts: Bootstrap file for application initialization.

๐Ÿ” Environment Variables Configuration (.env)

This document explains the purpose of each environment variable used in the application. These variables ensure proper configuration and connection to external services and databases.

General Variables For Microservices

  • PORT: Specifies the port each microservice on which the application will run.

  • SECRET_KEY: A secret key used for token generation and application security. (authentication microservice)

  • PERCENTAGE_INCREMENT: Represents a percentage value for specific application logic. (products microservice)

  • PROVIDER_DATABASE: Specifies the provider to prisma.

  • DATABASE_URL: Connection string for Prisma, supporting PostgreSQL, MySQL, SQLite, SQL Server, MongoDB, or CockroachDB. It is used by Prisma to interact with the database. Refer to the Prisma documentation for more details:

General Variables For Gateway

PORT=5000

AUTH_MICROSERVICE_HOST=localhost
AUTH_MICROSERVICE_PORT=3001
AUTH_SERVICE=AUTH_SERVICE

PRODUCTS_MICROSERVICE_HOST=localhost
PRODUCTS_MICROSERVICE_PORT=3002
PRODUCTS_SERVICE=PRODUCTS_SERVICE

SALES_MICROSERVICE_HOST=localhost
SALES_MICROSERVICE_PORT=3003
SALES_SERVICE=SALES_SERVICE

RETURNS_MICROSERVICE_HOST=localhost
RETURNS_MICROSERVICE_PORT=3004
RETURNS_SERVICE=RETURNS_SERVICE

Description of Variables

  • PORT: Port on which the client gateway will run.

  • MICROSERVICE_HOST: Host (usually localhost during development) where each microservice is running.

  • MICROSERVICE_PORT: Port assigned to each corresponding microservice.

  • SERVICE: Identifier used by the gateway to route and communicate with each microservice.

These variables configure the client gateway to communicate with the Authentication, Products, Sales, and Returns microservices. Make sure the host and port match the actual running services.

Database Variables

Run tests (Not implemented yet)

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Deployment

When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the deployment documentation for more information.

If you are looking for a cloud-based platform to deploy your NestJS application, check out Mau, our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:

$ npm install -g mau
$ mau deploy

With Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.

Stay in touch

License

Nest is MIT licensed.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published