0

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 ?

2 Answers 2

1

Instead of calling your TodoService todoService, you should just call it service and make sure to pass it to super(service) to make sure the base class has its proper dependencies too. Here's the docs for adding-routes from nestjsx/crud

Sign up to request clarification or add additional context in comments.

Comments

-2

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);
  }
}

1 Comment

That's not what he needs to do. He wants to keep the existing functionality AND add a new endpoint

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.