NestJS starter template with TypeScript and Prisma to kickstart your Node.js project.
This starter template includes:
- NestJS - Modern and progressive Node.js framework
- TypeScript - Your safety net when writing JavaScript
- Prisma - Next generation Node.js and TypeScript ORM
- pnpm - Fast and efficient package manager
- Docker - Develop, ship, and run your app with containers
- Redis - Popular in-memory key-value database and caching system
- PostgreSQL - Popular open-source SQL relational database management system
- pgAdmin - Open-source Postgres admin and development tools
- ESLint & Prettier - Find & fix problems in the codebase and format code automatically on save
- Makefile - Handy automation tool to run and compile your programs more efficiently
- Pino - Node.js logging tools
- OpenAPI - Standard specification for HTTP APIs
- Husky - Git hooks tool
- Commit Lint - Force devs to follow conventional commit message
- Lint Staged - Format & lint your code before committing, block commit if issues are detected
- Github Actions - Check, test, and build your code automatically on push and PR
- Dependabot - Create pull-request to update your dependencies
- Jest - Popular JavaScript unit testing library
- Supertest - SuperAgent driven library for testing HTTP servers
-
Clone repo:
Tips: You can use degit to clone this repo without running
git clone
. Learn moredegit hanstanawi/nestjs-starter-template your-app-name
or simply use this template on GitHub when creating new repository.
-
Install dependencies:
Make sure you have pnpm installed. Learn more about installing pnpm.
pnpm install
-
Install Nest CLI
Nest CLI is a great command-line interface tool that helps you to initialize, develop, and maintain your Nest project. You need to install it globally in order to run the
nest
command in terminal.pnpm -g add @nestjs/cli
-
Run the database
Since this project uses Prisma ORM. The Postgres database container needs to be running upon the project init to avoid any Prisma connection error.
pnpm db:up
To learn more about running databases using Docker. Checkout the Prisma section.
-
Run the development server
pnpm start:dev
-
Make a request to http://localhost:3333 with cURL to see the result.
curl --location "http://localhost:3333/api/v1"
You can start editing the project by modifying
app.module.ts
or create a new module by runningnest g module your-new-module
The project auto-updates as you edit the file.
You can run this project without having Node.js installed on your machine by running it using Docker as a container. You can learn more about Docker here.
Make sure Docker is installed and Docker daemon is running on your machine. You can learn more how to download and setup Docker here.
-
Run the Docker containers using Docker Compose
docker compose up -d --build
-
Stop running containers
docker compose down
You can also run these commands with make
command based on this project's Makefile configuration to avoid memorizing the long docker
commands.
-
Run containers
make docker-run
-
Stop container
make docker-stop
This NestJS starter template comes with Prisma ORM, a modern Node.js and TypeScript ORM that works really well with NestJS. You can check the full official guide to get started with NestJS and Prisma here.
-
Running the database
It only runs Postgres container with Docker Compose
pnpm db:up
-
Create a database migration
Whenever you make any changes in the
schema.prisma
file, you need to create a migration to reflect the changes you made on the schema to the database.To create a migration, simply run this command, then enter the name of the migration. e.g.
add_user_table
pnpm prisma migrate dev
-
Run existing migrations
pnpm db:deploy
-
Generate Prisma Client
It runs the
prisma generate
command under the hood to generate type definitions based on theschema.prisma
schema.pnpm db:generate
-
Run Prisma Studio
It runs the Prisma Studio tools to open Prisma database admin client tools.
pnpm db:studio
This starter template has Jest setup to run unit and integration API tests. Tests are automatically executed on every pull request and push by GitHub Actions CI workflow.
You can run unit tests and e2e tests manually on your machine:
- Run all unit tests
pnpm test
- Run test in watch mode
pnpm test:watch
- Unit test coverage
pnpm test:cov
This starter template has integration tests with Jest and Supertest setup out of the box. In integration tests, we test the full feature of each APIs to ensure each API behave correctly and spot bugs early.
We need to test the database connection and integration with the app. Therefore, before we run integration tests, all database connection needs to be setup. Different database for integration tests needs to be setup, so it will not disturb our development database. The Postgres test database container configuration can be found in docker-compose.test.yaml
to learn more.
-
Create
.env.test
file to load env variables for test environmentYour existing
.env
variables may vary. However, you should change these.env
variables to test variablesPOSTGRES_USERNAME="YOUR_TEST_DB_USERNAME" POSTGRES_PASSWORD="YOUR_TEST_DB_PASSWORD" POSTGRES_DB="YOUR_TEST_DB_NAME" DATABASE_URL="" REDIS_HOST="" REDIS_PORT=""
-
Run integration test setup script
make e2e-test-up
In this script, it runs the Postgres docker container test database and run existing migrations to test database.
-
Run integration tests
-
Run all integration tests
pnpm test:e2e
-
Watch mode
pnpm test:e2e:watch
-
-
Stop Postgres test database container
make e2e-test-down
This starter template has OpenAPI Swagger documentation setup out of the box. You can update the API documentation based to your liking on your application API specification. To learn more about OpenAPI with NestJS configuration, you can visit the documentation here.
To view OpenAPI documentation page
-
Start the development server
pnpm start:dev
Don't forget to start the postrges container, otherwise Prisma will throw a connection error when starting up the app.
pnpm db:up
-
Open your browser and visit http://localhost:3333/api to view the API documentation.
To learn more about NestJS and its ecosystem, take a look at the following resources:
- Learn NestJS - learn about NestJS fundamentals, techniques, and recipes.
- Learn NestJS tutorial series - comprehensive tutorial to learn NestJS from the ground up.
- Learn TypeScript - an interactive course to learn TypeScript
- Learn Prisma - learn about Prisma ORM core concepts, queries, and migrations.
- Learn Building REST API with NestJS and Prisma - learn about building a REST API with NestJS and Prisma
- Learn PostgreSQL - learn the fundamentals of PostgreSQL
- Learn Docker - learn about the basics of Docker
- Learn Testing with Prisma - learn about multiple testing techniques with Prisma