I have two little entities TodoEntity and CategoryEntity. It creates the tables that I expect in the database. I use Postgres. And I need to select from the database categories ordered by actual date. I can select all categories using QueryBuilder. But the problem is I don't need the categories to repeat. How to make them distinct?
.createQueryBuilder('todo')
.select('category')
.innerJoinAndSelect('todo.category', 'category')
.orderBy('todo.actualTime', 'ASC')
.getRawMany();
@Entity({ name: 'todo_entity' })
class TodoEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ name: 'name' })
name: string;
@Column({ name: 'description' })
description: string;
@Column({ name: 'actual_time', type: 'timestamp' })
actualTime: Date;
@ManyToOne(() => CategoryEntity, (categoryEntity) => categoryEntity.id, {
cascade: true,
})
category: CategoryEntity;
}
@Entity({ name: 'category_entity' })
class CategoryEntity {
@PrimaryGeneratedColumn({ name: 'id' })
id: number;
@Column({ name: 'title' })
@Index({ unique: true })
title: string;
}