title: Correctly Saving and Updating Entites in Netsjs+TypeORM
body: <p>I've got a Question regarding TypeORM-Relations and how to use them 'nest-like'.</p>
  <p>Suppose I have two Entities defined <code>ChildEntity</code> and <code>TestEntity</code>, which are related.</p>
  <p><code>TestEntity</code>:</p>
  <pre class="lang-js prettyprint-override"><code>import { ChildEntity } from 'src/modules/child-entity/entities/child-entity.entity';
  import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
  
  @Entity()
  export class TestEntity {
    @PrimaryGeneratedColumn()
    id: number;
  
    @Column('varchar')
    name: string;
  
    @ManyToOne(() =&gt; ChildEntity, (childEntity) =&gt; childEntity.testEntities)
    childEntity: ChildEntity;
  
    constructor(name: string, childEntity: ChildEntity) {
      this.name = name;
      this.childEntity = childEntity;
    }
  }
  
  </code></pre>
  <p>My first question occurs when I want to create the entity.  I have to first translate the passed <code>childEntityId</code> into a <code>ChildEntity</code>, which I can pass to the constructor:</p>
  <p><code>CreateTestEntityDto</code></p>
  <pre class="lang-js prettyprint-override"><code>import { ApiProperty } from '@nestjs/swagger';
  import { IsNotEmpty, IsNumber } from 'class-validator';
  
  export class CreateTestEntityDto {
    @ApiProperty()
    @IsNotEmpty()
    name: string;
  
    @ApiProperty()
    @IsNumber()
    childEntityId: number;
  
    constructor(name: string, childEntityId: number) {
      this.name = name;
      this.childEntityId = childEntityId;
    }
  }
  
  </code></pre>
  <pre class="lang-js prettyprint-override"><code>  async create(createTestEntityDto: CreateTestEntityDto) {
      const { name, childEntityId } = createTestEntityDto;
      const childEntity = await this.childEntityService.findOne(childEntityId);
      const testEntity = new TestEntity(name, childEntity);
      return this.testEntityRepo.save(testEntity);
    }
  </code></pre>
  <p>Is there a way to just pass the <code>childEntityId</code> to the <code>save()</code>-Method without explicitly looking for the <code>ChildEntity</code> beforehand?</p>
  <hr />
  <p>The Second problem occurs when updating.</p>
  <p><code>UpdateTestEntityDto</code></p>
  <pre class="lang-js prettyprint-override"><code>import { PartialType } from '@nestjs/swagger';
  import { CreateTestEntityDto } from './create-test-entity.dto';
  
  export class UpdateTestEntityDto extends PartialType(CreateTestEntityDto) {}
  </code></pre>
  <p>As updating only a partial Entity is possible I have to check if the Id is even passed along the request and if it is I have to retrieve the correct Entity for the update. Is there a more streamlined way to do this?</p>
  <pre class="lang-js prettyprint-override"><code>  async update(id: number, updateTestEntityDto: UpdateTestEntityDto) {
      const { name, childEntityId } = updateTestEntityDto;
      const props = { name };
      if (childEntityId) {
        props['childEntity'] = await this.childEntityService.findOne(
          childEntityId,
        );
      }
      return this.testEntityRepo.update(id, props);
    }
  </code></pre>