@Crud() decorator is the core feature of NestJS CRUD. It automatically generates RESTful API routes for your controller based on the configuration you provide.
Basic usage
Apply the@Crud() decorator to your controller class to generate CRUD routes:
GET /users- Get many usersGET /users/:id- Get one userPOST /users- Create one userPOST /users/bulk- Create many usersPATCH /users/:id- Update one userPUT /users/:id- Replace one userDELETE /users/:id- Delete one user
Configuration options
The@Crud() decorator accepts a CrudOptions object with the following properties:
The entity model for this controller. Must include a
type property with your entity class.Custom DTOs for create, update, and replace operations.
Response serialization options for each route. Use DTOs to transform response data.
Query configuration including filtering, sorting, pagination, and joins.See Query options for details.
Route-specific configuration to customize or exclude individual routes.See Route options for details.
URL parameter configuration for path parameters.See CRUD options for details.
Validation pipe options or
false to disable validation.See Validation for details.Examples
With query configuration
With nested routes and params
With custom DTOs and serialization
With UUID primary key
Controller implementation
Your controller’s service must be exposed as a public property named
service. The CRUD decorator uses this to access your service methods.Minimal implementation
With CrudController interface
For better type safety, implement theCrudController interface:
Generated route names
The decorator generates these base route methods that you can override:getManyBase- Get multiple entitiesgetOneBase- Get a single entitycreateOneBase- Create a single entitycreateManyBase- Create multiple entities (bulk)updateOneBase- Update a single entity (partial)replaceOneBase- Replace a single entity (full)deleteOneBase- Delete a single entityrecoverOneBase- Recover a soft-deleted entity
Related topics
CRUD options
Detailed reference for all configuration options
Route options
Configure individual routes
Overriding routes
Customize generated route handlers
Validation
Configure request validation