search

Home  >  Q&A  >  body text

How to search for a word in a column of 2 different repositories using one query (MySQL with TYPEORM and Nestjs)

I'm using MySQL and Typeorm in the latest version of nestjs and I have the following entities:

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 Filter query

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 Quote Filter

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

Each query is working fine, but what I want to do is combine these two queries into one query so that I can get the search results from the store and the offer and sort the records and then apply skip and accept them. Is there any way to do this?

P粉122932466P粉122932466360 days ago352

reply all(1)I'll reply

  • P粉410239819

    P粉4102398192024-02-27 10:39:41

    TypeORM enables you to use any query you want. Use entityManager.query() This is documentation.

    reply
    0
  • Cancelreply