Я пытаюсь разработать небольшое приложение для записи кулинарных рецептов. Для этого я объявил 2 сущности с гнездом JS, что позволяет мне управлять рецептами, а другое - с ингредиентами. Я также создал третий объект для записи необходимого количества ингредиентов:
Диаграмма базы данных
// recipe.entity.js
@Entity()
export class Recipe {
@PrimaryGeneratedColumn()
id: number
@Column('datetime')
createdAt: Date
@Column('datetime')
updatedAt: Date
@Column('varchar', { length: 100 })
title: string;
@Column('varchar', {nullable: true})
image: string;
@OneToMany(type => RecipeIngredients, recipeIngredients => recipeIngredients.recipe)
ingredients: RecipeIngredients[];
}
// ingredient.entity.js
@Entity()
export class Ingredient {
@PrimaryGeneratedColumn()
id: number
@Column('datetime')
createdAt: Date
@Column('datetime')
updatedAt: Date
@Column('varchar', { length: 100 })
name: string;
@Column('varchar', {nullable: true})
image: string;
@OneToMany(type => RecipeIngredients, recipeIngredients => recipeIngredients.ingredient)
recipes: RecipeIngredients[];
}
// recipe_ingredients.entity.js
@Entity()
export class RecipeIngredients {
@PrimaryGeneratedColumn()
id: number
@ManyToOne(type => Recipe, recipe => recipe.ingredients)
recipe: Recipe
@ManyToOne(type => Ingredient)
ingredient: Ingredient
@Column()
quantity: string;
}
Во-первых, я бы хотел быть возможность получить рецепт со списком необходимых ингредиентов:
const recipe = await this.recipesRepository.createQueryBuilder('recipe')
.where('recipe.id = :recipeId', {recipeId: _id})
.leftJoin('recipe.ingredients', 'recipe_ingredients')
.leftJoin('recipe_ingredients.ingredient', 'ingredient')
.getMany();
Но этот метод возвращает только мой объект рецепта без ингредиентов ...
[
{
"id": 1,
"createdAt": "2020-04-30T09:12:22.000Z",
"updatedAt": "2020-04-30T09:12:22.000Z",
"title": "Test",
"image": null
}
]
Оттуда я потерял ... Как я могу получить список своих ингредиентов (по крайней мере, поля имени и количества) непосредственно из моего сервиса?
Заранее благодарю за помощь.