简体   繁体   English

NestJS 类型ORMCrudService

[英]NestJS TypeORMCrudService

I need to use one of the basic TypeORMCrudService methods, for example, createOne, I got the body as dto(I guess) but the second parameter is the CrudRequest.我需要使用基本的 TypeORMCrudService 方法之一,例如 createOne,我将主体作为 dto(我猜),但第二个参数是 CrudRequest。 Should I redefine its methods or what?我应该重新定义它的方法还是什么?

if I understand what you mean: if you use repositoryT it's so easy to do it:如果我明白你的意思:如果你使用 repositoryT 它很容易做到:

your controller should be like this:你的 controller 应该是这样的:

@Get(':id')
  async findOne(@Param('id', ParseIdPipe) id: number) {
    return await this.unitService.findOne(id);
  }

your service should be like this:你的服务应该是这样的:

  async findOne(id: number, ...relations: string[]): Promise<Entity> {
    const record = await this.repository.findOne(id, { relations });
    this.checkNotFound(record);
    return record;
  }

you can use:您可以使用:

const record = await this.repository.findOne(id, { relations });
const record = await this.repository.create({dto});
const record = await this.repository.find();
const record = await this.repository.delete(id);
const record = await this.repository.update(id, {dto});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM