Interacting with our SQL Database
As you know, our database is what is called a relational database, and therefore database that can be queried through SQL
But because no one likes SQL because it can be really annoying to write - most programming languages and ecosystems provide ways to write SQL in their own programming language - and adhering to the principles of that language. These systems are called Object Relational Mappers (ORMs)
Basically, the ORM we use converts typescript code into SQL queries
We use an ORM called drizzle to interact with our database and execute SQL queries.
It would be good to do some research on how drizzle converts sql queries to typescript queries, and later on i will make you create your own query.
Here is an example of an SQL query and the equivalent drizzle query:
SQL:
SELECT DISTINCT
places.id,
places.name
FROM places
INNER JOIN reviews
ON places.id = reviews.placeId;
Drizzle:
await db
.selectDistinct({
id: places.id,
name: places.name,
})
.from(places)
.innerJoin(reviews, eq(places.id, reviews.placeId));
Note the asynchronous await clause. I understand this is unfamiliar territory and you might be not as familiar with asynchronous programming. Basically we need to 'wait' for our database to complete the query - and therefore we must define how and where our code waits.