/next-blog-backend

Primary LanguageTypeScriptMIT LicenseMIT

Install dependencies

Install Nestjs CLI to start and generate CRUD resources

# pnpm
pnpm i -g @nestjs/cli
# yarn
yarn add -g @nestjs/cli
# npm
npm add -g @nestjs/cli

Install the dependencies for the Nest application:

# pnpm
pnpm install
# npm
npm install
# yarn
yarn install

.env

support different .env files .env.development and .env.production

PostgreSQL setup

notice

Don't use prisma client command directly. Because prisma doesn't support different .env files, so we use ./scripts/prisma.js to loaded different .env files.

using prisma client command like this below

npm run prisma -> node ./scripts/prisma.js
prisma migrate dev -> npm run prisma dev migrate dev
prisma migrate deploy -> npm run prisma prod migrate deploy

dev -> development

prod -> production

Init postgres with docker

Setup a development PostgreSQL with Docker. Copy .env.example and rename to .env - cp .env.example .env.development and cp .env.example .env.production - which sets the required environments for PostgreSQL such as POSTGRES_USER, POSTGRES_PASSWORD and POSTGRES_DB. Update the variables as you wish and select a strong password.

Start the PostgreSQL database using docker (recomand)

docker-compose -f docker-compose.db.yml up -d

Prisma Migrate

Prisma Migrate is used to manage the schema and migration of the database. Prisma datasource requires an environment variable DATABASE_URL for the connection to the PostgreSQL database. Prisma reads the DATABASE_URL from the root .env file.

Use Prisma Migrate in your development environment to

  1. Creates migration.sql file
  2. Updates Database Schema
  3. Generates Prisma Client
npm run migrate:dev

deploy db changes in production

If you are happy with your database changes you want to deploy those changes to your production database. Use prisma migrate deploy to apply all pending migrations, can also be used in CI/CD pipelines as it works without prompts.

npm run migrate:deploy

Prisma: Prisma Client JS

Prisma Client JS is a type-safe database client auto-generated based on the data model.

Generate Prisma Client JS by running

Note: Every time you update schema.prisma re-generate Prisma Client JS

npx prisma generate
# or
npm run prisma:generate

NestJS Server

Run Nest Server in Development mode:

npm run start

# watch mode
npm run start:dev

Run Nest Server in Production mode:

npm run start:prod

logs

  • pino-http to pretty log and logging http logs
  • rotating-file-stream saves log files to ./logs directory. it will divide logs files to seperate files day by day.

Rest Api

RESTful API (localhost:3000/api) documentation available with Swagger.

Docker

Nest server is a Node.js application and it is easily dockerized.

See the Dockerfile on how to build a Docker image of your Nest server.

Now to build a Docker image of your own Nest server simply run:

# give your docker image a name
docker build -t <your username>/nest-prisma-server .
# for example
docker build -t nest-prisma-server .

After Docker build your docker image you are ready to start up a docker container running the nest server:

docker run -d -t -p 3000:3000 -v /path/to/logs:/app/logs --env-file .env.production nest-prisma-server

Now open up localhost:3000 to verify that your nest server is running.

When you run your NestJS application in a Docker container update your .env file

Docker Compose

You can also setup a the database and Nest application with the docker-compose

# building new NestJS docker image
docker-compose build
# or
npm run docker:build

# start docker-compose
docker-compose up -d
# or
npm run docker

nest-prisma-starter