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

How to set up typeorm .env file?

1个答案

1

The .env file is a custom environment file used by TypeORM to configure database connection options. In TypeORM, the default configuration files are ormconfig.json, ormconfig.js, ormconfig.yml, etc., but you can specify different files or set the configuration directly in environment variables.

If you want to use the .env file to configure TypeORM, you should first install the dotenv library to load environment variables from the .env file. Here are the steps to do it:

  1. Install the dotenv library:
shell
npm install dotenv
  1. Create the .env file:

Create a .env file in the root directory of your project and set your database connection options in it. For example:

shell
# .env TYPEORM_CONNECTION=postgres TYPEORM_HOST=localhost TYPEORM_USERNAME=myuser TYPEORM_PASSWORD=mypassword TYPEORM_DATABASE=mydatabase TYPEORM_PORT=5432 TYPEORM_SYNCHRONIZE=true TYPEORM_LOGGING=true TYPEORM_ENTITIES=src/entity/*.ts
  1. Load the .env file at the application entry point:

In your application's entry point, such as index.ts or app.ts, load the .env file using the dotenv library.

typescript
import 'dotenv/config'; // Other application imports and code...
  1. Configure TypeORM:

Use environment variables to configure TypeORM. If you are using TypeScript, create a data source instance with these variables, as shown below:

typescript
import { DataSource } from 'typeorm'; const AppDataSource = new DataSource({ type: process.env.TYPEORM_CONNECTION as any, host: process.env.TYPEORM_HOST, port: parseInt(process.env.TYPEORM_PORT), username: process.env.TYPEORM_USERNAME, password: process.env.TYPEORM_PASSWORD, database: process.env.TYPEORM_DATABASE, synchronize: process.env.TYPEORM_SYNCHRONIZE === 'true', logging: process.env.TYPEORM_LOGGING === 'true', entities: [process.env.TYPEORM_ENTITIES], }); export default AppDataSource;
  1. Start your application:

When you launch your application, TypeORM will use the settings from the .env file to connect to the database.

Example:

Suppose you have a simple Node.js application using TypeORM to connect to a PostgreSQL database. Here is the .env file:

shell
# .env TYPEORM_CONNECTION=postgres TYPEORM_HOST=localhost TYPEORM_USERNAME=postgres TYPEORM_PASSWORD=postgres123 TYPEORM_DATABASE=test_db TYPEORM_PORT=5432 TYPEORM_SYNCHRONIZE=true TYPEORM_LOGGING=false TYPEORM_ENTITIES=dist/entity/**/*.js

In index.ts:

typescript
import 'dotenv/config'; import { DataSource } from 'typeorm'; import AppDataSource from './data-source'; AppDataSource.initialize() .then(() => { // Logic after successful database initialization... console.log('Data Source has been initialized!'); }) .catch((error) => { console.error('Error during Data Source initialization:', error); });

This approach allows you to flexibly configure your database connection using environment variables and easily switch configurations across different environments (development, testing, production, etc.).

2024年6月29日 12:07 回复

你的答案