【发布时间】:2021-04-23 13:02:23
【问题描述】:
我的产品实体
@Entity({ name: 'products' })
export class ProductEntity extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
product_name: string;
@Column({ nullable: true, default: 0 })
unit_qty: number;
@Column({ nullable: true, default: 0 })
unit_price: number;
@Column()
size: number;
@Column({ nullable: true })
cost: number;
@Column({ type: 'boolean', default: false })
status: boolean;
@ManyToOne(
() => ProductCategoryEntity,
(productCategoryEntity) => productCategoryEntity.product,
)
@JoinColumn({ name: 'category_id', referencedColumnName: 'id' })
category: ProductCategoryEntity;
@Column()
category_id: number;
@BeforeInsert()
async lowerCase() {
this.product_name = this.product_name.toLowerCase();
}
}
**My Product Category Entity**
@Entity({ name: 'product_category' })
export class ProductCategoryEntity extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
category: string;
@OneToMany(() => ProductEntity, (productEntity) => productEntity.category)
product: ProductEntity;
@BeforeInsert()
async lowerCase() {
this.category = this.category.toLowerCase();
}
}
我的产品服务 我想使用 createQueryBuilder 连接表的代码有什么例子吗?
findAll(option: IPaginationOptions): Observable<Pagination<ProductEntity>> {
const queryBuilder = this.productRepo
.createQueryBuilder('product')
.innerJoinAndSelect();
return from(paginate<ProductEntity>(queryBuilder, option)).pipe(
map((products) => products),
catchError(() => throwError(new InternalServerErrorException())),
);
}
如何将产品表和产品类别连接在一起?
这里是原始查询“ select * 从产品 p 内连接product_category pc on pc.id=p.category_id; "
我正在使用 Nestjs Typeorm+ Postgresql*
【问题讨论】:
标签: postgresql pagination inner-join nestjs typeorm