NestJs CRUD

repository·master·Indexed 26 days ago

https://github.com/nestjsx/crud

A microframework for NestJS that automates the creation of RESTful CRUD endpoints. It provides powerful query parsing for filtering, pagination, sorting, and relations across different databases. The project includes the core @nestjsx/crud package, @nestjsx/crud-request for request building and parsing, and @nestjsx/crud-typeorm for TypeORM-specific database operations.

Tokens
8.4K
Snippets
9
Records
70
Agent score
85%

What's inside @nestjsx/crud

  1. Overview of NestJs CRUD

    master
    NestJs CRUD is a microframework designed for building RESTful APIs with NestJS. It provides full-featured controllers and services that are database and service agnostic, allowing for rapid development of CRUD functionality.
  2. Configure joins and selection in TypeOrmCrudService

    master

    The TypeOrmCrudService uses the createBuilder method to translate CrudRequest options into a TypeORM SelectQueryBuilder.

    • Joins: You can specify joins via options.query.join. If a join is marked as eager: true in the configuration, it will be applied automatically. Otherwise, joins are applied based on the parsed.join array in the request.
    • Selection: You can restrict returned columns using query.fields or via options.allow/options.exclude. The service always ensures primary columns are included in the selection.
    • Soft Delete: If your entity contains a column marked with @DeleteDateColumn, the service supports soft deletion. You can toggle this behavior using options.query.softDelete and include deleted records using parsed.includeDeleted === 1.
  3. Configure TypeORM connection options

    master

    When setting up @nestjsx/crud-typeorm, you can define connection settings using TypeOrmModuleOptions. The configuration supports different database types (e.g., postgres, mysql) and can be driven by environment variables for connection type and logging behavior.

    Key configuration properties:

    • type: The database engine (e.g., 'postgres', 'mysql'). Can be set via process.env.TYPEORM_CONNECTION.
    • logging: Boolean flag to enable/disable logging. Can be controlled via process.env.TYPEORM_LOGGING (expects a numeric string like '1' for true).
    • entities: An array of paths to entity files (e.g., join(__dirname, './**/*.entity{.ts,.js}')).
  4. Identify the NestJs CRUD packages

    master

    The project is split into several specialized packages depending on your needs:

    • @nestjsx/crud: The core package. It provides the @Crud() decorator for automatic endpoint generation, global configuration, validation, and helper decorators.
    • @nestjsx/crud-request: A request builder/parser package. It includes RequestQueryBuilder for frontend usage and RequestQueryParser for backend handling and validation of query/path parameters.
    • @nestjsx/crud-typeorm: A TypeORM-specific package. It provides the TypeOrmCrudService which contains methods for standard CRUD database operations.
  5. Configure route parameter options with ParamsOptions

    master

    The ParamsOptions interface allows you to define configuration for route parameters using a dictionary where keys are parameter names and values are ParamOption objects. This is used to control how specific fields are handled in CRUD operations.

    Each ParamOption can specify:

    • field: The underlying database field name associated with the parameter.
    • type: The data type of the parameter (using ParamOptionType from @nestjsx/crud-request).
    • enum: An enumeration type for the parameter (using SwaggerEnumType).
    • primary: A boolean indicating if this is a primary parameter.
    • disabled: A boolean to disable the parameter.
    export interface ParamsOptions {
      [key: string]: ParamOption;
    }
    
    export interface ParamOption {
      field?: string;
      type?: ParamOptionType;
      enum?: SwaggerEnumType;
      primary?: boolean;
      disabled?: boolean;
    }
  6. Use TypeOrmCrudService for CRUD operations

    master

    The TypeOrmCrudService<T> is the TypeORM implementation of the CrudService. It provides asynchronous methods to perform standard CRUD operations using a TypeORM Repository.

    Key methods include:

    • getMany(req: CrudRequest): Retrieves multiple records based on the request parameters (filtering, sorting, pagination, joins).
    • getOne(req: CrudRequest): Retrieves a single record.
    • createOne(req: CrudRequest, dto: T | Partial<T>): Creates a single record.
    • createMany(req: CrudRequest, dto: CreateManyDto<T | Partial<T>>): Creates multiple records in bulk (using a chunk size of 50).
    • updateOne(req: CrudRequest, dto: T | Partial<T>): Updates an existing record.
    • replaceOne(req: CrudRequest, dto: T | Partial<T>): Replaces an existing record.
    • deleteOne(req: CrudRequest): Deletes a record (supports soft delete if the entity has a delete column and softDelete: true is in the query options).
    • recoverOne(req: CrudRequest): Recovers a soft-deleted record.