Skip to content

Latest commit

 

History

History
206 lines (152 loc) · 6.71 KB

README.md

File metadata and controls

206 lines (152 loc) · 6.71 KB

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Description

Nest framework TypeScript starter repository with configurations of the following -

  • Postgres DB
  • TypeORM (structured migrations & entities see usage)
  • Queue (Custom seamless queue implementation see usage)
  • Caching
  • Swagger for documentation
  • Custom environment variables handling with multiple files & validation using Zod ( ie. simplified Testing )
  • Helmet to set security-related HTTP headers appropriately
  • Logger using winston (console logs are nest like see usage)
  • Auth Guard (Expects a Bearar token): By default the following token will work if you want to test the protected routes eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjkzMTdmN2ZjLWVjYjQtNGEwOS04ZGFkLTZiMGZlMGZjM2VhMyIsImZpcnN0X25hbWUiOiJqb2huIiwibGFzdF9uYW1lIjoiZG9lIiwiZW1haWwiOiJqb2huQGRvZS5jb20iLCJpYXQiOjE3MDc0MTkwNzV9.PfUoWAPjFyA93GNaqAtys4x2wJgLd1sm89JvM3xlnt8

Installation

$ npm install

Usage

TypeORM

Define a new entity with the .entity.ts extension preferably in here src/models/your-model/entities/name.entity.ts or wherever you want, then generate a migration file with the following command -

npm run migration:generate --name=your_migration_file_name

It will generate a migration file in src/providers/typeorm/migrations

You can deploy the migrations using -

npm run migration:deploy

OR undo the latest migration using -

npm run migration:undo

QUEUE

Add a new job with the @Job() decorator & add processing logic to src/providers/queue/queue.jobs.ts with proper type definitions.

export class QueueJobs {
  /** Other Code */

  @Job()
  async newJob(payload: { message: string }): Promise<boolean> {
    /** code related to processing of the job */
    return true;
  }

  /** Other Code */
}

Now using QueueService, just add the job & processing will be handled automatically.

Class AnyService{
  constructor(
    private readonly queueService: QueueService
  ){}

  someMethod(){
    this.queueService.addJob({
      task: "newJob",
      payload: {
        message: "my-message"
      }
    })
  }
}

LOGGER

Crate new logger from the WinstonLogger class from src/utils/winston-logger/winston-logger.ts.

Logs are saved in the logs folder in the root directory, by default it will keep logs upto 30 days.

class MyService{
  private logger = new WinstonLogger(MyService.name) // sets up the context

  foo(){
    // use like this to pass additional data
    this.logger.error({
      message: "Unauthorized",
      data: {
        user: {
          id: 1,
          first_name: "Jyotirmay",
          last_name: "Barman"
        }
      }
    });
    // OR just to pass the message
    this.logger.error("Unauthorized");
  }

}

We have the following methods available in the WinstonLogger class -

type LogPayload = string | {
  message: string,
  data?: any,
  context?:string,
  stack?: string,
}

fatal(payload: LogPayload, context?: string, stack?: string);
error(payload: LogPayload, context?: string, stack?: string);
log(payload: LogPayload, context?: string, stack?: string);
warn(payload: LogPayload, context?: string, stack?: string);
debug(payload: LogPayload, context?: string, stack?: string);
verbose(payload: LogPayload, context?: string, stack?: string);
setContext(ctx:string);
getContext(): string;

NOTE : We avoid using dependency injection for the logger because it disrupts the context when implemented

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.