How to select only single/multiple fields from joined entity in Typeorm How to select only single/multiple fields from joined entity in Typeorm database database

How to select only single/multiple fields from joined entity in Typeorm


const user = await createQueryBuilder("user")    .leftJoinAndSelect("user.photos", "photo")    .select(['user', 'photo.url', 'photo.alt'])    .where("user.name = :name", { name: "Timber" })    .getOne();

or

const user = await createQueryBuilder("user")    .leftJoinAndSelect("user.photos", "photo")    .addSelect(['photo.url', 'photo.alt'])    .where("user.name = :name", { name: "Timber" })    .getOne();

(not sure about the second one)


The first answer given by Art Olshansky is right (the second one doesn't), so, you must stand "the base" entity but, if you don't need all/any user fields you can just apply

const user = await createQueryBuilder("user")    .leftJoinAndSelect("user.photos", "photo")    .select(['user.id', 'photo.url', 'photo.alt'])    .where("user.name = :name", { name: "Timber" })    .getOne();