我在 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)
}
每个查询都工作得很好,但我想做的是将这两个查询合并为一个查询,这样我可以从商店和报价中获得搜索结果,并对记录进行排序,然后应用跳过并接受它们。 有什么办法可以做到吗?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
TypeORM 使您能够随心所欲地使用任何查询。使用 entityManager.query() 这是文档。 p>