DTO Type Incompatible with Crud Type
See original GitHub issueHello, how can I use a DTO that is incompatible with the Crud Type? For example:
// appRequest.entity.ts
@Entity({ name: "AppRequest" })
export class AppRequest {
@Column("int)
id: number;
@Column("int")
updatedById?: number;
}
// appRequest.dto.ts
export class UpdateAppRequestDTO {
id: number;
updatedById: number;
}
// appRequest.controller.ts
@Crud({
dto: {
update: UpdateAppRequestDTO
},
model: {
type: AppRequest
},
})
@Controller("app-request")
export class AppRequestController implements CrudController<AppRequest> {
constructor(public service AppRequestService) {}
}
// appRequest.service.ts
@Injectable()
export class AppRequestService extends TypeOrmCrudService<AppRequest> {
constructor( @InjectRepository(AppRequest) private readonly appRequestRepository: Repository<AppRequest>) {
super(appRequestRepository);
}
async updateOne(req: CrudRequest, dto: UpdateAppRequestDTO) {
super.updateOne(req, dto);
}
}
Here is the error:
src/appRequest/appRequest.controller.ts:27:14 - error TS2420: Class 'AppRequestController' incorrectly implements interface 'CrudController<AppRequest>'.
The types of 'service.updateOne' are incompatible between these types.
Type '(req: CrudRequest, dto: UpdateAppRequestDTO) => Promise<AppRequest>' is not assignable to type '(req: CrudRequest, dto: AppRequest) => Promise<AppRequest>'.
Types of parameters 'dto' and 'dto' are incompatible.
Type 'AppRequest' is not assignable to type 'UpdateAppRequestDTO'.
Types of property 'appRequestRoles' are incompatible.
Type 'AppRequestRole[]' is not assignable to type 'UpdateAppRequestRoleDTO[]'.
Type 'AppRequestRole' is not assignable to type 'UpdateAppRequestRoleDTO'.
Types of property 'updatedById' are incompatible.
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
I know I can fix this by making the types match, but that doesn’t accurately reflect the logic I’m trying to implement. Must I create a custom route?
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
java - findById(ID) in CrudRepository - attempting to use ...
findById(ID) in CrudRepository - attempting to use incompatible return type ; private long ; public · getUser · user; } ; public ·...
Read more >CrudRepository (Spring Data Core 3.0.0 API)
Returns all instances of the type T with the given IDs. Optional<T>. findById(ID id). Retrieves an entity by its id.
Read more >Hibernate ORM 5.4.33.Final User Guide - Red Hat on GitHub
Custom SQL for CRUD (Create, Read, Update and Delete). 18. Spatial. 18.1. Overview; 18.2. Configuration. 18.2.1. Dependency; 18.2.2. Dialects. 18.3. Types.
Read more >Spring Boot findById - get entity by Id - ZetCode
Spring Boot findById tutorial shows how to retrieve an entity by its Id using CrudRepository's findById method. Spring is a popular Java/Kotlin ...
Read more >Spring Data - CrudRepository save() Method - Baeldung
CrudRepository is a Spring Data interface for generic CRUD operations on a repository of a specific type. It provides several methods out of ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The
CrudController
has methods which differ a bit from the equivalent methods inTypeOrmCrudService
.TypeOrmCrudService
declares that the dto is required only to be aDeepPartial
. Adding the following declaration into your project would reflect what’s actually happening. thecrud
package doesn’t have a dependency ontypeorm
- so it doesn’t feel appropriate to define this in thecrud
package. But I do think that it makes sense to extendCrudController
from inside@nestjsx/crud-typeorm
The ultimate solution to my question is to enable a capability where one can pass a
CreateEntityDTO
orUpdateEntityDTO
to theTypeOrmCrudService
likeTypeOrmCrudService<UserEntity, CreateUserEntity, UpdateUserEntity>
but in terms of priorities that’s probably pretty low. For others reading this issue, you’ll need to add an additional method on your service that’s not one of the reserved names.