I am somewhat new to SQL. I have three tables users_table, products_table, and users_orders. The users_table holds basic information about the user and the users_orders table holds information about the user's orders. Here is a structure of the products_table and users_orders.
N.B
Users can signup and create products to be sold on the platform (like a marketplace).
id: Int
productName: String
price: Int
...
createdBy: Foreign key to user's table
id: Int
isOrdered: bool // to know if a user ordered a particular item
userId: Foreign key to the user's table
productId: Foreign key to the product table
Problem
In the front-end, I make an API call to grab all products from the database and display them on the main page. I want to be able to know if a user has ordered a particular item, if they have, then based on a boolean check, I can make the order button unclickable. This is to prevent them from ordering a particular item twice. (Business requirements).
Expected Behaviour
I currently use TypeORM to handle my database interaction with Postgres. How do I run the query to get all orders from the products_table alongside users_orders to know which item a user has ordered, so that I have the isOrdered field returned back to me?
API Response Example
When I make a call to the DB to get all products, a response like so is returned.
[
{
productName: String
price: Int
createdBy: User who created the product listing
date: Date
}
]
I want to be able to make a single query that can check the users_orders and products_table to know which product the current loggedIn user has made. So the response could be like this
[
{
id: Int
productName: String
price: Int
createdBy: User who created it
isOrdered: Boolean
productId: Product object details
}
]
How do I achieve this with TypeORM? Thank you in advance!