Skip to content

SMRPO-2023/smrpo-api

Repository files navigation

SMRPO Project Backend

One API to rule them all

SMRPO Project Backend is an API, which communicates with the Web application to bring you seamless experience.

Features

  • NestJS
  • Prisma for database modelling, migration and type-safe access (Postgres, MySQL & MongoDB)
  • 🔐 JWT authentication w/ passport-jwt
  • REST API docs w/ Swagger

Setup

1. Install Dependencies

Install Nestjs CLI to start and generate CRUD resources

npm i -g @nestjs/cli

Install the dependencies for the Nest application:

npm install

2. Create database

Setup a development PostgreSQL with Docker. Copy .env.example and rename to .env - cp .env.example .env - 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

docker-compose up -d smrpo-db
# or
npm run docker:db

3. 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
npx prisma migrate dev
# or
npm run migrate:dev

If you like to customize your migration.sql file run the following command. After making your customizations run npx prisma migrate dev to apply it.

npx prisma migrate dev --create-only
# or
npm run migrate:dev:create

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.

npx prisma migrate deploy
# or
npm run migrate:deploy

Migrations can be also done using a Docker container.

docker-compose up -d prisma-migrate
# or
npm run docker:migrate

4. Seed the database data with this script (Optional)

Execute the script with this command:

npm run seed
# or
npm run docker:seed

5. Prisma: Prisma Client JS (Optional)

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

Run Nest Server

Directly

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

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

Using 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.

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

- DB_HOST=localhost
# replace with name of the database container
+ DB_HOST=smrpo-db

# Prisma database connection
+ DATABASE_URL=smrpo-db://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DB_HOST}:${DB_PORT}/${POSTGRES_DB}?schema=${DB_SCHEMA}&sslmode=prefer

If DATABASE_URL is missing in the root .env file, which is loaded into the Docker container, the NestJS application will exit with the following error:

(node:19) UnhandledPromiseRejectionWarning: Error: error: Environment variable not found: DATABASE_URL.
  -->  schema.prisma:3
   |
 2 |   provider = "postgresql"
 3 |   url      = env("DATABASE_URL")

Build Docker image:

docker build -t smrpo-api .

After Docker build your docker image you are ready to start up a docker container:

docker run -d -t -p 3000:3000 --env-file .env smrpo-api

Using Docker Compose

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

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

# start docker-compose
docker-compose up -d smrpo-api smrpo-db
# or
npm run docker

Schema Development

Update the Prisma schema prisma/schema.prisma and after that run the following two commands:

npx prisma generate
# or in watch mode
npx prisma generate --watch
# or
npm run prisma:generate
npm run prisma:generate:watch

You can also use the Docker migration container.

docker-compose up -d prisma-migrate
# or
npm run docker:migrate

Rest Api

RESTful API documentation available with Swagger.

Database model

Database model scheme available online.

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages