简体   繁体   English

如何从关系中只获取列名

[英]How to get only column name from relation

How to join column, that when i launch my function, he return me only columns name instead of entity_columnName .如何加入列,当我启动我的函数时,他只返回列名而不是entity_columnName

I'm using TypeORM, and i try this;我正在使用 TypeORM,我试试这个;

const data = this.conn.getRepository(User).createQueryBuilder('user');
data.leftJoinAndSelect('user.orders', 'orders');
data.getRawMany();

but return me:但回我:

firstName: ...
lastName: ...
age: ...
order_name: ...
order_price: ...

instead of:代替:

firstName: ...
lastName: ...
age: ...
name: ...
price: ...

can someone tell me how do this?有人能告诉我怎么做吗? thanks for any help谢谢你的帮助

How are your User and Order entities defined?您的用户和订单实体是如何定义的?

If you define a relation (eg OneToMany) with the eager: true option, then TypeORM will automatically include the related entities when you query using the repository *find methods.如果您使用eager: true选项定义关系(例如OneToMany),那么当您使用存储库*find方法进行查询时,TypeORM 将自动包含相关实体。 It won't do this when you use the QueryBuilder, where you have to add them such as with leftJoinAndSelect() (as you have done).当您使用 QueryBuilder 时它不会这样做,您必须在其中添加它们,例如使用leftJoinAndSelect() (就像您所做的那样)。

An example from an Invoice entity that has OneToMany line items:来自具有 OneToMany 行项目的 Invoice 实体的示例:

  @OneToMany(
    () => InvoiceLineItem,
    (item: InvoiceLineItem) => item.invoice,
    { eager: true }
  )
  items: InvoiceLineItem[]

Per the example, if I were then to find() or findMany() invoices, then the related objects, items will be included as well because eager: true .根据示例,如果我当时要find()findMany()发票,那么相关的对象、 items也将包括在内,因为eager: true

This behaviour might translate well to your situation with users and orders.这种行为可能会很好地转化为您的用户和订单情况。

Also be aware of the differences between getMany() and getRawMany() when using the query builder.在使用查询构建器时,还要注意getMany()getRawMany()之间的区别。

If you use getMany() then TypeORM will automagically give you entities back (eg you'd get an instance of User with property orders that is an array of Order instances).如果你使用getMany()然后TypeORM会自动地给你回的实体(例如,你会得到的一个实例, User财产orders是数组Order情况)。 The property names will be correct.属性名称将是正确的。

Since you added the NestJS tag to your question, also understand serialization: https://docs.nestjs.com/techniques/serialization由于您在问题中添加了 NestJS 标签,因此还要了解序列化: https ://docs.nestjs.com/techniques/serialization

There is a built-in ClassSerializerInterceptor that comes with NestJS that you might find useful. ClassSerializerInterceptor附带一个内置的 ClassSerializerInterceptor,您可能会发现它很有用。

In your controller you can decorate the class or any of its methods eg在您的控制器中,您可以装饰类或其任何方法,例如

@UseInterceptors(ClassSerializerInterceptor)

This will transform the response to JSON, and will apply the rules specified with class-transformer decorators on the entity/DTO class.这会将响应转换为 JSON,并将在实体/DTO 类上应用由class-transformer装饰器指定的规则。

If you were to use this interceptor, the response sent to the client will have your desired property names.如果您要使用此拦截器,则发送到客户端的响应将具有您想要的属性名称。

If you really want to modify the response that your client gets back, you can also look into writing your own Interceptor.如果您真的想修改客户端返回的响应,您还可以考虑编写自己的拦截器。

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

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