My Entity definition currently looks like this:
export class ItemEntity implements Item {
@PrimaryColumn()
@IsIn(['product', 'productVariant', 'category'])
@IsNotEmpty()
itemType: string;
@PrimaryColumn()
@IsUUID()
@IsNotEmpty()
itemId: string;
@OneToOne(() => ProductEntity, product => product.id, { nullable: true })
@JoinColumn()
@Expose({ name: 'bundleItem' })
producttItem: ProductEntity;
@OneToOne(() => ProductVariantEntity, variant => variant.id, {
nullable: true,
})
@JoinColumn()
@Expose({ name: 'bundleItem' })
variantItem: ProductVariantEntity;
@OneToOne(() => CategoryEntity, category => category.id, { nullable: true })
@JoinColumn()
@Expose({ name: 'bundleItem' })
categoryItem: CategoryEntity;
}
My use of the @Expose()
decorator is to ensure that only one of productItem
, variantItem
, or categoryItem
is returned as a single bundleItem
field in the response. Only one of them should have a value, not two or three.
However, upon performing a GET request on the ItemEntity's controller, the desired effect is applied only to the first item, not the rest:
[
{
"itemType": "category",
"itemId": ""
"bundleItem": {
"categoryType": "Custom",
"description": "First custom category",
"id": "e00ad76c-95d3-4215-84b1-de17c7f1f82c",
"name": "Category A",
"updatedAt": "2023-02-24T08:49:22.913Z"
}
},
{
"itemType": "variant",
"itemId": "",
"bundletem": null
}
]
I aim to extend this effect to all items in the response array. Currently, the other items are returning as null
. In essence, I want the response to include a bundleItem
field regardless of the itemType
(be it productItem
, variantItem
, or categoryItem
). Is it possible to achieve this using `class-transformer`?
Thank you.