I'm trying to create a generic crud controller that use a generic crud service in a NestJS application. It works properly but the swagger module doesn't generate the documentation about the REST services parameters correctly.
This is the service:
import { Model, Document } from "mongoose";
export abstract class CrudService<CrudModel extends Document, CreateDto, UpdateDto> {
constructor(protected readonly model: Model<CrudModel>) {}
async findAll(): Promise<CrudModel[]> {
return this.model.find().exec();
}
async create(dto: CreateDto): Promise<CrudModel> {
const createdDto = new this.model(dto);
return createdDto.save();
}
async update(id: any, dto: UpdateDto): Promise<CrudModel> {
return this.model.findOneAndUpdate({ _id: id }, dto, { new: true });
}
async delete(id: any): Promise<boolean> {
const deleteResult = await this.model.deleteOne({ _id: id });
return deleteResult.ok === 1 && deleteResult.deletedCount === 1;
}
}
This is the controller:
import { Body, Delete, Get, Param, Post, Put } from "@nestjs/common";
import { Document } from "mongoose";
import { CrudService } from "./crud-service.abstract";
export abstract class CrudController<CrudModel extends Document, CreateDto, UpdateDto> {
constructor(protected readonly service: CrudService<CrudModel, CreateDto, UpdateDto>) {}
@Get()
async findAll(): Promise<CrudModel[]> {
return this.service.findAll();
}
@Post()
async create(@Body() dto: CreateDto): Promise<CrudModel> {
return this.service.create(dto);
}
@Put(':id')
async update(@Param('id') id: string, @Body() dto: UpdateDto): Promise<CrudModel> {
return this.service.update(id, dto);
}
@Delete(':id')
async delete(@Param('id') id: string): Promise<boolean> {
return this.service.delete(id);
}
}
I found this issue on Github repo: https://github.com/nestjs/swagger/issues/86
The last comment mentions a solution using mixins but I can't figure it out how to adapt it to my needs
