माईप्रोडक्ट एंटिटी
@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())),
);
}
मैं उत्पाद तालिका और उत्पाद श्रेणी को एक साथ कैसे जोड़ सकता हूँ?
यहां कच्ची क्वेरी है "उत्पादों से चुनें * पी आंतरिक शामिल हों product_category pc पर pc.id=p.category_id;"
मैं Nestjs Typeorm+ Postgresql का उपयोग कर रहा हूं*
1 उत्तर
आपको केवल उस तालिका को निर्दिष्ट करने की आवश्यकता है जिसमें आप शामिल होना चाहते हैं:
findAll(option: IPaginationOptions): Observable<Pagination<ProductEntity>> {
const result = await this.productRepo
.createQueryBuilder('product')
.innerJoinAndSelect('product.category','category')
.getMany();
console.log(result); // check the result
return from(paginate<ProductEntity>(queryBuilder, option)).pipe(
map((products) => products),
catchError(() => throwError(new InternalServerErrorException())),
);
}
अधिक जानकारी के लिए देखें इनर और लेफ्ट जॉइन
संबंधित सवाल
नए सवाल
postgresql
PostgreSQL एक खुला-स्रोत, रिलेशनल डेटाबेस मैनेजमेंट सिस्टम (RDBMS) है जो लिनक्स, यूनिक्स, विंडोज और ओएस एक्स सहित सभी प्रमुख प्लेटफार्मों के लिए उपलब्ध है। प्रश्न पूछते समय कृपया अपने पोस्टग्रेज के संस्करण का उल्लेख करें। प्रशासन या उन्नत सुविधाओं से संबंधित प्रश्न dba.stackexchange.com के लिए सर्वोत्तम हैं।