In my data structure, there is a Product entity and an Image entity with a OneToMany relationship. This means that One Product can have many Images attached to it.
When an image needs to be removed, instead of deleting the data from the table, I have chosen to simply disable it by adding a boolean column called 'deleted'. This way, when querying for the product, if the image has been deleted, it will not be included in the results.
Currently, I have a query that works perfectly when the product has an image. However, I am unsure of how to handle the scenario where the image has been deleted.
Here is the query I have:
const [ products, count ] = await this.productRepo
.createQueryBuilder('product')
.innerJoinAndSelect('product.images', 'image')
.where([{ name: Like(`%${terms}%`) }, { description: Like(`%${terms}%`) }, { code: Like(terms) }])
.orderBy('product.' + params.orderBy, params.orderMode)
.offset(params.skip)
.limit(params.take)
.getManyAndCount()
return { products, count }
Here is the structure of the Image entity:
@Entity()
export class Image extends BaseEntity {
@Column()
name: string
@Column({ default: false })
deleted: boolean
@ManyToOne(() => Product, product => product.images) product: Product[];
}
And here is the structure of the Product entity:
export class Product extends BaseEntity {
@Column({ length: 20 })
code: string
@OneToMany(() => Image, image => image.product) images: Image[]
}