我在 nestjs 最新版本中使用 MySQL 和 Typeorm,並且我有以下實體:
shop.entity.ts:
@Entity() export class Shop { @PrimaryGeneratedColumn("uuid") id: string; @Column({ unique: true, nullable: false }) name: string; @Column({ nullable: true }) description: string; @Column({ default: "" }) image_url: string; @Column({ default: true }) is_active: boolean; @Column({ default: false }) is_special: boolean; @Column({ nullable: false, default: () => "CURRENT_TIMESTAMP" }) created_at: Date; }
offer.entity.ts
@Entity() export class Offer { @PrimaryGeneratedColumn("uuid") id: string; @Column({ nullable: false }) name: string; @Column({ nullable: false }) fabric: string; @Column({ nullable: false }) code: string; @Column({ nullable: false }) start_date: Date; @Column({ nullable: false }) end_date: Date; @Column({ default: "" }) description: string; @Column({ nullable: false, default: () => "CURRENT_TIMESTAMP" }) created_at: Date; }
shop.service.ts 過濾查詢
async filter(filter: FilterShopDto) { const queryBuilder = this.shopRepository .createQueryBuilder("shop") .where( `shop.description LIKE :description`, { description: filter.description ? `%${filter.description}%` : "%", }, ) .orderBy("shop.created_at", "DESC") .skip(filter.skip) .take(filter.take) }
offer.service.ts 報價過濾器
async filter(filter: FilterOfferDto) { const queryBuilder = this.offerRepository .createQueryBuilder("offer") .where( " offer.description LIKE :description", { description: filter.description ? `%${filter.description}%` : "%", }, ) .orderBy( "offer.created_at", "DESC", ) .skip(filter.skip) .take(filter.take) }
每個查詢都運作得很好,但我想做的是將這兩個查詢合併為一個查詢,這樣我可以從商店和報價中獲得搜尋結果,並對記錄進行排序,然後應用程式跳過並接受它們。 有什麼辦法可以做到嗎?
TypeORM 讓您可以隨心所欲地使用任何查詢。使用 entityManager.query() 這是文件。 p>