Giter Club home page Giter Club logo

demo-nest-example's Introduction

NestJS Example

A simple Nestjs service built with Nestjs 9, ioredis, mongoose, event-emitter and Joi.

Author: Thanh Nguyen

Tech stack

Back-end

  • NestJS 9
  • ioredis - Redis client: set, get, job and more.
  • mongoose - Database TypeOrm: save, pre-hooks and more.
  • event-emitter - Nodejs Event: emit, listent and more.
  • Joi - Validator for JavaScript

Structure

Below is the simplified version of the application structure.

.
└── root
    └── apps (dir)
    │   └── main.ts
    └── libs (dir)
        ├── demo (dir)
        │   ├── data-access (dir) - for configure service, middleware, validator logic to share across the feature
        │   ├── feature (dir) - for configure my controller forRoot modules
        │       └── demo.controller.ts (nest:controller Route handler client request)
        └── shared (dir)
            ├── auth-config (dir, authorization)
            ├── data-access (dir)
            │       └── adapters (@nestjs:lib, Adapter logics to share across the feature)
            │       └── managers (@nestjs:lib, Event-emitter listenter logic)
            │       └── entities (@nestjs:lib, Database schema to share across the feature)
            │       └── models (@nestjs:lib, Database schema to share across the feature)
            ├── enviroments (dir, replace token for environment)
            ├── persistent-config (dir, All database register)

Getting Started

  1. Clone My Project: git clone https://github.com/thanh1669/demo-nest-example.git
  2. Install dependencies: yarn install
  3. Open http://localhost:3000/api/docs to access the SwaggerUI
  4. Set environment variables: cp .env.example .env
  5. Running: yarn start:dev to start the API
  6. Running Production: yarn start:prod
  7. Building Production: yarn build

Generate a Nest element

nest generate <schematics> <dir> <options>

Options:
  -d, --dry-run                      Report actions that would be taken without writing out results.
  -p, --project [project]            Project in which to generate files.
  --flat                             Enforce flat structure of generated element.
  --no-flat                          Enforce that directories are generated.
  --spec                             Enforce spec files generation. (default: true)
  --skip-import                      Skip importing (default: false)
  --no-spec                          Disable spec files generation.
  -c, --collection [collectionName]  Schematics collection to use.
  
Schematics:
┌───────────────┬─────────────┬──────────────────────────────────────────────┐
    │ name          │ alias       │ description                                  │
    │ application   │ application │ Generate a new application workspace         │
    │ class         │ cl          │ Generate a new class                         │
    │ configuration │ config      │ Generate a CLI configuration file            │
    │ controller    │ co          │ Generate a controller declaration            │
    │ decorator     │ d           │ Generate a custom decorator                  │
    │ filter        │ f           │ Generate a filter declaration                │
    │ gateway       │ ga          │ Generate a gateway declaration               │
    │ guard         │ gu          │ Generate a guard declaration                 │
    │ interceptor   │ itc         │ Generate an interceptor declaration          │
    │ interface     │ itf         │ Generate an interface                        │
    │ middleware    │ mi          │ Generate a middleware declaration            │
    │ module        │ mo          │ Generate a module declaration                │
    │ pipe          │ pi          │ Generate a pipe declaration                  │
    │ provider      │ pr          │ Generate a provider declaration              │
    │ resolver      │ r           │ Generate a GraphQL resolver declaration      │
    │ service       │ s           │ Generate a service declaration               │
    │ library       │ lib         │ Generate a new library within a monorepo     │
    │ sub-app       │ app         │ Generate a new application within a monorepo │
    │ resource      │ res         │ Generate a new CRUD resource          

Create a Library Example

# 1. Check file generator
nest generate lib demo/feature --flat --dry-run

# 2. Add to project
nest generate lib demo/feature --flat

# 3. Import to apps/app.module
@Module({
    imports: [FeatureDemoModule]
})

# 4. Run project
yarn start:dev

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.