nestjsx / crud

NestJs CRUD for RESTful APIs
https://github.com/nestjsx/crud/wiki
MIT License
4.04k stars 533 forks source link

Nested creating #752

Open whitered932 opened 2 years ago

whitered932 commented 2 years ago

Hello! I have such an entity -

import { IModeratable, IUpdatable } from '~/@common/entity';
import {
  Column,
  CreateDateColumn,
  Entity,
  ManyToOne,
  PrimaryGeneratedColumn,
  Unique,
  UpdateDateColumn,
} from '~/@core/orm';

import { IngredientEntity } from '~/ingredient/ingredient.entity';
import { RecipeEntity } from '~/recipe/recipe.entity';
import { Rule } from '~/rule/rule.types';
import { UnitEntity } from '~/unit/unit.entity';

@Unique(['recipe', 'unit', 'ingredient'])
@Entity()
export class RecipeIngredientEntity {
  @PrimaryGeneratedColumn()
  id: string;

  @Column()
  amount: number;

  @ManyToOne(() => RecipeEntity, (recipe) => recipe.ingredients)
  recipe: RecipeEntity;

  @ManyToOne(() => UnitEntity, (unit) => unit.recipeIngredients)
  unit: UnitEntity;

  @ManyToOne(
    () => IngredientEntity,
    (ingredient) => ingredient.recipeIngredients,
  )
  ingredient: IngredientEntity;
}

I know that i can create relations like this:

{
  "title": "Some title",
  "description": "Some description",
  "ingredients": [
        {"id": "some id"}
  ]
}

but I want to create a record like this:

{
  "title": "Some title",
  "description": "Some description",
  "ingredients": [
        {"unit": "some id", "amount": 12, "ingredient": "some id"}
  ]
}

Is there a built-in way? i tried this syntax

{"unit": "some id", "amount": 12, "ingredient": "some id"},
{"unitId": "some id", "amount": 12, "ingredientId": "some id"}