Greg Magolan_ Jay Bell - Nest.js- A Progressive Node.js Framework (2020).pdf

(2789 KB) Pobierz
Table of Contents
Preface .............................................................................................................................. 8
What is Nest.js?
................................................................................................................ 8
About the example ........................................................................................................... 9
About the authors .......................................................................................................... 10
Chapter 1. Introduction ................................................................................................. 11
Topics discussed ............................................................................................................. 12
Nest CLI
......................................................................................................................... 12
Dependency Injection
..................................................................................................... 13
Authentication
............................................................................................................... 14
.
ORM .............................................................................................................................. 15
REST API ...................................................................................................................... 16
WebSockets .................................................................................................................... 16
Microservices ................................................................................................................. 17
GraphQL ....................................................................................................................... 17
Routing
........................................................................................................................... 17
Nest specific tools ........................................................................................................... 18
OpenAPI (Swagger) ....................................................................................................... 19
Command Query Responsibility Segregation (CQRS) .................................................. 19
Testing
............................................................................................................................ 19
Server-side rendering with Angular Universal .............................................................. 20
Summary
........................................................................................................................ 20
Chapter 2. Overview ...................................................................................................... 22
Controllers ..................................................................................................................... 22
Providers ........................................................................................................................ 22
Modules
.......................................................................................................................... 23
Bootstrapping
................................................................................................................. 24
Middleware .................................................................................................................... 25
Guards ........................................................................................................................... 27
Summary
........................................................................................................................ 28
Chapter 3. Nest.js authentication .................................................................................... 30
Passport
......................................................................................................................... 30
.
Manual implementation ................................................................................................. 31
Implementation
..................................................................................................................... 31
AUTHENTICATION MODULE
.......................................................................................... 31
USER MODULE
..................................................................................................................... 36
APP MODULE
........................................................................................................................ 42
Authentication middleware
................................................................................................ 44
.
Managing restrictions with guards ................................................................................... 44
Nest.js passport package ................................................................................................ 45
Summary
........................................................................................................................ 48
Chapter 4. Dependency Injection system of Nest.js ......................................................... 49
Overview of Dependency Injection ................................................................................ 49
Why use Dependency Injection ......................................................................................... 50
How it works without Dependency Injection ................................................................... 50
How it works with a manual Dependency Injection
........................................................ 51
Dependency Injection pattern today ................................................................................. 52
Nest.js Dependency Injection ......................................................................................... 53
The difference between Nest.js and Angular DI ............................................................ 57
Summary
........................................................................................................................ 57
Chapter 5. TypeORM ....................................................................................................... 59
What database to use ..................................................................................................... 59
About MariaDB ..................................................................................................................... 60
Getting started ............................................................................................................... 60
Start the database ................................................................................................................ 61
Connect to the database ..................................................................................................... 62
Initialize TypeORM ............................................................................................................... 63
Modelling our data
......................................................................................................... 63
Our first entity
....................................................................................................................... 63
Using our models
............................................................................................................ 65
The service
............................................................................................................................ 65
The controller ....................................................................................................................... 67
Building a new module
........................................................................................................ 67
Improving our models
.................................................................................................... 70
Auto-generated IDs .............................................................................................................. 71
When was the entry created?
............................................................................................. 72
Column types
........................................................................................................................ 74
COLUMN TYPES FOR
MYSQL
/
MARIADB ..................................................................
74
COLUMN TYPES FOR
POSTGRES .................................................................................
75
COLUMN TYPES FOR
SQLITE
/
CORDOVA
/
REACT-NATIVE
............................ 75
COLUMN TYPES FOR
MSSQL .........................................................................................
75
COLUMN TYPES FOR
ORACLE ......................................................................................
75
NoSQL in SQL
....................................................................................................................... 75
Relationships between data models
................................................................................ 76
How to store related entities .............................................................................................. 80
SAVING RELATED ENTITIES THE EASIER WAY .................................................
82
Retrieving related entities in bulk
..................................................................................... 84
.
Lazy relationships
................................................................................................................ 86
Other kinds of relationships
........................................................................................... 88
One-to-one ............................................................................................................................ 88
BI-DIRECTIONAL ONE-TO-ONE RELATIONSHIPS ..............................................
90
Many-to-many ....................................................................................................................... 92
Advanced TypeORM ..................................................................................................... 92
Security first
......................................................................................................................... 92
.
Other listeners ...................................................................................................................... 93
Composing and extending entities ................................................................................... 93
EMBEDDED ENTITIES
....................................................................................................... 94
ENTITY INHERITANCE
...................................................................................................... 96
Caching
.................................................................................................................................. 97
Building a query ................................................................................................................... 99
Building our model from a existing database ............................................................... 100
Summary
...................................................................................................................... 100
Chapter 6. Sequelize ...................................................................................................... 102
Configure Sequelize ..................................................................................................... 102
Create a model ............................................................................................................. 105
@Table ................................................................................................................................. 105
@column ............................................................................................................................. 106
Create the User model
....................................................................................................... 107
LifeCycle hooks
.................................................................................................................. 109
Injecting a model into a service
.................................................................................... 110
Usage of Sequelize transaction ..................................................................................... 111
Migration ..................................................................................................................... 112
Configuring the migration script ..................................................................................... 113
Create a migration
............................................................................................................. 114
.
Summary
...................................................................................................................... 114
Chapter 7. Mongoose .................................................................................................... 116
A word about MongoDB .............................................................................................. 116
A word about Mongoose .............................................................................................. 116
Mongoose and Nest.js
....................................................................................................... 117
Getting started ............................................................................................................. 117
Set up the database ........................................................................................................... 117
Start the containers ........................................................................................................... 118
Connect to the database ................................................................................................... 118
THE CONNECTION STRING .........................................................................................
119
THE RIGHT ARGUMENT FOR THE
FORROOT()
METHOD
............................... 120
Modelling our data
....................................................................................................... 120
Our first schema
................................................................................................................. 120
INCLUDING THE SCHEMA INTO THE MODULE ..................................................
122
INCLUDE THE NEW MODULE INTO THE MAIN MODULE ...............................
122
Using the schema
.......................................................................................................... 123
The interface ....................................................................................................................... 123
The service
.......................................................................................................................... 124
The controller ..................................................................................................................... 125
The first requests ......................................................................................................... 126
Relationships
................................................................................................................ 127
Modelling relationships
..................................................................................................... 128
Saving relationships
.......................................................................................................... 129
Reading relationships ....................................................................................................... 131
Summary
...................................................................................................................... 133
Chapter 8. Web sockets
................................................................................................ 134
WebSocketGateway ..................................................................................................... 134
Gateways ...................................................................................................................... 135
Authentication
............................................................................................................. 137
.
Adapter ........................................................................................................................ 138
Client side
.................................................................................................................... 141
.
Summary
...................................................................................................................... 144
Chapter 9. Microservices ............................................................................................. 145
Server bootstrap
.......................................................................................................... 145
.
Configuration
............................................................................................................... 146
First microservice handler ........................................................................................... 146
Sending data
................................................................................................................ 147
.
Exception filters ........................................................................................................... 149
Pipes ............................................................................................................................. 151
Guards ......................................................................................................................... 152
Interceptors
.................................................................................................................. 154
Built-in transports
........................................................................................................ 155
Redis .................................................................................................................................... 155
MQTT
.................................................................................................................................... 157
NATS .................................................................................................................................... 158
gRPC .................................................................................................................................... 160
Custom transport ......................................................................................................... 165
Hybrid application ....................................................................................................... 172
Advanced architecture design
...................................................................................... 173
Summary
...................................................................................................................... 175
Chapter 10. Routing and request handling in Nest.js ..................................................... 177
Request handlers
.......................................................................................................... 177
Generating responses ................................................................................................... 178
Standard approach ............................................................................................................ 178
Express approach .............................................................................................................. 179
Route parameters
......................................................................................................... 179
Request body
................................................................................................................ 180
Request object .............................................................................................................. 180
Asynchronous handlers
................................................................................................ 181
Async/await
......................................................................................................................... 181
Promise
................................................................................................................................ 182
Observables ........................................................................................................................ 182
Error responses
............................................................................................................ 183
HttpException ..................................................................................................................... 183
Unrecognized exceptions ................................................................................................. 184
Summary
...................................................................................................................... 184
Chapter 11. OpenAPI (Swagger) Specification ................................................................ 186
Document Settings ....................................................................................................... 187
Documenting authentication ............................................................................................ 188
Swagger UI
................................................................................................................... 190
API input decorators
................................................................................................... 192
.
@Body ................................................................................................................................. 196
@Param ............................................................................................................................... 200
@Query ................................................................................................................................ 203
@Headers ............................................................................................................................ 206
Authentication .................................................................................................................... 210
API request and response decorators .......................................................................... 213
API metadata decorators ............................................................................................. 219
Saving the swagger document ...................................................................................... 225
Summary
...................................................................................................................... 225
Chapter 12. Command Query Responsibility Separation (CQRS) .................................... 227
Entry module commands ............................................................................................. 228
Command handlers ........................................................................................................... 229
Invoking command handlers
............................................................................................ 234
Linking keywords with events
..................................................................................... 239
.
Keyword events
.................................................................................................................. 249
Invoking event handlers
................................................................................................... 253
.
Retrieving keywords APIs ........................................................................................... 258
Linking keywords with sagas ....................................................................................... 262
Keyword saga commands ................................................................................................ 262
Zgłoś jeśli naruszono regulamin