'Node MVC user data modelling - linking records to the current user

I've set up a user model for an application. I would like each user to have access to their own records after they've logged in, e.g. orders, customers, etc. How best to implement this?

My only idea is that I add a "user" field to the records which holds the id of the user it belongs to (i.e. through parent referencing). Then add some middleware which ensures that only records linked to the currently logged in user are returned. This doesn't seem very efficient but I don't really know.

Is there a better way to model the data here?



Solution 1:[1]

this situation depends on your DB's data relationships. For instance; 1:many,many:many, 1:1 etc.

Let's assume you have a product and user who bought that product could check it.

1 user can buy more than 1 product. so it's best to use 1:many.

but everytime when you call an user you're going to upload product data as well, that's why you need to know approximately how many data will be inserted.

user - product

  1. UserID
  2. ProductID
  3. OrderID
  4. other details...

Seller can access order by querying product id or order id, as well as Customer also can check what kind of product has been bought.

BTW, My english is not good enough, sorry for that.
Happy Coding?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ă–mer Atayilmaz