typeorm 基础
TypeOrmModule 中的 forRoot、forFeature、forRootAsync
TypeOrmModule 是 Nest.js 中用于集成 TypeORM 的模块,它提供了不同的静态方法来配置 TypeORM 并将其集成到你的 Nest.js 应用程序中。这些方法包括 forRoot、forFeature 和 forRootAsync,它们各自用于不同的目的:
forRoot(options?: TypeOrmModuleOptions)
forRoot方法用于全局配置TypeORM,通常在应用程序的主模块中使用。它接受一个配置对象TypeOrmModuleOptions,用于配置TypeORM的连接和其他全局设置。这个方法应该只在应用程序的主模块中调用一次。
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'password',
database: 'nestjs',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true
})
]
// ...
})
export class AppModule {}forFeature(entities: EntityTarget<any>[], connection?: Connection | string)
forFeature方法用于在特定模块中导入实体类以便在该模块中使用TypeORM的repository进行数据库操作。你可以将实体类传递给forFeature方法,以便在该模块的上下文中使用这些实体类。
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user.entity';
@Module({
imports: [TypeOrmModule.forFeature([User])]
// ...
})
export class UserModule {}forRootAsync(options: TypeOrmModuleAsyncOptions)
forRootAsync方法用于在异步设置下配置TypeORM。这对于需要动态加载配置的情况非常有用,例如从配置文件中加载数据库连接配置。forRootAsync方法接受一个配置对象TypeOrmModuleAsyncOptions,你可以使用其中的useFactory、useClass或useExisting来提供一个异步工厂函数、类或提供者以生成配置对象。
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
type: 'mysql',
host: configService.get('DB_HOST'),
port: configService.get('DB_PORT'),
username: configService.get('DB_USERNAME'),
password: configService.get('DB_PASSWORD'),
database: configService.get('DB_DATABASE')
}),
inject: [ConfigService]
})
]
// ...
})
export class AppModule {}typeorm 中的自动化
TypeOrmModule.forRootAsync({
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
type: 'mysql',
host: configService.get('db.host'),
port: configService.get('db.port'),
username: configService.get('db.username'),
password: configService.get('db.password'),
database: configService.get('db.database'),
synchronize: configService.get('typeOrm.synchronize'),
autoLoadEntities: configService.get('typeOrm.autoLoadEntities'),
}),
}),synchronize
synchronize属性是TypeORM中的一个配置选项,用于指定在应用程序启动时是否自动同步数据库结构。该属性用于配置数据库模式自动更新的行为。当
synchronize设置为true时,TypeORM会在应用程序启动时自动检查实体Entities的定义与数据库表的结构是否一致。如果存在差异,TypeORM将尝试更新数据库表以与实体定义保持一致。这种自动同步的机制适用于开发和测试环境,可以简化开发人员的工作流程。每当你更改实体的定义时,
TypeORM将自动更新数据库表的结构,无需手动执行数据库迁移或手动更新表结构。但需要注意的是,在生产环境中,将
synchronize设置为true是不推荐的。在生产环境中,建议使用数据库迁移工具(如TypeORM的迁移功能)来管理数据库结构的变更,以确保数据的完整性和一致性。默认情况下,
synchronize属性为false,即禁用自动同步功能。要启用自动同步,你可以在TypeORM的配置文件中将synchronize设置为true,或者在TypeOrmModule.forRoot()方法中将其配置为synchronize: true。
示例配置:
TypeOrmModule.forRoot({
// 其他配置项...
synchronize: true,
}),- 需要注意的是,自动同步功能可能会导致数据丢失或不可逆的表结构更改,因此在生产环境中使用时要非常谨慎,并在必要时备份数据。
autoLoadEntities
autoLoadEntities是TypeORM中的一个配置选项,用于指定是否自动加载实体Entities。当
autoLoadEntities设置为true时,TypeORM会自动加载应用程序中定义的所有实体。这意味着你不需要手动在TypeORM配置中指定每个实体的路径,TypeORM将会自动扫描并加载你的实体。使用
autoLoadEntities的好处是,它使实体的管理更加方便。你只需要在你的项目中创建实体类,并确保这些类被正确导入【实体还是需要在对应的模块用 TypeOrmModule.forFeature 引入的】。TypeORM将根据这些导入的实体类自动加载和管理数据库表结构。默认情况下,
autoLoadEntities的值为false,即禁用自动加载实体功能。要启用自动加载实体,你可以在TypeORM的配置文件中将autoLoadEntities设置为true,或者在TypeOrmModule.forRoot()方法中将其配置为autoLoadEntities: true。
示例配置:
TypeOrmModule.forRoot({
// 其他配置项...
autoLoadEntities: true,
}),注意
需要注意的是,启用自动加载实体功能时,TypeORM 将会扫描整个项目并加载所有符合要求的实体类。这可能会导致性能上的一些影响,特别是当项目中存在大量实体类时。如果你的项目中只有少量实体类,或者你希望手动控制要加载的实体,可以将 autoLoadEntities 设置为 false,并手动指定要加载的实体路径。通过明确指定要加载的实体路径,你可以更精确地控制哪些实体将被加载和管理。
TypeOrmModule.forRoot({
// 其他配置项...
autoLoadEntities: false,
entities: [Entity1, Entity2, ...],
}),
russ