Search code examples
typescriptormtypeorm

Is there a way to exclude fields from a TypeORM relations (or include certain fields)


I have some OneToMany and ManyToOne relationships defined in my TypeORM, which are working as expected; within my find I can select certain columns from the original table with the 'select statement thus::

  return await this.namedRepository.find({
    select: ["ua_id", "ua_linkid_agent", "ua_linkid_usr"],
    where: { ua_linkid_usr: usr_id },
    relations: ["ua_agent"],

Is there a way to limit the fields coming back from the array in "ua_agent", (for example "ua_id" and "ua_status") without having to resort to the select:false criteria on all the other @Column()s in the entity file?


Solution

  • you can do it using querybuilder as followes

    this.namedRepository.createQueryBuilder('your_entity')
        .leftJoinAndSelect('your_entity.ua_agent', 'ua_agent')
        .select(['ua_id', 'ua_status', 'ua_agent.whatevercolumn'])
        .getMany()
    

    with querybuilder you can select only what you need.