NestJS Chinese Documentation

website·Indexed 32 days ago

http://nestjs.inode.club

Chinese language documentation for NestJS version 10.x, featuring guides on installation, request lifecycles, and integration recipes for Prisma, MongoDB, TypeORM, Passport, and BullMQ. Includes detailed sections on OpenAPI, GraphQL subscriptions, microservices, and devtools for CI/CD integration.

Tokens
280K
Snippets
1.6K
Records
1.7K
Agent score
57%

What's inside NestJS

  1. Overview of GraphQL Federation in NestJS

    GraphQL Federation allows a monolithic GraphQL server to be split into independent microservices. It consists of a Gateway and one or more federated microservices (subgraphs). Each microservice maintains a portion of the schema, and the Gateway merges them into a single graph for the client.

    Key principles include declarative graph construction, separation of code by concern rather than type, and maintaining a simple product-centric graph for clients.

    Note: Federation currently does not support subscriptions.

  2. Overview of GraphQL Federation in NestJS

    GraphQL Federation allows splitting a monolithic GraphQL server into independent microservices. It consists of a gateway and one or more federated microservices (subgraphs). Each microservice maintains a portion of the schema, and the gateway merges them into a single graph for the client.

    Key principles include declarative graph construction, separation of code by concern rather than type, and maintaining a simple graph for the client.

    Warning: Federation currently does not support subscriptions.

  3. Overview of NestJS Framework

    NestJS is a progressive Node.js framework for building efficient, scalable server-side applications. It is built with and fully supports TypeScript (while allowing plain JavaScript) and combines elements of Object-Oriented Programming (OOP), Functional Programming (FP), and Functional Reactive Programming (FRP). By default, Nest uses Express as its HTTP server framework but can be configured to use Fastify. It provides an architectural layer over these frameworks while still exposing their underlying APIs.
  4. Overview of the NestJS GraphQL CLI Plugin

    The NestJS GraphQL CLI plugin enhances the TypeScript compilation process for code-first GraphQL approaches. It reduces boilerplate by automatically adding @Field() decorators to properties of input objects, object types, and argument classes.

    Key automated behaviors include:

    • Setting nullable: true if a property is marked as optional (e.g., name?: string).
    • Determining the GraphQL type based on the TypeScript type (including arrays).
    • Generating field descriptions from TSDoc comments if introspectComments is enabled.
    • Skipping properties marked with @HideField.

    To be analyzed by the plugin, files must have one of the following suffixes: .input.ts, .args.ts, .entity.ts, or .model.ts (configurable via typeFileNameSuffix).

    // Without plugin: Manual decorators required
    @ObjectType()
    export class Author {
      @Field(type => ID)
      id: number;
    
      @Field({ nullable: true })
      firstName?: string;
    }
    
    // With plugin: Automatic decorators
    @ObjectType()
    export class Author {
      @Field(type => ID) // Manual override still possible
      id: number;
      firstName?: string; // Plugin automatically adds @Field({ nullable: true })
    }
  5. Overview of Automock for NestJS unit testing

    Automock is a third-party library designed for NestJS unit testing. It uses the TypeScript reflection API (reflect-metadata) to automatically generate mock objects for a class's external dependencies, ensuring the unit under test is completely isolated from its providers in the DI container.
  6. Overview of NestJS Libraries in Monorepos

    In a NestJS monorepo, a library is a project that cannot run standalone and must be imported into an application to execute. Libraries provide a lightweight way to share code (such as a shared AuthModule) across multiple applications within the same organization or project boundary, ensuring all consumers use the latest version of the shared logic without needing to publish separate npm packages.
  7. Overview of Automock for NestJS unit testing

    Automock is a standalone library used for unit testing in NestJS. It leverages the TypeScript reflection API (reflect-metadata) to automatically generate mock objects for a class's external dependencies. This ensures that the unit under test is completely isolated from its providers in the Dependency Injection (DI) container, removing the need to manually rewrite every provider for every unit test.
  8. Overview of the NestJS Swagger CLI Plugin

    The NestJS Swagger CLI plugin enhances the TypeScript compilation process by automatically adding OpenAPI decorators to DTOs and controllers based on the Abstract Syntax Tree (AST). This reduces boilerplate code by automatically inferring properties, types, required status, and default values.

    Key automatic behaviors include:

    • Adding @ApiProperty to all DTO properties unless @ApiHideProperty is used.
    • Setting required: false for optional properties (e.g., name?: string).
    • Inferring type or enum (including arrays) from TypeScript types.
    • Setting default values based on assigned defaults.
    • Adding response decorators to endpoints with appropriate status and types.
    • Integrating with class-validator to set validation rules in the schema if classValidatorShim is enabled.

    Important Constraints:

    • Files must have the suffix .dto.ts or .entity.ts to be analyzed (configurable via dtoFileNameSuffix).
    • Runtime validation is NOT handled by the plugin; class-validator decorators (like @IsEmail()) are still required for actual validation.
    • When using mapped type utilities (e.g., PartialType), import them from @nestjs/swagger instead of @nestjs/mapped-types so the plugin can retrieve schema information.
  9. Overview of NestJS Framework

    v10

    NestJS is a progressive Node.js framework for building efficient and scalable server-side applications. It is built with TypeScript (while supporting pure JavaScript) and combines elements of Object-Oriented Programming (OOP), Functional Programming (FP), and Functional Reactive Programming (FRP).

    NestJS provides an abstraction layer over powerful HTTP server frameworks—using Express by default, with the option to switch to Fastify—while still exposing the underlying platform's APIs to allow the use of third-party modules.

  10. Overview of NestJS Pipes

    Pipes are classes decorated with @Injectable() that implement the PipeTransform interface. They are used to intercept arguments before they reach the route handler in a controller. Pipes have two primary use cases:

    1. Transformation: Converting input data to a desired output format (e.g., string to integer).
    2. Validation: Validating input data and throwing an exception if validation fails, preventing the controller method from executing.

    Pipes operate within the exception zone, meaning any thrown exceptions are handled by the core exception handler and any applicable exception filters.

  11. Overview of NestJS Framework

    Nest (NestJS) is a framework for building efficient, scalable Node.js server-side applications. It is built with and fully supports TypeScript (while allowing pure JavaScript) and combines elements of Object-Oriented Programming (OOP), Functional Programming (FP), and Functional Reactive Programming (FRP). By default, Nest uses Express as its HTTP server framework but can be configured to use Fastify. It provides an architectural layer over these frameworks while still exposing their underlying APIs.
  12. Understand the NestJS Request Lifecycle execution order

    NestJS processes incoming requests through a specific sequence of components. The general flow is: Middleware $\rightarrow$ Guards $\rightarrow$ Interceptors (pre-handler) $\rightarrow$ Pipes $\rightarrow$ Controller $\rightarrow$ Service $\rightarrow$ Interceptors (post-handler) $\rightarrow$ Exception Filters.

    Detailed execution order:

    1. Middleware: Global bound $\rightarrow$ Module bound.
    2. Guards: Global $\rightarrow$ Controller $\rightarrow$ Route.
    3. Interceptors (Pre-handler): Global $\rightarrow$ Controller $\rightarrow$ Route.
    4. Pipes: Global $\rightarrow$ Controller $\rightarrow$ Route $\rightarrow$ Route Parameter pipes.
    5. Controller: The method handler is executed.
    6. Service: Business logic execution (if applicable).
    7. Interceptors (Post-handler): Route $\rightarrow$ Controller $\rightarrow$ Global (LIFO order due to RxJS Observables).
    8. Exception Filters: Route $\rightarrow$ Controller $\rightarrow$ Global (LIFO order; only executes for unhandled exceptions).
    9. Server Response.