import { Cascade, Collection, Entity, Index, ManyToOne, OneToMany, Property, OneToOne } from '@mikro-orm/core'; import { Category } from './category.entity'; import { BaseEntity } from '../../../common/entities/base.entity'; import { Shop } from '../../../modules/shops/entities/shop.entity'; import { Review } from 'src/modules/review/entities/review.entity'; import { Favorite } from './favorite.entity'; @Entity({ tableName: 'products' }) @Index({ properties: ['shop', 'isActive'] }) @Index({ properties: ['category', 'isActive'] }) @Index({ properties: ['isActive'] }) export class Product extends BaseEntity { @ManyToOne(() => Shop) shop: Shop; @ManyToOne(() => Category) category: Category; @OneToMany(() => Review, review => review.product, { cascade: [Cascade.ALL], orphanRemoval: true }) reviews = new Collection(this); @OneToMany(() => Favorite, favorite => favorite.product) favorites = new Collection(this); @Property({ nullable: true }) title?: string; @Property({ type: 'text', nullable: true }) desc?: string; @Property({ type: 'json', nullable: true }) content?: string[]; @Property({ type: 'decimal', precision: 10, scale: 2, nullable: true }) price?: number; @Property({ type: 'int', nullable: true }) order?: number; @Property({ type: 'boolean', default: true }) isActive: boolean = true; @Property({ type: 'json', nullable: true }) images?: string[]; @Property({ type: 'float', default: null }) score?: number | null = null; @Property({ type: 'float', default: 0 }) discount: number = 0; @Property({ type: 'boolean', default: false }) isSpecialOffer: boolean = false; }