This video is available to students only

TinyHouse - PostgreSQL & TypeORM

In this lesson, we'll convert the server of our TinyHouse project to use PostgreSQL & TypeORM.

📝 In this lesson, we'll be using the following seed data to populate our database - listings and users. This seed data does not reference MongoDB's ObjectId property which is important as we upload this data to our PostgreSQL database.

In this lesson, we'll integrate PostgreSQL and TypeORM into our TinyHouse application. To begin, we'll clone the source code from lesson 15.6 from Part 2 of the course.

We are beginning this lesson from the last point of our source code for the TinyHouse app. Whatever you've managed to complete Part II of the course with will be a good starting point for this lesson.

Install & Update#

First, we'll head over to our client application and make sure our npm packages are installed:

That will be all we'll do for our React client. All the further changes we need to make to integrate PostgreSQL & TypeORM into TinyHouse will be on the server-side!

We'll head over to our Node server and make sure our npm packages are installed:

Next, we'll make sure our npm packages are updated:

If npm warns about any vulnerabilities from our packages, we'll also want to make sure we fix those:

Since we'll be using TypeORM and PostgreSQL instead of MongoDB, we'll be uninstalling the MongoDB driver and accompanying type definitions:

Like what we did in the previous lesson, we'll first install the packages TypeORM requires:

Next, we'll install the PostgreSQL database driver:

Next, we'll enable the following settings in the tsconfig.json file to complement some of the code we'll write with TypeORM:

Configuration#

We'll create a new .env file (or update our existing available .env file) with the following variables set.

In this lesson, we're focused on swapping our server interaction from MongoDB to PostgreSQL. You can use the same environment variables you used when going through Part 2 of the course but remove the DB_USER, DB_USER_PASSWORD, and DB_CLUSTER environment variables since we won't need these any longer.

Next, we'll copy over the same ormconfig.json from our previous lesson and drop it into our TinyHouse server project since we are to have a similar ORM configuration. We'll also need to create a new PostgreSQL database. To do this, we'll open pgAdmin and create a new database called tinyhouse and we'll update our ormconfig.json with our new database name.

The ormconfig.json file of our server project will look like the following:

Some of your ORM config values may look different depending on your database username, password, etc.

Set-up#

Like what we did in the previous lesson, we'll first need to import reflect-metadata at the beginning of our server project which we'll do in our server src/index.ts file.

Next, we'll head over to our database folder and modify our connectDatabase() function to use TypeORM's createConnection() method to connect our Node server to our PostgreSQL database. We'll also import entities that we'll soon create - BookingEntity, ListingEntity, and UserEntity.