如何设置 autoLoadEntities: true 连接 Nest js 和 typeorm

How to set autoLoadEntities: true in connecting Nest js with typeorm

我正在设置配置文件以将 mssql 与 Nest js 和 Typeorm 连接

import { SqlServerConnectionOptions } from 'typeorm/driver/sqlserver/SqlServerConnectionOptions';
import { Employee } from './src/employees/employee.entity';

const config: SqlServerConnectionOptions = {
  type: 'mssql',
  host: 'localhost',
  port: 1433,
  username: 'sa',
  password: 'sa',
  database: 'db1',
  synchronize: false,
};

export default config;

我想在配置中添加 autoLoadEntities: true,但 SqlServerConnectionOptions 不接受它。不确定如何正确添加?

您的 config 不需要 autoLoadEntities 属性。它是 NestJS 使用的 属性 而不是 typeORM 使用的

TypeOrmModule.forRoot 接受类型为 TypeOrmModuleOptions:

的对象
export declare type TypeOrmModuleOptions = {
    retryAttempts?: number;
    retryDelay?: number;
    toRetry?: (err: any) => boolean;
    autoLoadEntities?: boolean;
    keepConnectionAlive?: boolean;
    verboseRetryLog?: boolean;
} & Partial<ConnectionOptions>;

您的 config: SqlServerConnectionOptionsConnectionOptions 的一部分,而 ConnectionOptionsTypeOrmModuleOptions 的一部分。

所以当你声明你的 typeORM 模块时,你可以使用类似下面的东西:

TypeOrmModule.forRoot({
  ...config,
  autoLoadEntities: true,
});