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

How to inject NestJS config into TypeORM entity?

1个答案

1

NestJS provides a modular system that enables developers to organize various components, services, and other elements into distinct modules. When working with TypeORM in NestJS, it's standard practice to create a dedicated module for database configuration and entity registration. Here are the steps to configure TypeORM and inject entities in NestJS:

Step 1: Install Required Packages

First, install the necessary packages. For example, if you're using PostgreSQL, you'll need to install the pg package.

bash
npm install --save @nestjs/typeorm typeorm pg

Step 2: Create TypeORM Entities

Entities represent database tables. You need to define entities to model your data. For example:

typescript
// cat.entity.ts import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Cat { @PrimaryGeneratedColumn() id: number; @Column() name: string; @Column() age: number; @Column() breed: string; }

Step 3: Configure TypeORM Module

In your application module, import TypeOrmModule and configure it in the root module or a specific feature module. Configuring TypeORM can be done via hard-coded settings or asynchronously using a configuration service to dynamically load settings.

Here's an example of hard-coded configuration:

typescript
// app.module.ts import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { CatsModule } from './cats/cats.module'; import { Cat } from './cats/cat.entity'; @Module({ imports: [ TypeOrmModule.forRoot({ type: 'postgres', host: 'localhost', port: 5432, username: 'test', password: 'test', database: 'test', entities: [Cat], synchronize: true, // Note: Avoid using `synchronize` in production environments. }), CatsModule, ], controllers: [], providers: [], }) export class AppModule {}

Step 4: Inject Entities into Feature Modules

Once TypeORM is globally configured, you can inject entities into feature modules using the TypeOrmModule.forFeature() method. For example, in a module handling cat data, you can do the following:

typescript
// cats.module.ts import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { CatsService } from './cats.service'; import { CatsController } from './cats.controller'; import { Cat } from './cat.entity'; @Module({ imports: [TypeOrmModule.forFeature([Cat])], controllers: [CatsController], providers: [CatsService], }) export class CatsModule {}

After registering entities via the forFeature() method in the module, you can use them within the module's services through dependency injection. For example:

typescript
// cats.service.ts import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Cat } from './cat.entity'; @Injectable() export class CatsService { constructor( @InjectRepository(Cat) private catsRepository: Repository<Cat>, ) {} findAll(): Promise<Cat[]> { return this.catsRepository.find(); } // Other CRUD methods... }

This completes the steps for configuring TypeORM and injecting entities in NestJS. All database-related operations can be performed through the respective repositories, highlighting the modularity and dependency injection advantages of the NestJS framework.

2024年6月29日 12:07 回复

你的答案