Skip to content

phatnguyenuit/itask-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

iTask GraphQL Server

CI/CD codecov License

iTask GraphQL server

Table of contents

Features

  • RESTful API
  • Apollo GraphQL server
  • Full TypeScript codebase
  • Unit test with Jest
  • Dockerize
  • Wait for PostgreSQL readiness before starting NodeJS server (on the Docker environment)

Setup

Prerequisites

  1. Prepare ENV files

    Base on .env.example file, define your environment variables

    cp .env.example .env
  2. Install node_modules

    • Using npm:
    npm install
    • Using yarn:
    yarn

Development

Start dev server

  • Using npm:

    npm run dev:server
  • Using yarn:

    yarn dev:server

Production

  1. Build production

    • Using npm:
    npm run build
    • Using yarn:
    yarn build
  2. Start production server using PM2

    • Using npm:
    npm run start:prod
    • Using yarn:
    yarn start:prod

Dockerize

1. Development

  • Prepare ENV files

    Base on .docker.dev.env.example file, define your environment variables

    cp .docker.dev.env.example .docker.dev.env
  • Start docker compose (docker-compose.yml & docker-compose.dev.yml)

    ./scripts/dev up -d
    
  • Stop docker compose (docker-compose.yml & docker-compose.dev.yml)

    ./scripts/dev stop
    

2. Production

  • Prepare ENV files

    Base on .docker.prod.env.example file, define your environment variables

    cp .docker.prod.env.example .docker.prod.env
  • Start docker compose (docker-compose.yml & docker-compose.prod.yml)

    ./scripts/prod up -d
    
  • Stop docker compose (docker-compose.yml & docker-compose.prod.yml)

    ./scripts/prod stop
    

Setup GitHub Actions

There are 2 (two) action workflows:

  • test

    This will be triggered automatically on every open Pull request to master branch

    This workflow runs test coverage script during the invocation

  • ci

    This will be triggered automatically on every open Pull request to master branch

    This workflow runs:

    • Test coverage
    • Upload test coverage to Codecov
    • Release new version

Required steps:

  • Register your repository with Codecov to get CODECOV_TOKEN
  • Configure repository secrets with name CODECOV_TOKEN

Scripts

1. Development

Run & develop

  • Using yarn:

    yarn dev:server
  • Using npm:

    npm run dev:server

2. Build

Build production source code

  • Using yarn:

    yarn build
  • Using npm:

    npm run build

3. Generate type validators

Generate TypeScript validator files

  • Using yarn:

    yarn codegen:validators
  • Using npm:

    npm run codegen:validators

4. Generate GraphQL TypeScript shared types

Generate TypeScript shared types from GraphQL

  • Using yarn:

    yarn codegen:gql
  • Using npm:

    npm run codegen:gql

5. Generate both GraphQL TypeScript shared types and type validators

Combine command 3 and command 4

  • Using yarn:

    yarn codegen
  • Using npm:

    npm run codegen

6. Generate Prisma client

Generate Prisma client (./node_modules/.prisma)

  • Using yarn:

    yarn prisma:generate
  • Using npm:

npm run prisma:generate

7. Migrate Prisma dev environment

Migrate Prisma schema and apply to database

  • Using yarn:

    yarn prisma:dev
  • Using npm:

    npm run prisma:dev

8. Deploy Prisma schema

Apply Prisma schema to production environment

  • Using yarn:

    yarn prisma:deploy
  • Using npm:

    npm run prisma:deploy

8. Git commit with commitizen CLI

Commit CLI command with interactive

  • Using yarn:

    yarn commit
  • Using npm:

    npm run commit

9. Backup database

pg_dump -v -U $POSTGRES_USER -O -d $POSTGRES_ITASK_DB > path/to/sql/file

10. Restore database

psql -v -U $POSTGRES_USER -d $POSTGRES_ITASK_DB < path/to/sql/file

11. Restore database automatically on Docker

  • Prepare a SQL backup file (named $POSTGRES_ITASK_DB.sql with $POSTGRES_ITASK_DB is your defined environment variable)
  • Place the SQL backup file under ./backup folder.
  • When docker running up, this backup file will be restored automatically.

12. Unit test

  • Using npm:

    npm run test
  • Using yarn:

    yarn test

13. Test coverage

As configuration from ./jest.config.ts Jest will fail if there is less than 90% branch, line, and function coverage, or if there are more than 10 uncovered statements:

  • Using npm:

    npm run test -- --coverage
  • Using yarn:

    yarn test --coverage

References