Search code examples
javascripttypescriptnestjstypeorm

NestJs : How to implement a Controller using nestjsx/crud and manual implementation together


Under my Nestjs app , i am using @nestjsx/crud to handle the crud actions

But for some paths and some cases i need to implement the call for services manually which runs some sql queries.

My Controller would look like this

import {Controller, Get} from '@nestjs/common';
import { Crud, CrudController } from '@nestjsx/crud';
import { TodoEntity } from './todo.entity';
import { TodoService } from './todo.service';

@Crud({
  model: {
    type: TodoEntity
  }
})

@Controller('todo')
export class TodoController implements CrudController<TodoEntity> {
  constructor(public todoService: TodoService) {}

  // I WANT TO DO THIS in addition to the previous tremanet->
  @Get('/mypath')
  public async myManulaTreatment() {
    return await this.todoService.getManual();
  }
}

My service would look like this :

import { Injectable } from '@nestjs/common';
import { TypeOrmCrudService } from "@nestjsx/crud-typeorm";
import { TodoEntity } from './todo.entity';
import { InjectRepository } from '@nestjs/typeorm';
import {getManager , getConnection} from "typeorm";

@Injectable()
export class TodoService extends TypeOrmCrudService<TodoEntity>{
  constructor(@InjectRepository(TodoEntity) repo) {
    super(repo)
  }

  // THIS IS MY MANUAL TREATMENT
  async getManual(){
    const entityManager = await getConnection().manager.query("SELECT * FROM "+process.env.DATABASE_DB.mytable);
  }
}

But i got this error :

Class 'TodoController' incorrectly implements interface 'CrudController<TodoEntity>'.
  Property 'service' is missing in type 'TodoController' but required in type 'CrudController<TodoEntity>'.

Suggestions ?


Solution

  • You need to override the routes: https://github.com/nestjsx/crud/wiki/Controllers#routes-override

    Then you can inject services in constructor and use it in routes.

    Example:

    import {
      Crud,
      CrudController,
      Override,
      CrudRequest,
      ParsedRequest,
      ParsedBody,
      CreateManyDto,
    } from '@nestjsx/crud';
    
    @Crud({
      model: {
        type: Hero,
      }
    })
    @Controller('heroes')
    export class HeroesCrud implements CrudController<Hero> {
      constructor(public service: HeroesService) {}
    
      get base(): CrudController<Hero> {
        return this;
      }
    
      @Override()
      getMany(
        @ParsedRequest() req: CrudRequest,
      ) {
        return this.base.getManyBase(req);
      }
    
      @Override('getOneBase')
      getOneAndDoStuff(
        @ParsedRequest() req: CrudRequest,
      ) {
        return this.base.getOneBase(req);
      }
    
      @Override()
      createOne(
        @ParsedRequest() req: CrudRequest,
        @ParsedBody() dto: Hero,
      ) {
        return this.base.createOneBase(req, dto);
      }
    
      @Override()
      createMany(
        @ParsedRequest() req: CrudRequest,
        @ParsedBody() dto: CreateManyDto<Hero>
      ) {
        return this.base.createManyBase(req, dto);
      }
    
      @Override('updateOneBase')
      coolFunction(
        @ParsedRequest() req: CrudRequest,
        @ParsedBody() dto: Hero,
      ) {
        return this.base.updateOneBase(req, dto);
      }
    
      @Override('replaceOneBase')
      awesomePUT(
        @ParsedRequest() req: CrudRequest,
        @ParsedBody() dto: Hero,
      ) {
        return this.base.replaceOneBase(req, dto);
      }
    
      @Override()
      async deleteOne(
        @ParsedRequest() req: CrudRequest,
      ) {
        return this.base.deleteOneBase(req);
      }
    }