title: Using entity inside entity with relationships and populating database in Nest JS / TypeORM
body: <p>Let's say we are constructing our very first entities for a new web application.
  There is an <strong>entity for users</strong>:</p>
  <pre><code>@Entity()
  export class User {
    @PrimaryGeneratedColumn()
    id: number;
  
    @Column()
    username: string;
  
    @Column()
    password: string;
  
    @OneToOne(() =&gt; Author)
    author: Author;
  </code></pre>
  <p><strong>Users can be Authors</strong> after registration. So there's a one-to-one relationship between users and authors table. Let's say we have another <strong>table called books</strong>, an author can have <strong>multiple</strong> books.</p>
  <pre><code>export class Author {
    @PrimaryGeneratedColumn()
    id: number;
  
    @Column()
    Name: string;
  
    @OneToMany(() =&gt; Book, (Book) =&gt; Book.Author)
    Book: Book[];
  
    @OneToOne(() =&gt; User)
    @JoinColumn()
    User: User;
  </code></pre>
  <p>Here is a sample entity for Books repository:</p>
  <pre><code>export class Book {
    @PrimaryGeneratedColumn()
    id: number;
  
    @Column()
    Name: string;
  
    @Column()
    ISIN: string;
  
    @OneToOne(() =&gt; Author)
    @JoinColumn()
    Author: Author;
  </code></pre>
  <p>The question is, when we migrated these entities and built our <strong>very first/clean database</strong>, how can we <strong>insert the data</strong> via calling API?
  A sample service for API Post method:</p>
  <pre><code>@Injectable()
  export class BookService {
    constructor(
      @InjectRepository(Book)
      private BookRepository: Repository&lt;Book&gt;,
    ) {}
    async create(createBookDto: CreateBookDto) {
      await this.BookRepository.insert(createBookDto);
    }
  }
  </code></pre>
  <p>And the Controller:</p>
  <pre><code>@Controller('books')
  export class BookController {
    constructor(private readonly BookService: BookService) {}
  
    @Post('/Book')
    create(@Body() createBookDto: CreateBookDto) {
      return this.BookService.create(createBookDto);
    }
  }
  </code></pre>
  <p>The problem here is that when I want to <strong>create a new book</strong> by POSTing data to the API route, it needs the Author to be defined. So how can I post <strong>existing user-books-author data</strong> into the database via this service? <br />
  The best option I think of is to create a <strong>new instance of the classes</strong>, get the data from <strong>request @Body</strong> and assign it to the objects of the class then save it to the database.<br />
  But I think it's not a good solution, as it's very preferred to <strong>use repositories</strong> instead of object-class type.</p>