An Intro to Object-Relational Mapping With TypeORM in Node.js

Object-Relational Mapping (ORM) is a technique that lets one access and modify data from a database using an object-oriented paradigm. In this lesson, we'll discuss ORM and be introduced to a popular ORM used for JavaScript and TypeScript, TypeORM.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two

From our previous lesson, we learned to write SQL statements like these to perform CRUD operations on our Postgres database. These SQL statements are the most basic and simple statements taught to new developers. Depending on what you want to achieve and the complexity of your database, SQL statements can become extremely complex. Some SQL statements can be hundreds of lines of code, and many companies have dedicated SQL developers. Object Relational Mapping or ORM is a technique that lets you access and modify data from a database using an object-oriented paradigm. In other words, instead of writing SQL statements manually, we're going to use some library to generate the SQL statements using some virtual database object. For example, to perform the same CRUD operations on our Postgres database, using an ORM, our code could look something like this. As you can see, this completely abstracts away our SQL database. Here are some pros and cons of ORM. Pros of ORM. Writing in the language of your choice. For us, this will be TypeScript. Abstracts away the database, so switching between MongoDB and Postgres is extremely easy. Many ORM support advanced features such as database migrations. Unless you are a master at SQL, statements generated by an ORM will most likely perform better than the ones you write yourself. Cons of ORM. An ORM must be configured before usage. By abstracting the way the database, many new developers will not understand what is happening under the hood. The ORM we'll be using for this course is TypeORM. TypeORM is an extremely popular ORM that supports multiple database systems. Such as MySQL, Postgres, MongoDB and much more. It also integrates extremely well with TypeScript. Before we start integrating TypeORM into our tiny house application, let's spend some time getting familiar with TypeORM's API using a simple node server. To begin, clone the source code from Lesson 4.9 from part 1 of the course. First, we'll make sure our npm packages are installed using npm install. Next, we'll make sure our npm packages are updated using npm update. If npm warns about any vulnerabilities from our packages, we'll also want to make sure we fix those using npm audit fix. Since we'll be using TypeORM and Postgres instead of MongoDB, we'll be uninst alling the MongoDB drivers. To integrate TypeORM into our node server, we'll simply follow the instructions found on their official documentation. First, we'll install the packages TypeORM requires by running npm install Type ORM and reflect metadata. Next, we'll install the Postgres database driver by running npm install Pg. Next, we'll enable the following settings in TS config. First, we'll create a new .env file with a port variable set to 9000. Remember, when we were using MongoDB, we had to set up dbuser, dbuser password, and db cluster in our .env file as well. TypeORM on the other hand, uses an ORM config.json file to store these variables. Let's head over to their official Quick Start guide to get a copy of this file. Now we'll make the following modifications. Change Type to Postgres. Change Port to the port your Postgres server runs on. For me, this is 5432. Change username to the Postgres super user. For me, this is simply Postgres. Change password to your Postgres super user's password. For me, this is my password. Change database to the Postgres database you wish to connect to. For me, I will use the same database from the previous lesson. Test db001. We'll leave Synchronize to True. This tells TypeORM to update the schema in our Postgres server automatically when we update the schema in our source code. We'll leave Logging to False. However, if you want to see the SQL statements generated by TypeORM in your console, you can turn this option to True. Finally, we'll update the path to entities, migrations, and subscribers. By updating the variables in this ORM config.json file, TypeORM can be configured to connect to other databases, such as MongoDB or MySQL. According to TypeORM's installation instruction, we'll first need to import Reflect metadata in the beginning of our code. Next, we'll head over to our database folder and modify our Connect database function to use TypeORM's CreateConnection method, which basically takes the information from our ORM config.json file to connect our node server to our Postgres database. Next, we'll create a new folder called Entity. Because TypeORM is designed to support multiple database systems, such as Postgres and MongoDB, the term Entity is used to represent a SQL table or a MongoDB collection. This Entity folder is where we'll define our database schema. In this folder, we'll create and export a listing entity, and we'll create a new folder called Entity. Entity is used to specify the name of our SQL table. Primary column ensures a column must contain unique values, which can be used to identify each row of data. Here, we are introduced to some new data types. Text represents a string. We learned about this in the previous lesson. Varchar, also known as Character Varying, also represents a string. However, we can set a maximum length to this data type. [BLANK_AUDIO] [BLANK_AUDIO] [BLANK_AUDIO] To remove the node initializer TypeScript error, we'll disable the following setting in TS config. [BLANK_AUDIO] Integer represents an integer number. Decimal represents a decimal or floating point number. To see a full list of data types type ORM supports for each database system head over to their official documentation. Let's head back to our connect database function to import the new listing entity and export a database object. [BLANK_AUDIO] Next, we'll need to modify our TypeScript definitions like so. Instead of using MongoDB's collection type, we'll use TypeORM's repository type . Instead of using MongoDB's underscore ID of TypeObject ID, we'll use ID of Type String. To explore TypeORM's API a bit more, let's head over to our GraphQL type-def file and make the following changes. We'll add a new listing query which we'll find one listing based on user input. We'll add a new create listing mutation which we'll insert a new listing into our database. We'll add a new update listing mutation which we'll find and update one listing based on the user's input. We'll also need to change listing ID from type ID to optional ID. This is because TypeORM's delete1 method returns a deleted object without the ID. Before we start modifying our GraphQL resolvers to use TypeORM instead of Mongo DB, let's see our Postgres database with some data. In the previous lesson, we learned how to manually insert data into a Postgres database using the insert SQL statement. Now we'll do the same from our source code. Instead of using MongoDB's object ID method, we'll use node crypto library instead. [silence] Let's run our seed function by running npm run seed. [silence] And we can see our seeded data in our test listings table from the PGR admin application. Finally, let's modify our GraphQL resolvers to use TypeORM. [silence] Let's start off simple and find one listing from our test listings table. [silence] We'll use TypeORM's find1 method. [silence] Next, to find all the listings in our test listings table, we'll use TypeORM's find() method. [silence] Next, to insert a new listing into our test listing table, we'll use TypeORM's create() method to create a new instance of an entity local name. And then we'll use the save() method to save it to our database. [silence] Next, to update a database in our test listings table, we'll use TypeORM's save () method to save any entity changes to our database. [silence] And finally, to delete a listing from our test listings table, we'll use TypeOR M's remove() method. To see a full list of TypeORM's repository API, check out their official documentation. Finally, we can start our node server and test out our GraphQL API by running npm start. In the GraphQL playground, we can find all the listings using the listings query. [silence] Find one listing using the listing query. Create a new listing using the create listing mutation. [silence] Update a listing using the update listing mutation. [silence] Delete a listing using the delete listing mutation. Now that we have gotten familiar with TypeORM's API, in the next lesson, we'll integrate Postgres and TypeORM into our tiny house application.