First, here is a sort of Diagram of my database and my shared DTOs
The issue is that, when I want to map my Recipe to a RecipeDTO, I can't get the quantity field.
recipe.repository.ts
@Injectable()
export class RecipeRepository {
public async getAll(): Promise<Recipe[]> {
return Recipe.findAll({
include: [Item]
});
}
public async create(recipe: Recipe) {
try {
await Recipe.create(recipe);
return true;
} catch(err) {
console.log(err);
return false;
}
}
}
recipe.model.ts
@Table({tableName: 'RECIPE'})
export class Recipe extends BaseModel<Recipe> {
@Column({field: 'NAME'})
name: string
@Column({field: 'IMAGE'})
image: string
@BelongsToMany(() => Item, () => ItemRecipe)
items: Item[]
}
item.model.ts
@Table({tableName: 'ITEM'})
export class Item extends BaseModel<Item> {
@Column({field: 'NAME'})
name: string
@Column({field: 'IMAGE'})
image: string
@BelongsToMany(() => Recipe, () => ItemRecipe)
recipes: Recipe[]
}
item-recipe.model.ts
@Table({tableName: 'ITEM_RECIPE'})
export class ItemRecipe extends BaseModel<ItemRecipe> {
@Column({field: 'QUANTITY'})
quantity: number
@ForeignKey(() => Item)
@Column({field: 'ITEM_ID'})
itemId: number
@ForeignKey(() => Recipe)
@Column({field: 'RECIPE_ID'})
recipeId: number
}
recipe.mapper.ts
@Injectable()
export class RecipeMapper {
constructor(private itemMapper: ItemMapper) {}
toDto(recipe: Recipe): RecipeDto {
return {
name: recipe.name,
image: recipe.image,
itemRecipe: recipe.items.map(item => {
return {
item: this.itemMapper.toDto(item),
quantity: item.itemRecipe.quantity <--------- itemRecipe DOES NOT EXISTS
} as ItemRecipeDto
})
} as RecipeDto
}
}
Finaly, here is an example of JSON that I currently get when I'm not using my mapper (so the returned object is Recipe and not RecipeDTO, and we can see that the ItemRecipe is under each Item):
[
{
"id": 1,
"createdAt": "2021-10-31T19:26:26.507Z",
"updatedAt": "2021-10-31T19:26:26.507Z",
"name": "Bouillon de Chair",
"image": "69741.png",
"items": [
{
"id": 1,
"createdAt": "2021-10-31T19:26:26.507Z",
"updatedAt": "2021-10-31T19:26:26.507Z",
"name": "Viande Intangible",
"image": "63407.png",
"ItemRecipe": {
"id": 1,
"createdAt": "2021-10-31T19:26:26.507Z",
"updatedAt": "2021-10-31T19:26:26.507Z",
"quantity": 1,
"itemId": 1,
"recipeId": 1
}
}
]
},
{
"id": 2,
"createdAt": "2021-10-31T19:26:26.507Z",
"updatedAt": "2021-10-31T19:26:26.507Z",
"name": "Boulette de Viande",
"image": "69750.png",
"items": [
{
"id": 2,
"createdAt": "2021-10-31T19:26:53.832Z",
"updatedAt": "2021-10-31T19:26:53.832Z",
"name": "Viande Hachée",
"image": "175008.png",
"ItemRecipe": {
"id": 2,
"createdAt": "2021-10-31T19:26:26.507Z",
"updatedAt": "2021-10-31T19:26:26.507Z",
"quantity": 1,
"itemId": 2,
"recipeId": 2
}
}
]
},
{
"id": 3,
"createdAt": "2021-10-31T19:26:26.507Z",
"updatedAt": "2021-10-31T19:26:26.507Z",
"name": "Beignet Astrubien",
"image": "69115.png",
"items": [
{
"id": 3,
"createdAt": "2021-10-31T19:27:27.044Z",
"updatedAt": "2021-10-31T19:27:27.044Z",
"name": "Viande Faisandée",
"image": "63348.png",
"ItemRecipe": {
"id": 3,
"createdAt": "2021-10-31T19:26:26.507Z",
"updatedAt": "2021-10-31T19:26:26.507Z",
"quantity": 1,
"itemId": 3,
"recipeId": 3
}
},
{
"id": 21,
"createdAt": "2021-10-31T20:32:03.815Z",
"updatedAt": "2021-10-31T20:32:03.815Z",
"name": "Blé",
"image": "34009.png",
"ItemRecipe": {
"id": 4,
"createdAt": "2021-10-31T19:26:26.507Z",
"updatedAt": "2021-10-31T19:26:26.507Z",
"quantity": 1,
"itemId": 21,
"recipeId": 3
}
}
]
}
]
So in this Many-to-Many association, how can I access the associative model from my Recipe model ?