I need to establish relationships between tables correctly.
There are Orders, Invoices and Users tables.
For example, the Orders table consists of fields (Id, startDate, finalDate, price).
For example, the Invoices table consists of fields (Id, startDate, finalDate, totalPrice).
For example, the Users table consists of fields (Id, username, email, password).
* The fields of the tables above do not contain coherent fields.
Links (for the time being, I see connections like this)
db.users.hasMany(db.invoices); db.users.hasMany(db.orders); // db.orders.belongsTo(db.users); db.orders.belongsTo(db.invoices); // db.invoices.belongsTo(db.users); db.invoices.hasMany(db.orders); When an order is made, OrderController does not know about the final invoices, it simply adds a new order to the user.
When an invoice request (list) is made, InvoiceController gets the last invoice from the database, checks the dates, if the old invoice creates a new one from the orders for the current month. A request is made for orders related to Users for the selected dates. And the total amount of bills is added to the totalPrice field.
If the account of the current month, then a request is made for orders related to Users for the selected dates. And the total amount of bills is updated in the totalPrice field.
And then a request is made for all accounts associated with the Users (list).
If the user wants to see one account, a request is made for orders related to Users for the selected dates and a second request to Invoices to receive account data. Formed by json and sent to the client.
Something I do not understand how best to make the relationship between the tables based on this logic.