• Stars
    star
    165
  • Rank 228,906 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created 12 months ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Template for new services based on NestJS with the Best Practices and Ready for Production

Nest Logo

⭐ NestJS Service Template ⭐

Template for new services based on NestJS with the Best Practices and Ready for Production

nodejs node typescript npm fastify swc swc docker

πŸ‘€ Motivation

When we start creating some new service based on NestJS most often we just use the Nest cli for starting a new service that already give us some convention and structure for our project. This is a good starting point however I was missing a couple of interesting things that almost all services should have to be ready to deploy to production like fully dockerized, ensuring coding conventions...

For this reason I created this custom template for new services based on this framework, with everything I would like to have to start developing a service with the best practices but with a simple file structure so later developers can change to implement their logic.

Here we are not providing any specific architecture like hexagonal architecture or others, this is like a simple template where later we can customize and create the architecture we need.

🌟 What is including this template?

  1. 🐳 Fully dockerized service ready for development and production environments with the best practices for docker, trying to provide a performance and small image just with the code we really need in your environments.
  2. πŸ‘· Use SWC for compiling and running the tests of the service. As commented in the own NestJS docs, this is approximately x20 times faster than default typescript compiler that is the one that comes by default in NestJS.
  3. ⚑️ Use Fastify as Web Framework. By default, NestJS is using Express because is the most widely-used framework for working with NodeJS, however, this does not imply is the one is going to give us the most performance. Also, NestJS is fully compatible with Fastify, so we are providing this integration by default. You can check here comparison between different web frameworks.
  4. 🐢 Integration with husky to ensure we have good quality and conventions while we are developing like:
    • πŸ’… Running the linter over the files that have been changed
    • πŸ’¬ Use conventional commits to ensure our commits have a convention.
    • βœ… Run the tests automatically.
    • βš™οΈ Check our project does not have type errors with Typescript.
  5. πŸ—‚οΈ Separate tests over production code. By default, NestJS is combining in the same folder, the src, the unit tests and the code we are developing for production. This is something I personally don't like so here I am separating this and having a dedicated folder for the unit tests.
  6. πŸ§ͺ Testing with Vitest and supertest for unit and e2e tests.
  7. πŸ€œπŸ€› Combine unit and e2e test coverage. In the services we may have both type of tests, unit and e2e tests, and usually we would like to see what is the combined test coverage, so we can see the full picture.
  8. πŸ“Œ Custom path aliases, where you can define your own paths (you will be able to use imports like @shared/logger instead of ../../../src/shared/logger).
  9. πŸš€ CI/CD using GitHub Actions, helping ensure a good quality of our code and providing useful insights about dependencies, security vulnerabilities and others.

🀩 Other templates

Are you thinking in start new projects in other frameworks or create a super fancy library? If you like this template there are others base on this you can check:

πŸ§‘β€πŸ’» Developing

First, we will need to create our .env file, we can create a copy from the example one:

cp .env.example .env

The project is fully dockerized 🐳, if we want to start the app in development mode, we just need to run:

docker-compose up -d my-service-dev

This development mode with work with hot-reload and exposing a debug port, the 9229, so later we can connect from our editor to it.

Now, you should be able to start debugging configuring using your IDE. For example, if you are using vscode, you can create a .vscode/launch.json file with the following configuration:

{
  "version": "0.1.0",
  "configurations": [
    {
      "type": "node",
      "request": "attach",
      "name": "Attach to docker",
      "restart": true,
      "port": 9229,
      "remoteRoot": "/app"
    }
  ]
}

Also, if you want to run the production mode, you can run:

docker-compose up -d my-service-production

This service is providing just a health endpoint which you can call to verify the service is working as expected:

curl --request GET \
  --url http://localhost:3000/health

If you want to stop developing, you can stop the service running:

docker-compose down

βš™οΈ Building

npm run build

βœ… Testing

The service provide different scripts for running the tests, to run all of them you can run:

npm run test

If you are interested just in the unit tests, you can run:

npm run test:unit

Or if you want e2e tests, you can execute:

npm run test:e2e

πŸ’… Linting

To run the linter you can execute:

npm run lint

And for trying to fix lint issues automatically, you can run:

npm run lint:fix

More Repositories

1

express-typescript-service-template

Template for new services based on Express and Typescript with the Best Practices and Ready for Production
TypeScript
69
star
2

hexagonal-architecture-typescript-service-template

Typescript template for new services using hexagonal architecture
TypeScript
48
star
3

hexagonal-typescript

TypeScript
12
star
4

typescript-library-template

Template for new libraries based on Typescript with the Best Practices and Ready for Production
TypeScript
10
star
5

branch-name-action

Check if the branch name is following a convention name
JavaScript
9
star
6

awilix-ts-example

App example about how we can improve our code using awilix
TypeScript
8
star
7

repository-pattern-typescript-example

Example of how to implement the repository pattern using typescript
TypeScript
8
star
8

nest-users-crud

TypeScript
7
star
9

assign-me

Automatically adds you to the assignees in the pull request
JavaScript
6
star
10

working-label-action

GitHub Action to add labels depending if the pull request is ready to review
JavaScript
5
star
11

ts-node-hexagonal-template

Template for new projects using a basic hexagonal architecture
TypeScript
5
star
12

ts-husky-example

Example of how we can configure a typescript project for using husky to ensure the quality of our code
TypeScript
4
star
13

controllers-single-responsibility-principle

TypeScript
4
star
14

dependency-cruiser-example

JavaScript
2
star
15

object-mother-example

Example of how we can use object mother for improving testing strategy
TypeScript
2
star
16

async-local-storage-example

TypeScript
2
star
17

nodejs-redis-cache-example

TypeScript
2
star
18

hello

Shell
2
star
19

bull-bus

Node bus implementation using bull for async queues
TypeScript
2
star
20

ts-node-sentry-example

Example about how to configure sentry in a typescript backend app
TypeScript
2
star
21

visual-studio-code-config

Configuration of my visual studio code
2
star
22

nest-users-service

TypeScript
1
star
23

git-hooks

Shell
1
star
24

ts-node-dotenv-example

Example of a typescript node application using dotenv for env vars
TypeScript
1
star
25

wn-tapes-service

What Next - Tapes Service
TypeScript
1
star
26

consume-api-in-node

JavaScript
1
star
27

system-status

Application that helps companies to report on the status of their services
TypeScript
1
star
28

AlbertHernandez

My personal profile
1
star
29

feature-flag-config-cat-example

TypeScript
1
star
30

js-sdk-with-commands

Example of how we can create a sdk using the command pattern
JavaScript
1
star
31

ts-node-convict-example

Example of a Typescript Node Application that uses convict for env vars
TypeScript
1
star
32

ts-node-request-validation-joi

Example of a Node application that uses joi for validating requests
TypeScript
1
star
33

haikus-for-codespaces

Example of a proejct using codespaces
EJS
1
star
34

github-action-nodejs-template

Template for new Github Actions based on Typescript with the Best Practices and Ready to be Released
TypeScript
1
star
35

nestjs-decorators-example

Example of how to create typescript decorators in nestjs accessing to the container of nestjs
TypeScript
1
star
36

ts-node-logger-with-request-id-example

TypeScript
1
star
37

todo-node

Todo application using node
TypeScript
1
star
38

nestjs-async-validation-class-validator

Example of how to use class-validator for creating async validations in nestjs project
TypeScript
1
star
39

is-ready-action

Action that checks if a PR is ready to be merged in the main branch
JavaScript
1
star