Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

267
Vistas
Using entity inside entity with relationships and populating database in Nest JS / TypeORM

Let's say we are constructing our very first entities for a new web application. There is an entity for users:

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  username: string;

  @Column()
  password: string;

  @OneToOne(() => Author)
  author: Author;

Users can be Authors after registration. So there's a one-to-one relationship between users and authors table. Let's say we have another table called books, an author can have multiple books.

export class Author {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  Name: string;

  @OneToMany(() => Book, (Book) => Book.Author)
  Book: Book[];

  @OneToOne(() => User)
  @JoinColumn()
  User: User;

Here is a sample entity for Books repository:

export class Book {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  Name: string;

  @Column()
  ISIN: string;

  @OneToOne(() => Author)
  @JoinColumn()
  Author: Author;

The question is, when we migrated these entities and built our very first/clean database, how can we insert the data via calling API? A sample service for API Post method:

@Injectable()
export class BookService {
  constructor(
    @InjectRepository(Book)
    private BookRepository: Repository<Book>,
  ) {}
  async create(createBookDto: CreateBookDto) {
    await this.BookRepository.insert(createBookDto);
  }
}

And the Controller:

@Controller('books')
export class BookController {
  constructor(private readonly BookService: BookService) {}

  @Post('/Book')
  create(@Body() createBookDto: CreateBookDto) {
    return this.BookService.create(createBookDto);
  }
}

The problem here is that when I want to create a new book by POSTing data to the API route, it needs the Author to be defined. So how can I post existing user-books-author data into the database via this service?
The best option I think of is to create a new instance of the classes, get the data from request @Body and assign it to the objects of the class then save it to the database.
But I think it's not a good solution, as it's very preferred to use repositories instead of object-class type.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think you do a mistake in your entity definition. There is OneToMany relation between Author to Book.

Modify your book entity like this

export class Book {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  Name: string;

  @Column()
  ISIN: string;

  @ManyToOne(() => Author, author=> author.book, { nullable: true })
  @JoinColumn()
  Author: Author;

{ nullable: true } this will allow you to save your book data without having an author.

Modify your service to save the book information

public async create(createBookDTO: CreateBookDTO) {
        try {
            if (
                createBookDTO.hasOwnProperty('author') &&
                createTaskDTO.author
            ) {
                const author = await this.connection.manager.findOne(Author, {
                    where: {
                        id: createBookDTO.author,
                    },
                });
                if (author) {
                    createBookDTO.author = author;
                } else {
                    throw new NotAcceptableException('Author not found');
                }
            }

            return await this.bookRepo.save(createBookDTO);
        } catch (err) {
            throw new HttpException(err, err.status || HttpStatus.BAD_REQUEST);
        }
    }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda