Is there a way to generate or modify a single entity after another entity has been created?

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...

Answer №1

Implementing cascade functionality allows for the automatic saving, updating, and deleting of related entities whenever actions are performed on the Sale Entity.

@Entity()
export class Sale {
  @PrimaryGeneratedColumn()
  id: number

  @ManyToOne((type) => Store, (store) => store.sales , {cascade : true})
  store: Store

  @Column()
  productId: number

  @Column()
  user: number

  @Column()
  count: number

  @Column()
  sum: number

  @CreateDateColumn()
  createDate: Date
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips on creating a universal shortcut function for React.js components using decorators

Utilizing React Intl for internationalization requires me to utilize this.props.intl.formatMessage({id: 'some.message.id'}, values) to retrieve a translated string within the render() method. Is there a way to create a decorator that acts as a sh ...

The issue with JQuery's attr function is that it is successfully setting the minimum value for an input tag in

In the midst of working on some JQuery/Javascript code and HTML tags, I've encountered a puzzling issue. My javascript document successfully sets the min value for input tags in an HTML5 document, but for some reason, the max value is not being proper ...

`Fetching data from a table using a variable in PDO`

After switching to PDO in PHP to improve my skills, I learned that concatenation is no longer recommended. As part of this learning process, I created a basic "user control panel" system where user details are stored in the accounts table. Each account has ...

Fix the problem of "@typescript-eslint/no-invalid-this" in class fields without causing issues with "@typescript-eslint/no-this-alias"

Take a look at the code snippet below: import { Vue, Component } from "vue-property-decorator"; @Component({ components: {} }) export default class ProductViewingAndEditingPage extends Vue { private readonly componentsReferencesIDs: { ...

Refresh grid components with checkbox feature - ionic 2

I'm in need of assistance. I have a grid with multiple columns and I want to update the selected rows in my database by clicking a button. Unfortunately, I haven't been able to find any information on how to do this. Below is the code I am curre ...

What is the proper way to perform date validation using the moment library in JavaScript?

I am facing an issue with the departure date validation on my website. I have a textbox called txtDepartureDate where users can choose the date of departure. The requirement is that if the selected date is before today's date, an error message should ...

Dealing with problematic hover behaviors in Cypress: A guide

I encountered an issue with Cypress hover functionality while trying to access a sub menu that appears after hovering over a main menu item. The error message I received was This element is not visible because it has CSS property: position: fixed and it&ap ...

An error occurred: Unable to access the 'indexOf' property of an undefined variable within the angularjs framework

Hello world of coding, I am a newbie seeking help from experts. The angularJs error I've encountered in the file has been identified and is related to this code snippet: if( searchItemsSmallLetters.indexOf(searchTextSmallLetters) !== -1){ Your assi ...

What is the best way to add elements to a custom-defined array?

I am currently utilizing Angular 5 with typescript version 2.7.1. Within typescript, I have created a custom type: arr: {id: string; name: string; }[]; I am attempting to add an element to the array and have experimented with the following methods: thi ...

Update the src of #contentImg to display the image from the clicked link

I have a jQuery mobile listview set up where I want to use jQuery to change the source of #contentImg to the source of the image thumbnail that is clicked. So, when an 'a' element within a 'ul' with the data-role of "listview" is click ...

Using the angular function directly is not possible without using setTimeout

My goal is to maintain the functionality of the back button in our application. We have implemented a back button that, when clicked, should preserve all the values selected or filled out by the user in the form. This task is being carried out in an MVC fr ...

Guide on developing a JavaScript script for implementing across numerous Bootstrap modals within a single webpage

I have been working on setting up a page with 14 different modals. Initially, all the modals were opening normally, but I recently discovered a way to make them draggable when opened. After some trial and error, I managed to implement this feature successf ...

Learn the process of developing a web client application using Node.js and NPM similar to the AngularJS tutorial

I am new to nodejs, npm and angularjs. I recently explored the angularjs tutorial project available at https://github.com/angular/angular-phonecat.git. This project has been really interesting for me as it demonstrates how easy it is to manage modules wi ...

Enhancing the website with a search feature

Running a website with an ever-increasing number of resources, I initially implemented SQL Full Text Search which worked well until recently. However, performance issues have arisen, prompting me to seek advice on optimizing the search functionality. Using ...

Error encountered when attempting to access an instance property or method using dynamic input within the square brackets in Typescript

Here is the code snippet that I am working with: class Label{ constructor( public name:string='name', public configPath:string='path', public foo:{bar:string} = {bar:'hello'} ){ } } const labelName:string = ...

Passing a value to a component to delete a table row in ReactJS using Material UI

My task is to delete a specific table row after clicking. I have two components and have already written a function, handleDelete(), to remove a row from the table. The issue is that whenever I click, it always deletes the last row instead of the one I cli ...

Is it achievable to create a seamless fade in/out effect using jQuery?

I am experiencing a problem with smooth fading of image backgrounds on my website. Interestingly, this issue only occurs in Safari and Opera. In Firefox, Chrome, and IE7/8, you can see "frames" of the fade transition, almost like taking pictures. I am cu ...

Getting rid of the Horizontal Scroll Bar

Having trouble with a persistent horizontal scrollbar in the "section3__container" container despite attempts to adjust size and overflow settings. Need assistance in removing this unwanted feature. <html lang="en"> <head> <m ...

Instructions for utilizing lodash or JavaScript to apply a filter to an object

Received this object from the Server: var data = { test1: { documents: [] }, test2: { documents: [{ vId: 'sdfas23', TypeId: '81', isDeleted: false }], answer: true }, test3: { documents: ...

In need of assistance with Ember data! Struggling to deserialize JSON into model

Here is the technology stack I'm currently using: Ember 1.10.0 Ember Data 1.0.0-beta.15 Within my application, I have defined a model as shown below: //models//acceptedtask.js import DS from "ember-data"; export default DS.Model.extend({ userAg ...