Seeking assistance with database operation
Within my database, I have three tables: stores, sales, and stocks. My goal is to update the stock table whenever a new sale is saved in the sales table.
The following are my entity definitions:
@Entity()
export class Sale {
@PrimaryGeneratedColumn()
id: number
@ManyToOne((type) => Store, (store) => store.sales)
store: Store
@Column()
productId: number
@Column()
user: number
@Column()
count: number
@Column()
sum: number
@CreateDateColumn()
createDate: Date
}
Stock Entity:
@Entity()
export class Stock {
@PrimaryGeneratedColumn()
id: number
@Column()
product: number
@ManyToOne((type) => Store, (store) => store.stocks)
store: Store
@Column()
count: number
}
In the stock entity, there are fields for product ID and store ID, which also exist in the sale entity. My objective here is to update the 'count' field of an item in the stock table matching both 'product' and 'store' from a new sale entry. To achieve this, it involves incrementing the 'count' value by the quantity sold. Is this feasible?
While one approach could be utilizing the stock service within the sale service to implement this logic, I am exploring more streamlined solutions...