如何使用一個查詢(MySQL 和 TYPEORM 和 Nestjs)在 2 個不同儲存庫的欄位中搜尋單字
P粉122932466
P粉122932466 2024-02-26 15:52:00
0
1
292

我在 nestjs 最新版本中使用 MySQLTypeorm,並且我有以下實體:

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)
}

每個查詢都運作得很好,但我想做的是將這兩個查詢合併為一個查詢,這樣我可以從商店和報價中獲得搜尋結果,並對記錄進行排序,然後應用程式跳過並接受它們。 有什麼辦法可以做到嗎?

P粉122932466
P粉122932466

全部回覆(1)
P粉410239819

TypeORM 讓您可以隨心所欲地使用任何查詢。使用 entityManager.query() 這是文件。 p>

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!