乐闻世界logo
搜索文章和话题

How to config typeorm with .env variables

1个答案

1

Using the .env file makes our application configuration more secure, flexible, and simplifies migration and deployment across different environments.

1. Why Use .env Files for Configuration

The .env file is primarily used to store environment-sensitive information that should not be hardcoded directly in the code, such as database usernames, passwords, and hostnames. This approach offers several benefits:

  • Security: Avoid storing sensitive information directly in the source code to minimize the risk of leaks.
  • Flexibility: Use different configurations for various environments (development, testing, production) without modifying the code itself.
  • Maintainability: Centralize configuration management for easier maintenance and updates.

2. How to Use .env Files for Configuration in TypeORM

In TypeORM, we typically configure database connections by creating an ormconfig.json file. However, we can leverage environment variables to enhance configuration flexibility. Here, I will demonstrate how to use .env files in conjunction with TypeORM to configure database connections.

Step 1: Install Required Libraries

First, we need to install the dotenv library, which helps load environment variables from the .env file in Node.js applications.

bash
npm install dotenv --save

Step 2: Create the .env File

Create a .env file in the project's root directory and add the corresponding environment variables:

shell
# .env DB_HOST=localhost DB_PORT=3306 DB_USERNAME=root DB_PASSWORD=rootpassword DB_DATABASE=mydatabase

Step 3: Configure TypeORM to Use Environment Variables

Next, in the project, we need to configure TypeORM to use these environment variables. We can do this in the ormconfig.js file (rather than ormconfig.json) since we need to execute code to read environment variables:

javascript
require('dotenv').config(); module.exports = { type: "mysql", host: process.env.DB_HOST, port: process.env.DB_PORT, username: process.env.DB_USERNAME, password: process.env.DB_PASSWORD, database: process.env.DB_DATABASE, synchronize: true, logging: false, entities: [ "src/entity/**/*.ts" ], migrations: [ "src/migration/**/*.ts" ], subscribers: [ "src/subscriber/**/*.ts" ] };

In this configuration file, we use the dotenv library to load the .env file and then access the specified environment variables using process.env.

Example

Suppose we are developing an application that needs to connect to a MySQL database. During development, we might have a specific database configuration on our local machine, while in production, it is entirely different. By using the .env file and the configuration method above, we can easily switch these configurations without modifying the code itself. This not only enhances security but also improves the project's maintainability and flexibility.

This is an overview of using .env files to manage database connection configurations in TypeORM.

2024年8月3日 16:37 回复

你的答案