我在NestJS中使用typeorm,在app.module.ts中我尝试通过应用forRootAsync来初始化数据库设置
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
type: configService.get('database.type'),
host: configService.get('database.host'),
port: configService.get('database.port'),
username: configService.get('database.username'),
password: configService.get('database.password'),
database: configService.get('database.database'),
entities: configService.get('database.entities'),
synchronize: configService.get('database.synchronize'),
logging: configService.get('database.logging'),
}),
}),
在编译过程中,我得到了以下错误:
error TS2322: Type '(configService: ConfigService) => Promise<{ type: "mysql" | "mariadb" | "postgres" | "cockroachdb" | "sqlite" | "mssql" | "sap" | "oracle" | "cordova" | "nativescript" | "react-native" | "sqljs" | "mongodb" | "aurora-data-api" | "aurora-data-api-pg" | "expo" | "better-sqlite3"; ... 7 more ...; logging: LoggerOptions...' is not assignable to type '(...args: any[]) => TypeOrmModuleOptions | Promise<TypeOrmModuleOptions>'.
Type 'Promise<{ type: "mysql" | "mariadb" | "postgres" | "cockroachdb" | "sqlite" | "mssql" | "sap" | "oracle" | "cordova" | "nativescript" | "react-native" | "sqljs" | "mongodb" | "aurora-data-api" | "aurora-data-api-pg" | "expo" | "better-sqlite3"; ... 7 more ...; logging: LoggerOptions; }>' is not assignable to type 'TypeOrmModuleOptions | Promise<TypeOrmModuleOptions>'.
Type 'Promise<{ type: "mysql" | "mariadb" | "postgres" | "cockroachdb" | "sqlite" | "mssql" | "sap" | "oracle" | "cordova" | "nativescript" | "react-native" | "sqljs" | "mongodb" | "aurora-data-api" | "aurora-data-api-pg" | "expo" | "better-sqlite3"; ... 7 more ...; logging: LoggerOptions; }>' is not assignable to type 'Promise<TypeOrmModuleOptions>'.
Type '{ type: "mysql" | "mariadb" | "postgres" | "cockroachdb" | "sqlite" | "mssql" | "sap" | "oracle" | "cordova" | "nativescript" | "react-native" | "sqljs" | "mongodb" | "aurora-data-api" | "aurora-data-api-pg" | "expo" | "better-sqlite3"; ... 7 more ...; logging: LoggerOptions; }' is not assignable to type 'TypeOrmModuleOptions'.
Type '{ type: "mysql" | "mariadb" | "postgres" | "cockroachdb" | "sqlite" | "mssql" | "sap" | "oracle" | "cordova" | "nativescript" | "react-native" | "sqljs" | "mongodb" | "aurora-data-api" | "aurora-data-api-pg" | "expo" | "better-sqlite3"; ... 7 more ...; logging: LoggerOptions; }' is not assignable to type '{ retryAttempts?: number; retryDelay?: number; toRetry?: (err: any) => boolean; autoLoadEntities?: boolean; keepConnectionAlive?: boolean; verboseRetryLog?: boolean; } & Partial<AuroraDataApiConnectionOptions>'.
Type '{ type: "mysql" | "mariadb" | "postgres" | "cockroachdb" | "sqlite" | "mssql" | "sap" | "oracle" | "cordova" | "nativescript" | "react-native" | "sqljs" | "mongodb" | "aurora-data-api" | "aurora-data-api-pg" | "expo" | "better-sqlite3"; ... 7 more ...; logging: LoggerOptions; }' is not assignable to type 'Partial<AuroraDataApiConnectionOptions>'.
Types of property 'type' are incompatible.
Type '"mysql" | "mariadb" | "postgres" | "cockroachdb" | "sqlite" | "mssql" | "sap" | "oracle" | "cordova" | "nativescript" | "react-native" | "sqljs" | "mongodb" | "aurora-data-api" | "aurora-data-api-pg" | "expo" | "better-sqlite3"' is not assignable to type '"aurora-data-api"'.
Type '"mysql"' is not assignable to type '"aurora-data-api"'.
19 useFactory: async (configService: ConfigService) => ({
~~~~~~~~~~
node_modules/@nestjs/typeorm/dist/interfaces/typeorm-options.interface.d.ts:19:5
19 useFactory?: (...args: any[]) => Promise<TypeOrmModuleOptions> | TypeOrmModuleOptions;
~~~~~~~~~~
The expected type comes from property 'useFactory' which is declared here on type 'TypeOrmModuleAsyncOptions'
但是,如果在返回之前应用Object.assign(),它将通过
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) =>
Object.assign({
type: configService.get('database.type'),
host: configService.get('database.host'),
port: configService.get('database.port'),
username: configService.get('database.username'),
password: configService.get('database.password'),
database: configService.get('database.database'),
entities: configService.get('database.entities'),
synchronize: configService.get('database.synchronize'),
logging: configService.get('database.logging'),
}),
}),
这是因为Object.assign()将对象转换为任何类型吗?异步初始化类型的通常方法是什么?
发布于 2021-05-10 07:33:31
在搜索之后我找到了这条线。
https://github.com/nestjs/nest/issues/1119
它说我们需要特别指定类型。
因此,我在返回对象中更改了type属性:
type: configService.get('database.type') as 'mysql',
但是如果删除Object.assign(),我仍然会得到相同的编译错误,并且我注意到
password: configService.get('database.password')
将configService.get()转换为
string | (() => string) | (() => Promise<string>);
这会导致问题,所以我需要传递类型,最后的配置如下所示:
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
type: configService.get('database.type') as 'mysql',
host: configService.get('database.host'),
port: configService.get('database.port'),
username: configService.get('database.username'),
password: configService.get('database.password'),
database: configService.get<string>('database.database'),
entities: [configService.get('database.entities')],
synchronize: configService.get<boolean>('database.synchronize'),
logging: configService.get<boolean>('database.logging'),
}),
})
但我还是不知道为什么它会把密码转换成那种奇怪的类型。
发布于 2021-05-10 07:19:34
正如我从你的日志- Type '"mysql"' is not assignable to type '"aurora-data-api"'.
中看到的
你可以参考- 覆盖在ormconfig中定义的选项.在这里,它显式地提到了一个示例来附加自定义命名策略或自定义记录器。
TypeORM提供了getConnectionOptions
函数,该函数从ormconfig文件或环境变量中读取连接选项。- 关于TypeORM集成的提示
https://stackoverflow.com/questions/67465876
复制相似问题