REST CRUD Controllers
Relevant source files
The following files were used as context for generating this wiki page:
REST CRUD Controllers provide a code-first implementation of Representational State Transfer (REST) endpoints for Create, Read, Update, and Delete operations. Built on top of NestJS, this module automates the generation of standard API endpoints by integrating DTOs with underlying query services.
Sources: packages/query-rest/README.md:16-17, AGENTS.md:31-31
Architecture and Design
The system uses a factory pattern to compose controllers from specialized functional traits. The CRUDController factory assembles multiple controller interfaces into a single class that handles all standard persistence operations.
Component Composition
The controller assembly process aggregates the following functional modules:
- CreateController: Handles record creation.
- ReadController: Manages resource retrieval and filtering.
- UpdateController: Processes resource modifications.
- DeleteController: Handles resource removal.
- ExportController: Provides data export functionality.
Sources: packages/query-rest/src/controllers/crud.controller.ts:47-58
Controller Data Flow
The diagram below illustrates how a request flows through the REST controller to the service layer.
The CRUDController acts as a facade that delegates specific actions to internal controller implementations based on the request type.
Sources: packages/query-rest/src/controllers/crud.controller.ts:114-124
Configuration and Options
You configure controllers using the CRUDControllerOpts interface. This interface allows you to specify DTO classes, paging strategies, and individual options for each CRUD action.
Configuration Properties
| Property | Type | Description |
|---|---|---|
CreateDTOClass | Class<C> | DTO used as input for create endpoints. |
UpdateDTOClass | Class<U> | DTO used as input for update endpoints. |
pagingStrategy | PagingStrategies | Defines how results are paged (e.g., NONE, CURSOR, OFFSET). |
enableTotalCount | boolean | Enables total count metadata in read responses. |
basePath | string | Sets the base URL path for the controller. |
Sources: packages/query-rest/src/controllers/crud.controller.ts:15-45
Paging Strategies
The system supports different paging methods through the pagingStrategy option. This determines how the ReadController structures its response and handles pagination parameters.
Implementation Example
To implement a controller, you extend the result of the CRUDController factory. You must provide the DTO class and a QueryService instance.
import { Controller } from '@nestjs/common'
import { CRUDController } from '@ptc-org/nestjs-query-rest'
@Controller('todo-items')
export class TodoItemController extends CRUDController(TodoItemDTO) {
constructor(readonly service: TodoItemService) {
super(service)
}
}
Sources: packages/query-rest/src/controllers/crud.controller.ts:101-112
Controller Factory Logic
The CRUDController factory uses higher-order functions to wrap the base controller with additional functionality. It processes options through internal extraction functions to isolate relevant configurations for each component.
- extractCreateResolverOpts: Merges global options with specific create settings.
- extractReadResolverOpts: Configures paging and total count settings.
- extractUpdateResolverOpts: Isolates update DTO requirements.
- extractDeleteResolverOpts: Configures deletion parameters.
- extractExportResolverOpts: Sets up data export configurations.
Sources: packages/query-rest/src/controllers/crud.controller.ts:63-95
Summary
REST CRUD Controllers in nestjs-query provide a highly modular way to expose persistence layers via REST APIs. By utilizing a factory that composes create, read, update, and delete behaviors, developers can generate standardized endpoints while maintaining the flexibility to override DTOs and paging strategies. This module bridges the gap between NestJS controllers and the framework-independent QueryService primitives.
Sources: packages/query-rest/src/controllers/crud.controller.ts:114-124, AGENTS.md:31-31