nestjsx / crud

NestJs CRUD for RESTful APIs
https://github.com/nestjsx/crud/wiki
MIT License
4.09k stars 539 forks source link

Suggestion: A better way to create crud controllers #690

Closed Char2sGu closed 3 years ago

Char2sGu commented 3 years ago

As said in the document, putting all the code in the @Crud() decorator will lose type hints, but we could implement that by building dynamic base classes and then extend them. (just like what I did in my own lib superest)

@Controller('path')
class CrudController<T> extends Crud(
    model: { type: MyEntity },
)</* generic types if needed */> {
    // using `@Crud()`, we must put `public service: MyCrudService` here, but now
    // we could use any param name and could make it private and readonly
    constructor(private readonly anyServiceName: MyCrudService) {
        super(anyServiceName);
    }

    // overriding
    getOneBase(
         @ParsedRequest() req: CrudRequest,
    ): Promise<T> {
        super.getOneBase(req);
        // ...
    }
}