Skip to content

Database Configuration

Mastering your database connection lies at the heart of this project. This page helps you navigate the configuration settings like a pro.

Configuration steps

1. Environment Variables:

  • Securely store your database credentials in the .env file:
DB_URL=            # Optional connection URL (overrides other options)
DB_HOST=localhost
DB_PORT=3306
DB_USER=your_username
DB_PASS=your_password
DB_NAME=your_database_name

2. TypeORM Configuration:

  • The configuration lives in src/config/typeorm.config.ts:
typescript
import { registerAs } from '@nestjs/config';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';

const typeOrmModuleConfig = registerAs(
  'typeOrmModuleConfig',
  (): TypeOrmModuleOptions => {
    return {
      type: 'mysql', // Adjust based on your database type
      logging: false,
      url: process.env.DB_URL,
      host: process.env.DB_HOST,
      port: parseInt(process.env.DB_PORT),
      username: process.env.DB_USER,
      password: process.env.DB_PASS,
      database: process.env.DB_NAME,
      entities: ['dist/src/**/*.entity{.js,.ts}'],
    };
  },
);

export { typeOrmModuleConfig };

Key Points:

  • Prioritize the DB_URL environment variable if set.
  • Explore the extensive configuration options documented by TypeORM.

Remember

setting up your database connection correctly lays a solid foundation for your project's success. Stay tuned for further sections diving deeper into specific functionalities!