Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

410
Views
TypeORM: consulta de muchos a muchos con propiedades personalizadas

todos Soy nuevo en TypeORM y necesito ayuda.

Intento crear relaciones de muchos a muchos con propiedades personalizadas como estos documentos aquí

Y aquí mis problemas.

Quiero un resultado de consulta como este ...

 { "id": 1, "username": "John Doe", "products": [ { "id": 1, "title": 'A shirt', "description": 'lorem ipsum' } ] }

Pero tengo...

 { "id": 1, "username": "John Doe", "products": [ { "id": 1, "userId": 1, "productId":1 } ] }

Aquí como consulto

 const user = await this.userRepository.findOne({ where: { id }, relations: ["products"], });

Aquí está mi código.

Entidad de producto de usuario

 // user-product.entity.ts @Entity() export class UserProduct extends BaseEntity { @PrimaryColumn() public id: number; @Column() public userId!: number; @Column() public productId!: number; @ManyToOne( () => User, (user) => user.products ) public user!: User; @ManyToOne( () => Product, (product) => product.users ) public product!: Product; }

Entidad de usuario

 // user.entity.ts @Entity() export class User extends BaseEntity { @PrimaryGeneratedColumn() id: number; @Column() username: string; @OneToMany(()=> UserProduct, userToProduct => userToProduct.user) public products!: UserProduct[]; }

Producto Entidad

 // product.entity.ts @Entity() export class Product extends BaseEntity { @PrimaryGeneratedColumn() id: number; @Column() title: string; @Column({ nullable: true }) subtitle: string; @Column({ nullable: true }) description: string; @OneToMany( () => UserProduct, (userProduct) => userProduct.product ) public users!: UserProduct[]; }

Me gustaría saber. ¿Cómo obtener un resultado como el anterior?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Creo que quieres saber cómo cargar las subrelaciones.

relaciones: las relaciones deben cargarse con la entidad principal. También se pueden cargar subrelaciones (abreviatura de join y leftJoinAndSelect)

Puede consultar este documento: Opciones de búsqueda

Puedes hacer esto:

 const user = await this.userRepository.findOne({ where: { id }, relations: ["products.product"], });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!