Is there a way to addSelect with type-orm Find Options without createQueryBuilder?

1.4k Views Asked by At

Property.entity.ts

@Column({ select: false })
address: string;

Property.service.ts

allWithLocation = async () => {
    const properties = await this.repository
        .createQueryBuilder("property")
        .select("property")
        .addSelect("property.address")
        .getMany();
    return properties;
};

Is there a way to write the code above like this using type-orm find options?

allWithLocation = async () => {
    const properties = await this.repository.find({
        addSelect: ["address"]
    });
    return properties;
};
1

There are 1 best solutions below

0
Dmytro Sokhach On

Looks like you need to use "relations" property of FindOptionsRelations<T>.

Check here: https://orkhan.gitbook.io/typeorm/docs/find-options

allWithLocation = async () => {
    const properties = await this.repository.find({
        relations: {
           address: true
        }
    });
    return properties;
};