{{announcement.body}}
{{announcement.title}}

Integration Dotenv With NestJS and Type ORM

DZone 's Guide to

Integration Dotenv With NestJS and Type ORM

In this article, we discuss how to integrate dotenv with NestJs and TypeORM to get you up and going with your Node application configuration.

· Web Dev Zone ·
Free Resource

When you are using third-party sources for app development, there is a need for the involvement of SSH keys or API credentials. This goes on to become a problem when it is handled by a team of developers. Thus, the source code has to be pushed to Git repositories periodically. Once the code is pushed to a repository, anyone can see it with third-party keys.

A very prominent and widely used solution for this problem is using environment variables. These are the local variables containing some useful information like API keys and are made available to the application or project.

A tool known as dotenv has made it easy to create such variables and making these variables available to the application. It is an easy to use tool which can be added to your project by using any package manager.

You may also like: NestJS: A Backend NodeJS Framework.

We will use Yarn as a package manager. To begin with, add the package using the terminal:

TypeScript
 




x


 
1
yarn add dotenv



Since we are using NestJS, which is based on Typescript, we need to add the @types package that acts as an interface between JavaScript and Typescript package.

TypeScript
 




xxxxxxxxxx
1


 
1
yarn add @types/dotenv



Since the database being used is Postgres, we need to install the necessary driver for Postgres.

TypeScript
 




xxxxxxxxxx
1


 
1
yarn add pg



Now, install the TypeORM module to your nest project.

TypeScript
 




xxxxxxxxxx
1


 
1
yarn add @nestjs/typeorm typeorm



Now, create TypeORM entities in your project folder. For the purpose of illustration, we will be creating a folder, db inside the src folder of our next project, and inside this folder, create another folder entities, and create a Typescript file containing information about your TypeORM entity

For the sake of simplicity, we will create a user-entity file. Also, we will be creating an ‘id‘ field, a ‘name‘ field and an ‘email‘ field for this entity.

TypeScript
 




xxxxxxxxxx
1
12


 
1
#src/db/entities/user.entity.ts
2
 import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
3
 @Entity({name: 'UserTable'})
4
class UserEntity extends BaseEntity {
5
 @PrimaryGeneratedColumn()
6
  id: number;
7
  @Column()
8
  name: string;
9
  @Column()
10
  email: string;
11
}
12
 export default UserEntity;



Note that this entity is given the name, UserTable, which is optional, but in case of migration, it becomes somewhat useful. We will get to know the reason shortly.

Now, create a migration file for this user entity. A migration file can be created using a command-line interface with the following command:

TypeScript
 




xxxxxxxxxx
1


1
typeorm migration:create -n CreateUserTable



This will create a migration file with the timestamp as a substring in the name of this file.

Here, CreateUserTable will be the name of your migration file created by the TypeORM environment. Now, we will create a folder, migrations, inside the db folder and place the migration file inside it if it is not done already.

Now, make a separate file that will be used as a migration utility to decide the schema of the database. Thus, we can name this file as migrationUtil.ts

Inside this migrationUtil.ts file, create functions to get various types of columns, namely varchar, integer, etc. We will be creating two functions for illustration, namely getIDColumn and getVarCharColumn

TypeScript

 

Here, TableColumnOptions is a type provided by TypeORM out of the box. The code for this file is simple and straight. Whenever one of these functions is called, they create a separate column in your entity table.

Now, back to the CreateUserTable migration file; the file should look like this:

Java


Now, add a table to this migration file using our migration utility file as:

TypeScript


Note that the name of this table is given the same as the userEntity, so as to improve entity-table mapping for developers. Also, finish up the code for async up and down methods using QueryRunner.

The idea is to create three columns in the user table — userId,  name, and email.

Thus, in the end, the migration file will be looking something like this:

TypeScript


Now, create your environment files containing environment variables. We will be creating two .env files, namely, development.env and test.env.

The environment variables for development.env will be:

TypeScript


And the environment variable for test.env will be:

TypeScript


Now, create a TypeORM config file for the connection setup.

We will place this file in the config folder under the src folder of the project.

TypeScript


Here, process.env will contain all our environment variables. Note that the environment will be specified by us during command execution and thus, anyone of the files development.env or test.env will be taken as environment variables supplying file.

In the same folder, create another configuration file for dotenv, and we will name it, dotenv-options.ts.

TypeScript


The code for this file is pretty straightforward. Note that the line of code containing console.log call will let us know which environment is taken by the nest while executing commands and the same file is being provided as dotenv options below it.

Now, to successfully integrate dotenv with Nest, it is recommended by the official Nest docs to create a config service along with a config module.

Thus, create a services folder and inside that folder — create a config.service.ts file.

TypeScript


Here, IEnvConfigInterface is an interface provided explicitly by us to improve the understandability of code.

TypeScript


dotenv.parse will read the contents of the file containing environment variables and is made available for use. It can accept string or buffer and convert it into an object of key-value pairs.

This object is then validated by using Joi schema object which is a library provided by Hapi. Under this schema, we have specified that the environment (whether test or development) will be grabbed as the NODE_ENV key in the command line.

Also, if no environment is specified, then set the environment to ‘development’. Thus, our envConfig variable is now initialized with this validated object.

Now, create a configModule and import it to the app module.

TypeScript


Here. the config service is injected into this module. But since our config service is expecting an argument through the constructor, we will use useValue to provide this service an argument, which by default is development.env file, if no environment is explicitly provided during the execution of CLI commands.

Now, we will create another loader file that will load all the configurations for database and dotenv.

We will create this file in the cli folder under the src folder of our project and name it as loader.ts.

TypeScript


Note that there is a comment in the code to import dbConfig only after dotenv config is imported. This is because our database configuration will depend on the environment used by nest.

Now, in our package.json file under the ‘scripts’ section, we will add two key-value pairs that will be our CLI command for migration.

TypeScript


Note that this command will directly execute our loader file.

And, that’s it!

We have, at last, integrated dotenv with NestJS and TypeORM.

To test this, start your database server, and then run the following CLI commands one after another:

TypeScript


It will console the environment currently being used by us.


Further Reading

Topics:
dotenv ,javascript ,nest.js ,typeorm ,typescript ,web dev

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}