@Crud() decorator automatically generates route handlers for common CRUD operations.
Basic controller
A minimal CRUD controller requires just three things:- The
@Crud()decorator with model configuration - The
@Controller()decorator with a route path - A public
serviceproperty
The service must be injected as a public property named
service. This is required for the framework to automatically connect route handlers to service methods.Configuration options
The@Crud() decorator accepts a configuration object with several options:
Model configuration
Define the entity model that the controller operates on:Query options
Control which fields can be queried, filtered, and joined:Route customization
Customize individual route behaviors:/home/daytona/workspace/source/integration/crud-typeorm/companies/companies.controller.ts:14-18:
Serialization
Control response serialization with DTOs:Joins and relations
Configure how related entities are loaded and exposed:/home/daytona/workspace/source/integration/crud-typeorm/users/users.controller.ts:31-43:
Join options
alias- SQL alias for the joined tableallow- Fields that can be selected from the relationexclude- Fields to hide from the relationeager- Auto-load the relation on every requestselect- Set tofalseto disable selecting fields (join only for filtering)required- UseINNER JOINinstead ofLEFT JOINpersist- Fields always included in the response
Eager joins are loaded automatically even if not requested. Use sparingly to avoid performance issues.
Path parameters
Define route parameters for nested resources:GET /companies/1/users- Get all users for company 1GET /companies/1/users/5- Get user 5 from company 1POST /companies/1/users- Create user in company 1
/home/daytona/workspace/source/integration/crud-typeorm/users/users.controller.ts:18-28:
Parameter options
field- Entity field to filter bytype- Parameter type ('number','uuid','string')primary- Whether this is the primary keydisabled- Exclude from route generation
UUID parameters
For entities with UUID primary keys:/home/daytona/workspace/source/integration/crud-typeorm/devices/devices.controller.ts:12-18:
Overriding routes
Customize generated route handlers using the@Override() decorator:
/home/daytona/workspace/source/integration/crud-typeorm/users/users.controller.ts:55-58:
Available route names
getManyBase- GET collectiongetOneBase- GET single resourcecreateOneBase- POST single resourcecreateManyBase- POST bulk resourcesupdateOneBase- PATCH resourcereplaceOneBase- PUT resourcedeleteOneBase- DELETE resourcerecoverOneBase- PATCH recover soft-deleted resource
Swagger integration
The framework automatically generates Swagger/OpenAPI documentation. Enhance it with@ApiTags():
/home/daytona/workspace/source/integration/crud-typeorm/companies/companies.controller.ts:45-49:
Complete example
Here’s a full-featured controller with all options:/home/daytona/workspace/source/integration/crud-typeorm/companies/companies.controller.ts:1-50:
Next steps
Services
Learn how to implement CRUD services
Requests
Understand request parsing and filtering