question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DTO Type Incompatible with Crud Type

See original GitHub issue

Hello, 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:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
tbrannamcommented, Aug 18, 2020

The CrudController has methods which differ a bit from the equivalent methods in TypeOrmCrudService. TypeOrmCrudService declares that the dto is required only to be a DeepPartial. Adding the following declaration into your project would reflect what’s actually happening. the crud package doesn’t have a dependency on typeorm - so it doesn’t feel appropriate to define this in the crud package. But I do think that it makes sense to extend CrudController from inside @nestjsx/crud-typeorm

declare module '@nestjsx/crud' {
  interface CrudController<T> {
    createOneBase?(req: CrudRequest, dto: DeepPartial<T>): Promise<T>;
    createManyBase?(req: CrudRequest, dto: CreateManyDto<DeepPartial<T>>): Promise<T[]>;
    updateOneBase?(req: CrudRequest, dto: DeepPartial<T>): Promise<T>;
    replaceOneBase?(req: CrudRequest, dto: DeepPartial<T>): Promise<T>;
  }
}
0reactions
bestickleycommented, Jun 25, 2020

The ultimate solution to my question is to enable a capability where one can pass a CreateEntityDTO or UpdateEntityDTO to the TypeOrmCrudService like TypeOrmCrudService<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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found