nestjs - NestJS/TypeORM - 连接到多个数据库 - ConnectionNotFoundError
问题描述
当使用每个数据库的连接(UserConnection 和 PhotoConnection)注册自定义存储库时,使用 TypeORM 和 NestJS连接到两个数据库会引发ConnectionNotFoundError 。我在这里创建了一个最小的仓库https://github.com/masonridge/connissuewithndb。TypeOrm 注册在 NestJS 的 AppModule 中完成
(app.module.ts)
@Module({
imports: [
TypeOrmModule.forRootAsync({
name: 'PhotoConnection',
useFactory: async () => {
return {
type: 'sqlite',
synchronize: true,
database: 'TestPhoto.sqlite',
entities: [Photo],
} as SqliteConnectionOptions;
},
}),
TypeOrmModule.forRootAsync({
name: 'UserConnection',
useFactory: async () => {
return {
type: 'sqlite',
synchronize: true,
database: 'TestUser.sqlite',
entities: [User],
} as SqliteConnectionOptions;
},
}),
// TypeOrmModule.forFeature([User, UserRepository], 'UserConnection'),
// TypeOrmModule.forFeature([Photo, PhotoRepository], 'PhotoConnection'),
PhotoModule, UserModule,
],
(photo.module.ts) DB connection1 - PhotoConnection 在这里注册
@Module({
imports: [
TypeOrmModule.forFeature([PhotoRepository], 'PhotoConnection'),
],
providers: [
PhotoService,
],
controllers: [PhotoController],
exports: [TypeOrmModule],
})
export class PhotoModule {}
(user.module.ts) DB connection2 - UserConnection 在这里注册
@Module({
imports: [
TypeOrmModule.forFeature([UserRepository], 'UserConnection'),
],
providers: [
UserService,
],
exports: [TypeOrmModule],
controllers: [UserController],
})
export class UserModule {}
(user.repository.ts) 自定义存储库
@EntityRepository(User)
export class UserRepository extends Repository<User> {
async createUser(name: string): Promise<string> {
const user = this.create();
user.username = name;
user.salt = 'salt';
user.password = 'xxkdkdk';
user.save();
return name;
}
}
(photo.repository.ts)
@EntityRepository(Photo)
export class PhotoRepository extends Repository<Photo> {
async createPhoto(name: string): Promise<string> {
const photo = this.create();
photo.name = name;
photo.save();
return name;
}
}
使用连接 (PhotoConnection) (photo.service.ts)将 repo 注入到服务中
export class PhotoService {
constructor(
@InjectRepository(Photo, 'PhotoConnection')
private readonly photoRepository: PhotoRepository,
) {}
在这里使用UserConnection (user.service.ts)
@Injectable()
export class UserService {
constructor(
@InjectRepository(User, 'UserConnection')
private readonly userRepository: UserRepository,
) {}
应用程序启动正常,但在 POST 请求中会引发 ConnectionNotFoundError 错误
(节点:190812)UnhandledPromiseRejectionWarning:ConnectionNotFoundError:未找到连接“默认”。在 ConnectionManager.get (C:\nestjs\type-orm-dbscratch\node_modules\typeorm\connection 的新 ConnectionNotFoundError (C:\nestjs\type-orm-dbscratch\node_modules\typeorm\error\ConnectionNotFoundError.js:10:28) \ConnectionManager.js:38:19) 在 Object.getConnection (C:\nestjs\type-orm-dbscratch\node_modules\typeorm\index.js:244:35) 在 Function.BaseEntity.getRepository (C:\nestjs\type -orm-dbscratch\node_modules\typeorm\repository\BaseEntity.js:67:57) 在 Photo.BaseEntity.save (C:\nestjs\type-orm-dbscratch\node_modules\typeorm\repository\BaseEntity.js:27:33 ) 在 PhotoRepository.createPhoto (C:\nestjs\type-orm-dbscratch\dist\db\photo\photo.repository.js:15:15) 在 PhotoService.createPhoto (C:
我想知道注册是否有问题。任何帮助,将不胜感激。
解决方案
看起来问题出在使用DataMapper和ActiveRecord的 TypeORM 方法之间。似乎使用该ActiveRecord
方法不支持带有 NestJS 的命名存储库,因此正在寻找名为'default'
. 如果您删除extends BaseEntity
并使用存储库的保存方法而不是this.save(user)
您user.save()
将获得成功的保存。我没有看到设置连接的直接方法,ActiveRecord
但如果这仍然是您想要遵循的方法,它可能是一个选项。
推荐阅读
- r - 从数据框中删除点(或句点)
- snowflake-cloud-data-platform - SQL 访问控制错误:权限不足,无法对架构“INFORMATION_SCHEMA”进行操作
- swift - SwiftUI从数组中返回不同类型的对象
- laravel - Laravel Api.php 路由组命名约定
- python - 用python中的边界替换单词的方法(如使用正则表达式)
- c# - 带有 ChromeDriver 的代理和 ChromeOptions 类不起作用
- google-chrome - AWS Amplify:DevTools 无法加载 SourceMap:位置 0 的 JSON 中的意外令牌 <
- r - 计算矩阵 R 内列表中的项目数
- vim - 在 WSL@Windows 10 上的带有 Cv 的 Vim 中,Visual-Block 模式不起作用
- r - 如何在 R 中将一个人的所有变量值相加?