Overview
The@Override decorator is a method decorator that allows you to replace the auto-generated CRUD routes with your own custom implementations. This is useful when you need to add custom business logic, validation, or modify the default behavior of a CRUD operation.
Signature
Parameters
BaseRouteName
The name of the base route to override. If not provided, the decorator infers the route name from the method name by appending
Base.Possible values:getManyBase- Override the get many routegetOneBase- Override the get one routecreateOneBase- Override the create one routecreateManyBase- Override the create many routeupdateOneBase- Override the update one routereplaceOneBase- Override the replace one routedeleteOneBase- Override the delete one routerecoverOneBase- Override the recover one route
Usage
Implicit Route Name
When you don’t specify a route name, the decorator automatically infers it from your method name:getMany automatically maps to overriding getManyBase.
Explicit Route Name
You can explicitly specify which route to override:Adding Business Logic
Modifying Response
Using with Authentication
Complete Integration Test Example
From the source tests:packages/crud/test/crud.decorator.override.spec.ts:25-56
Implementation Details
The decorator stores metadata about the override:packages/crud/src/decorators/override.decorator.ts:4-11
name is provided, the decorator uses the method name and appends Base to determine which route to override.
When you override a route, you still have access to the base implementation through the
base property if you implement the CrudController interface. This allows you to add logic before or after the base implementation.Tips
- Use
@ParsedRequest()to access the parsed CRUD request with filters, pagination, and joins - Use
@ParsedBody()to access the validated and transformed request body - Implement
CrudController<T>to get access to the base methods viathis.base - Validation still applies - DTOs are validated even in overridden routes
- Swagger documentation is automatically generated for overridden routes
See Also
- @ParsedRequest - Access parsed request data
- @ParsedBody - Access parsed request body
- @Crud - Main CRUD decorator
- Controllers - Controller concepts and usage