NestJS Playbook
creating NestJS project and generate nest artifacts with-in monorepo.
An Introduction to NestJS

Workspace

Prerequisites

Install Global Packages

1
yarn global remove @nestjs/cli
2
yarn global add @nestjs/cli
Copied!

Scaffold Project

adding code dependencies
1
# generate Yeti API app
2
# `nest generate app yeti-api`
3
ng g @nrwl/nest:app yeti-api --frontend-project=yeti-app --linter=eslint --tags="domain:yeti,type:api,platform:node"
4
# generate `config` lib
5
ng g @nrwl/nest:lib config --global=true --directory=nestjs \
6
--controller=false --service=true --publishable=true \
7
--target=es2020 --tags="domain:yeti,type:api,platform:node" --dry-run # use prefix @yeti
8
9
# Add nestjs
10
yarn workspace @yeti/api add @nestjs/{common,core,microservices,swagger,websockets,typeorm,passport,elasticsearch}
11
12
# fastify-* are optional. use only if you want to replace express with fastify
13
yarn workspace @yeti/api add fastify fastify-formbody fastify-swagger
14
15
yarn workspace @yeti/api add -D @nestjs/testing
16
yarn workspace @yeti/api add -D @nestjs/schematics
Copied!
check versions
1
# check of nest installed
2
cd apps/api
3
nest info
Copied!

Dependencies

adding 3rd party modules/libs
1
yarn add pg
2
yarn add dotenv
3
yarn add helmet
4
yarn add web-push
5
yarn add nest-router
6
yarn add nodemailer pug
7
yarn add class-validator
8
yarn add class-transformer
9
yarn add passport passport-jwt @xmlking/jwks-rsa
10
11
yarn add -D rimraf nodemon tsconfig-paths ts-loader
12
yarn add -D jest ts-jest @types/jest supertest @types/supertest
13
yarn add -D @types/nodemailer
14
yarn add -D @types/socket.io
15
yarn add -D @types/[email protected] @types/passport-jwt
16
yarn add -D @types/web-push
17
yarn add -D @types/helmet
Copied!

Plugins

1
# add https://github.com/prisma/prisma
2
nest add nestjs-prisma
Copied!

Generate Artifacts

1
# scaffold echo module
2
nest g module app/echo -p api --dry-run
3
nest g controller app/echo -p api --dry-run
4
5
# scaffold core module
6
nest g module app/core -d
7
nest g guard auth app/core -d
8
9
# scaffold shared module
10
nest g module app/shared -d
11
nest g gateway eventbus app/shared -d
12
13
# scaffold user module
14
nest g module app/user -d
15
nest g class user.entity app/user --no-spec -d
16
nest g controller user app -d
17
nest g service user app -d
18
nest g controller profile app/user -d
19
nest g service profile app/user -d
20
nest g class profile/profile.entity app/user --no-spec -d
21
nest g controller email app/user -d
22
23
# scaffold email module
24
nest g module app/email -d
25
nest g service email app/email --flat -d
26
27
# scaffold auth module
28
nest g module app/auth -d
29
nest g class auth.exception app/auth --no-spec -d
30
31
# scaffold chat module
32
nest g module app/chat -d
33
nest g controller chat app/chat --flat -d
34
nest g service chat app/chat --flat -d
35
nest g gateway chat app/chat --flat -d
36
37
# scaffold external module
38
nest g module app/external -d
39
nest g controller weather app/external -d
40
nest g service weather app/external -d
41
42
# scaffold project module
43
nest g module app/project -d
44
nest g controller kubernetes app/project -d
45
nest g service kubernetes app/project -d
46
nest g class cluster/cluster.entity app/project --no-spec -d
47
nest g controller cluster app/project -d
48
nest g service cluster app/project -d
49
nest g class project.entity app/project --no-spec -d
50
nest g controller project app -d
51
nest g service project app -d
52
53
# scaffold notifications module
54
nest g module app/notifications -d
55
nest g controller notification app/notifications -d
56
nest g service notification app/notifications -d
57
nest g service notification/push app/notifications --flat --no-spec -d
58
nest g class notification/notification.entity app/notifications --no-spec -d
59
nest g controller subscription app/notifications -d
60
nest g service subscription app/notifications -d
61
nest g class subscription/subscription.entity app/notifications --no-spec -d
62
63
You could also use `ng g` if you `yarn add -D @nestjs/schematics`
64
ng g @nestjs/schematics:module game --path app --source-root apps/api/src -d
65
ng g @nestjs/schematics:controller match --path app/game --source-root apps/api/src -d
66
ng g @nestjs/schematics:service match --path app/game --source-root apps/api/src -d
67
ng g @nestjs/schematics:class match/match.entity --path app/game --source-root apps/api/src --spec -d
Copied!

Ref

Last modified 9mo ago