Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

40
Vistas
Pagination without id column in TypeORM

Is there any way of selecting only given columns without id column and using pagination (skip, take) in TypeORM. I'm doing this in NestJS:

article.service.ts:

async getAll(skip: number): Promise<Article[]> {
  return await this.articleRepository.find({
    select: {
      id: false,
      title: true,
      description: true,
      body: false,
      created_at: true,
      updated_at: false,
      author: {
        full_name: true
      }
    },
    relations: {
      author: true
    },
    skip: skip,
    take: 10,
    order: {
      created_at: 'DESC'
    }
  });
}

And in article.controller.ts:

@Get('articles')
async getAll(@Query('page') page: number) {
  if(!page) { page = 1; }
  const skip = (page - 1) * 10;

  const articles = await this.articlesService.getAll(skip);
  return {
    status: true,
    articles: articles,
    page: page,
  }
}

My article.entity.ts:

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

  @Column({ length: 100 })
  title: string;

  @Column()
  description: string

  @Column({ type: 'text' })
  body: string;

  @ManyToOne(() => User, (user) => user.articles)
  author: User;

  @CreateDateColumn()
  created_at: string;

  @UpdateDateColumn()
  updated_at: string;
}

When I send a request to this endpoint error happens. I think this is because of pagination(skip, take).

QueryFailedError: column distinctAlias.Article_id does not exist
    at PostgresQueryRunner.query (project/src/driver/postgres/PostgresQueryRunner.ts:299:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at SelectQueryBuilder.loadRawResults (project/src/query-builder/SelectQueryBuilder.ts:3601:25)
    at SelectQueryBuilder.getRawMany (project/src/query-builder/SelectQueryBuilder.ts:1573:29)
    at SelectQueryBuilder.executeEntitiesAndRawResults (project/src/query-builder/SelectQueryBuilder.ts:3295:26)
    at SelectQueryBuilder.getRawAndEntities (project/src/query-builder/SelectQueryBuilder.ts:1617:29)
    at SelectQueryBuilder.getMany (project/src/query-builder/SelectQueryBuilder.ts:1707:25)
    at ArticlesService.getAll (project/src/articles/articles.service.ts:35:12)
    at ArticlesController.getAll (project/src/articles/articles.controller.ts:56:22)
    at project/node_modules/@nestjs/core/router/router-execution-context.js:46:28

But if I remove skip and take or select the id column it works. Is there any way that I can select other columns without id column and use pagination together

4 months ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Solved issue temporarily by selecting id columns and removing them before returning

articles.forEach(article => { delete article.id });

Here is the full function from article.service.ts:

async getAll(skip: number): Promise<Article[]> {
  const articles = await this.articleRepository.find({
    select: {
      id: true,
      title: true,
      description: true,
      body: false,
      created_at: true,
      updated_at: false,
      author: {
        full_name: true
      }
    },
    relations: {
      author: true
    },
    skip: skip,
    take: 10,
    order: {
      created_at: 'DESC'
    }
  });
  articles.forEach(article => { delete article.id });
  return articles;
}

NOTE: There should be a better solution though.

4 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.