README.md

May 23, 2024 ยท View on GitHub

Description

The app use Nest Framework and PostgreSQL connector.

1. Preparation

  1. Install Nodejs, Yarn, PostgreSQL, Docker

  2. Copy .env.example to .env

  3. Set up your database connection in .env

2. Install Dependencies

Install the dependencies for the Nest application:

# yarn
yarn install

3. Set Database schemas

# yarn
yarn migrate

4. Start NestJS Server

Run Nest Server in Development mode:

# yarn
yarn start

# watch mode
yarn start:dev

Run Nest Server in Production mode:

# yarn
yarn start:prod

GraphQL Playground for the NestJS Server is available here: http://localhost:3000/graphql

GraphQL Playground

You could use following example queries in the GraphQL Playground.

Some queries and mutations are secured by an auth guard. You have to acquire a JWT token from signup or login.

mutation {login(data: {username: "YOURUSER", password: "YOURPASSWORD"}) {refreshToken, accessToken, user {id} }}

Add the accessTokenas followed to HTTP HEADERS in the playground and replace YOURTOKEN like this:

{
  "Authorization": "Bearer YOURTOKEN"
}

########################################

5*. 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

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

yarn prisma:generate
# or
npx prisma generate
yarn migrate:dev
# or
npx prisma migrate dev

Using built-in features

Multi tenancy

Use tenancy config in apps/api/src/common/configs/config.ts to set tenancy ON/OFF

tenancy: {
  enabled: false;
}

Pagination

Cover your model with Paginated type

@ObjectType()
export class PaginatedItems extends Paginated<Item>(Item) {}

Use paginate function in your service

return paginate(
  this.prisma.item,
  {
    where: where_active,
    orderBy: orderArray(orderBy)
  },
  paginateOptions
);

Sorting

Use orderArray function in your service and Sort enum in your object type

{
  orderBy: orderArray(orderBy);
}
import { Sort } from 'common/sorting/sort.enum';

@InputType()
export class ItemOrderByInput {
  @Field(() => Sort, { defaultValue: Sort.ASC })
  createdAt?: Sort;
}

Safe delete and safe filter

Use safeWhere and safeDelete function in your service

return await safeDelete<Item>(this.prisma, 'item', id);
{
  where: safeWhere(where);
}