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

414
Vistas
Nestjs typeorm relation order and limit elements

I am new to relations and typeorm. And now have some troubles with them. And now a little confused. Is it possible in typeorm sort relation by id or createdDate without mapping it and to have limit elements? My code example below:

person.entity

@Entity()
export class Person {
  @PrimaryGeneratedColumn()
  public id!: number;

  @Column({ type: 'varchar', length: 120 })
  public firstName: string;

  @Column({ type: 'varchar', length: 120 })
  public lastName: string;

  @Column({ type: 'varchar', length: 13 })
  public phone: string;

  @Column({ type: 'varchar', length: 500 })
  public place: string;

  @Column({ type: 'boolean', default: false })
  public isDeleted: boolean;

  @OneToMany(() => Event, (event) => event.person, {
    onDelete: 'SET NULL',
  })
  public events: Event[];

  @CreateDateColumn({ type: 'timestamp' })
  public createdAt!: Date;

  @UpdateDateColumn({ type: 'timestamp' })
  public updatedAt!: Date;
}

event.entity

@Entity()
export class Event {
  @PrimaryGeneratedColumn()
  public id!: number;

  @ManyToOne(() => Person, (person) => person.id, {
    onDelete: 'CASCADE',
  })
  public person!: Person;

  @ManyToOne(() => User, (user) => user.id, {
    onDelete: 'CASCADE',
  })
  public user!: User;

  @CreateDateColumn({ type: 'timestamp' })
  public createdAt!: Date;

  @UpdateDateColumn({ type: 'timestamp' })
  public updatedAt!: Date;
}

My code for sorting

  // get persons and sort events
  public async getALlPersons(): Promise<Person[]> {
    return this.repository
      .find({
        relations: ['events'],
        order: {
          createdAt: 'DESC',
        },
      })
      .then((persons) =>
        persons.map((person) => {
          person.events.sort((a, b) => b.id - a.id);
          return person;
        }),
      );
  }

And how to limit the number of events per person, for example I want to show only 10 events?

Thanks

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

0

There is a typeorm issue related to this: https://github.com/typeorm/typeorm/issues/89.

But in summary, it is not possible.

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