How to Use SQL Databases in a NestJS Application With TypeORM

An Object-Relational Mapper (ORM) is a library that implements the Object-Relational mapping technique. This lets you write SQL database queries using the object-oriented paradigm of your preferred language.

Sign up forfree

Forgot your password?

Create an account

*Required: 8 chars, 1 capital letter, 1 number

By continuing, you agree to thePrivacy PolicyandTerms of Use.You also agree to receive our newsletters, you can opt-out any time.

4

NestJS provides high-level support for TypeORM out-of-the-box. The dedicated TypeORM package makes integration relatively easy.

Step 1: Installing Dependencies

Before using TypeORM in a NestJS application, you must install it with its native NestJS package and your preferred SQL database. SQLite is a simple, install-free option.

Run the following command to install TypeORM and its native NestJS package, usingthe npm package manager:

iCloud+ Website on MacBook Sitting on Kitchen Island

Run the following command to install SQLite:

Step 2: Creating an Entity

An entity is a collection of fields defining the data stored in a database. TypeORM uses the entity file to create a table in your database.

Follow the steps below to create an entity:

Apple’s Siri assistant on an iPhone 16 Pro

For example:

The entity file above creates this table in your database:

A smart TV in a living room with a muted Mic appended on top

PRIMARY KEY AUTO_INCREMENT

property_1

Bird nest in a tree

varchar(255)

property_2

property_3

TheTypeORM documentationcovers entities in further detail.

Step 3: Connecting Your Application to a Database

Now that your entity is set up, you’ll need to connect your application to a database. This example uses SQLite.

Follow the steps below to connect your application to a database:

Step 4: Creating a Repository

A repository is an entity’s access layer used to make queries (insert, delete, save, find, etc.) on a table created by the entity in the database. TypeORM supports the repository design pattern, thus, each entity has its own repository.

TypeORM automatically creates a repository for your entity when you follow the steps below:

Step 5: Injecting Your Repository to Its Service Using Dependency Injection

Dependency injection is a software engineering technique that is a form of the inversion of control principle. It shifts the burden of dependency management from client code to the library or service it depends upon.

Follow the steps below to inject your repository into a service:

Now that your setup is complete, it’s possible to makeSQL querieson it to retrieve or modify data.

Making SQL Queries with TypeORM

You can make any simple SQL query by calling TypeORM’s repository methods on therepovariable inside your service class. You can also create complex SQL queries by using TypeORM’s query builder.

Want to learn more about SQL? Having a handle on the various SQL query commands is a great way to get ahead.

Sometimes the smallest cleaning habit makes the biggest mess.

The key is not to spook your friends with over-the-top shenanigans.

it’s possible to’t call this offline, Notion.

One casual AI chat exposed how vulnerable I was.

The best features aren’t the ones being advertised.

Technology Explained

PC & Mobile