I want to get values from a relation of a relation – attribute value id, title and inStock only.
The query below returns all the other attributes of the product record in addition to the attribute values. This returns more information than required and uses more memory than needed.
Query:
const prodAttributes = await this.prodRepo.findOne({
select: {
attributes: {
id: true,
name: true,
attributeValues: {
id: true,
title: true,
inStock: true,
},
},
},
relations: ['attributes', 'attributes.attributeValues'],
where: {
model: p.item,
},
});
In the data definitions I have:
Product Attributes:
@OneToMany(
() => XTR_PROD_ATTRIBUTE,
(attr: XTR_PROD_ATTRIBUTE) => attr.product,
{
cascade: ['insert', 'update', 'soft-remove'],
},
)
attributes: XTR_PROD_ATTRIBUTE[];
From the attributes relation I have the attribute values;
@OneToMany(
() => XTR_ATTRIBUTE_VALUE,
(attrValue: XTR_ATTRIBUTE_VALUE) => attrValue.attribute,
{
cascade: true,
},
)
attributeValues: XTR_ATTRIBUTE_VALUE[];
The attribute values entity has the following:
id: number;
title: string;
inStock: boolean;