I am currently working with an entity called BookEntity:
@Entity()
export class BookEntity {
@PrimaryGeneratedColumn()
@Generated("increment")
public id: number
@CreateDateColumn()
public createdAt: Date
@Column()
public title: string
@Column()
public author: string
@Column({ type: "simple-array" })
public category: string[]
@Column({ default: "" })
public cover: string
@Column()
public issueDate: string
@Column({ default: 0 })
public liked: number
@Column({ default: 0 })
public downloads: number
@Column({ default: 0 })
public views: number
@Column({ type: "simple-array" })
public path: string
}
My goal is to filter entities based on the value in the category column. For example, if an entity has categories ["test1", "test2", "test3"], I would like to retrieve all entities with the category "test2". Can someone guide me on how to achieve this?
I have already implemented a service to fetch a paginated list of all book_entity:
public async getBooks(page: number): Promise<PageDto<BookEntity>> {
try {
const queryBuilder = this.bookRepository.createQueryBuilder("book_entity")
queryBuilder.take(10)
const itemCount = await queryBuilder.getCount()
const { entities } = await queryBuilder.getRawAndEntities()
const pageData = new PageDto<BookEntity>(entities, itemCount, page)
return pageData
} catch (error) {
console.log("Getting books error: ", error)
}
}
However, I am unsure about how to modify this service to filter by category. I understand that the queryBuilder has an .orderBy method, but I am uncertain about its usage for my specific requirement.