【问题标题】:How to set autoLoadEntities: true in connecting Nest js with typeorm如何设置 autoLoadEntities: true 将 Nest js 与 typeorm 连接起来
【发布时间】:2021-08-26 15:49:44
【问题描述】:

我正在设置配置文件以将 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 不接受它。不知道如何正确添加?

【问题讨论】:

  • 您要添加trustServerCertificate 还是autoLoadEntities?您的问题令人困惑

标签: sql-server typescript nestjs typeorm


【解决方案1】:

您的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 的一部分,它是TypeOrmModuleOptions 的一部分。

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

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

【讨论】:

    猜你喜欢
    • 2019-04-16
    • 1970-01-01
    • 2021-12-17
    • 2022-01-04
    • 2023-01-25
    • 2020-05-17
    • 2021-06-01
    • 2021-05-06
    • 2017-01-12
    相关资源
    最近更新 更多