A progressive Node.js framework for building efficient and scalable server-side applications.
Table of contents
Click here to expand
I create this repo for learning, practicing and understanding how to work NestJS, GraphQL & Prisma together. Project also how to build a clean architecture scalable & testable.
Features of project:
- GraphQL with apollo-server-express, solution code first and using GraphQL play ground
- Using PostgreQL for database and Prisma 2 as ORM
- CRUD operations, database relationship, pagination
- NestJS Authentication with
JWT - Refresh token solution
- Role base system: Admin & User
- Database seeding
- Rating limit API with express-rate-limit
- Using custom exceptions filter, custom decorators
- Docker solution for PostgreSQL, Prisma
- Solve N+1 problem graphql (using @paljs/plugin)
If you want to develop project only on your local, your need have:
- NodeJS: I use NodeJS lts version (14.15.1)
- npm or yarn: yarn recommend
- @prisma/cli
- PostgreSQL
-
Clone project from this repository
-
Create and update
.envfile for variables environment$ cp .env
Then modify the variables as you want.
Example of
.envfiles:DB_CONNECTOR=postgres DB_HOST=localhost DB_USER=chickfool DB_PASSWORD=123456 DB_DATABASE=dummy_db DB_PORT=5432 REFRESH_TOKEN_SECRET="mysecret" ACCESS_TOKEN_SECRET="mysecret" APP_URL=http://localhost:3000 IMAGE_PATH=/images DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}?schema=public
-
Install dependencies
$ yarn # or npm install -
Generate prisma schema Make sure you generate prisma schema before run server:
$ yarn prisma generate
Check Prisma Client for more details how
prisma generate works -
Run server locally
$ yarn start:dev # or npm run start:dev
Other useful commands to work with project:
-
Run production
$ yarn start:prod
-
Run testing:
$ yarn test # for running the the Unit $ integration testing $ yarn test:e2e # for running end to end testing
-
Prisma migrate Prisma migrate is an imperative database schema migration tool that enables you to make changes to your database schema.
Migrate auto your schema:
$ yarn prisma migrate dev --preview-feature
Migrate with the name schema:
$ yarn prisma migrate dev --name initDb --preview-feature
When we run
prisma generateorprisma migrate,prisma/cliwill try connect with your database first. So you need to connect successfully with your database through your variableDATABASE_URLgiven inprisma.schema:For example, you have configuration in your
prisma.schemalike below:// prisma.schema datasource db { provider = "postgresql" url = env("DATABASE_URL") }
So you need to provide this value in
.envfile:DB_HOST=postgres DB_USER=postgres DB_PASSWORD=postgres DB_DATABASE=nest_graphql DB_PORT=5432 DATABASE_URL="postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}?schema=public"Check more information at Prisma migrate
-
Seeding database(optional) If you have any errors, so you can try running command below to sync your database:
$ yarn prisma db push
Then you can run seeding database:
$ yarn seed
When you run seeding database done. You can use Prisma studio for checking your database. If you see Movie table has empty. Don't worry, when you run server, the server will run seeding database automatically by cron.
-
Prisma studio
A Visual Interface for Your Database Prisma Studio helps developers manage their application data. We're excited to share that it is now part of the stable Prisma release. Try out the online demo or connect it to your existing database.
So with Prisma studio, you maybe don't need pgAdmin anymore.
Ton run Prisma studio:
$ yarn prisma:studio # or npx prisma studio # or yarn prisma studio
Then check out at http://localhost:5555
You need to run the code below to generate the documentation:
$ yarn document
# or npx document
# or yarn documentYou can check out the documentation of project at http://localhost:4400
To start working with Docker, you need have Docker installed locally and also the package manage as npm ro yarn
In the development mode, we have already PostgreSQL in our docker-compose file. You can use the same values environment in your .env like before.
$ docker-compose build # Build all services in docker-compose.yml file
$ docker-compose up # Run all services in docker-compose.yml
$ docker-compose down # Stop all services


