VSCode prepare:
Concepts for Nestjs:
Modules
- Each application has at least one module - the root module. That is the starting point of the application.
- Modules are an effective way to organize components by a closely related set of capabilities (e.g. per feature)
- It is a good practice to have a folder per module, containing the module's components.
- Modules are singletons, therefore a module can be imported by multiple other modules.
Controller
Provider & Service
NestJS Providers
- Can be injected into constructors if decorated as an @Injectable, via dependency injection.
- Can be a plain value, a class, sync/async factory etc.
- Providers must be provided to a mobile for time to be usable.
- Can be exported from a module - and then be available to other modules that import it.
Service
- Defined as providers. Not all providers are services.
- Common concept within software development and are not exclusive NestJS, JavaScript or back-end development.
- Singleton when wrapped with @Injectable() and provided to a module. That means, the same instance will be shared across the application acting as a single source of truth.
- The main source of business logic. For example, a service will be called from a controller to validate data, create an item in the database and return a response.
![]() |
| Service in action |
![]() |
| Dependency injection into controller |
Error handling and Validation
NestJS Pipes
- Pipes operate on the arguments to be processed by the route handler, just before the handler is called.
- Pipes can perform data transformation or data validation.
- Pipes can return data - either original or modified - which will be passed on to the route handler.
- Pipes can throw exceptions. Exceptions thrown will be handled by NestJS and parsed into an error response.
- Pipes can be asynchronous.
![]() |
| Router pipe example |
![]() | |||||
| Parameter pipe |
![]() |
| Global pipe |
![]() |
| pipe handling cycle |
TypeORM --- object related mapping
![]() |
| Comparing ORM and no ORM |
JSON Web Tokens JWT
What are JSON Web Tokens?
- Open-source industry standard (RFC-7519).
- Usable for Authorization or secure exchange of information between parties.
- Verify that the sender is who it/he/she claims to be.
- Signed by the issuer, using a secret or keypair (HMAC algorithm, RSA or ECDSA).
Commands log:
$ sudo npm install --global @nestjs/cli $ nest new <your nest project> $ nest g --help Usage: nest generate|g [options] <schematic> [name] [path] Generate a Nest element. Schematics available on @nestjs/schematics collection: ┌───────────────┬─────────────┬──────────────────────────────────────────────┐ │ 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 │ │ library │ lib │ Generate a new library within a monorepo │ │ 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 │ │ resource │ res │ Generate a new CRUD resource │ │ service │ s │ Generate a service declaration │ │ sub-app │ app │ Generate a new application within a monorepo │ └───────────────┴─────────────┴──────────────────────────────────────────────┘ 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) --spec-file-suffix [suffix] Use a custom suffix for spec files. --skip-import Skip importing (default: false) --no-spec Disable spec files generation. -c, --collection [collectionName] Schematics collection to use. -h, --help Output usage information. $ nest g module <your module name> $ nest g controller <your controller name> --no-spec # spec.ts file is used for jest $ nest g service <your service name> --no-spec # inside your project, and now you are preparing the validation $ npm install class-validator --save $ npm install class-transformer --save # inside your project, and now you are preparing database connection $ npm install typeorm --save $ npm i --save @nestjs/typeorm typeorm $ npm install pg
Reference:
1. Arielweinberger. (n.d.). GitHub - arielweinberger/nestjs-recipe at validation/creating-a-task. GitHub. https://github.com/arielweinberger/nestjs-recipe/tree/validation/creating-a-task
2. Arielweinberger. (n.d.-a). GitHub - arielweinberger/nestjs-recipe at s2-validation-and-error-handling. GitHub. https://github.com/arielweinberger/nestjs-course-task-management/tree/s2-validation-and-error-handling
3. Arielweinberger. (n.d.-b). GitHub - arielweinberger/nestjs-recipe at s10-testing. GitHub. https://github.com/arielweinberger/nestjs-recipe/tree/s10-testing
4. Arielweinberger. (n.d.-b). GitHub - arielweinberger/nestjs-recipe at s3-persistence. GitHub. https://github.com/arielweinberger/nestjs-course-task-management/tree/s3-persistence
5. Biango, C. (2023, December 23). NestJS Request Log Middleware + Trace ID. https://www.linkedin.com/pulse/nestjs-request-log-middleware-trace-id-cleuber-biango-w1rlf/
6. Cleuber. (n.d.). GitHub - Cleuber/nestjs-requests-log-middleware. GitHub. https://github.com/Cleuber/nestjs-requests-log-middleware













Comments
Post a Comment