Database connection

First, we should connect to the database.

Let's create a DatabaseModule to connect to the database.

nest g mo database

In its imports array, add the following

TypeOrmModule.forRoot({
  type: 'postgres',
  username: 'postgres',
  password: 'pass123',
  host: 'localhost',
  port: 5432,
  database: 'postgres',
  autoLoadEntities: true,
})

The autoLoadEntities option automates the loading of domain's entities.

Commit - Connecting to database

Alright, we established a connection to the database. But this solution is not very interesting, don't you think? We are exposing all our credentials here. The same problem appeared again.

In the Configuration section, we'll learn how to fix this issue through the usage of environment variables and also have validation/typing for them.

Last updated